Thinking in Erlang
David N Murray
dmurray@REDACTED
Wed Apr 28 04:29:43 CEST 2010
Hi,
I'm one of those people that learned lisp and moved on, but am a better
programmer for what I learned from lisp. One of the things I picked up
was function returning functions and the lisp version of closures. In my
Erlang reading, I tend to think this way when I read about anonymous
functions and closures.
I have a function:
(defun make-fqueue (n)
(let ((queue '()))
(lambda (e)
(push e queue)
(if (> (length queue) n)
(setf queue (subseq queue 0 n)))
queue)))
The above function returns a function that maintains a fixed-length queue,
pushing each element it receives onto the queue and popping the head off
when the queue reaches a certain size and always returning the queue.
Multiple calls to make-fqueue return multiple queue "objects."
What I'm realling doing above is encapsulating a mutable list and carrying
it around inside the function, hidden from the user. If I try and
implement something like this in Erlang, and don't want to be passing
the list around to whomever may use it, I'm doing it with a gen_server,
correct?
TIA,
Dave
More information about the erlang-questions
mailing list