views:

407

answers:

2

Is it possible to implement a closure in Erlang?

For example, how would I translate this snippet from Scheme?

(define (make-adder n)
  (lamdba (x) (+ x n)))

I've tried the following, but I'm clearly missing something.

make_adder(n) ->
    fun (x) -> x + n end.

Compiling this gives the error

Warning: this expression will fail with a 'badarith' exception
+11  A: 

You can't add atoms. Variables start with Capital Letters in erlang. words starting with lower case letters are atoms.

In other words your problem is not related to funs at all, you just need to capitalize your variable names.

sepp2k
Thanks! I'm deleting the question and going to bed!
grifaton
Can't delete question... just going to bed.
grifaton
Leave the question and change the title to 'Why do I get "this expression will fail with a 'badarith' exception"?' or something along those lines. That way all the other erlang beginners who'll make this mistake (I know I did) will easily find it when searching for the message.
sepp2k
+3  A: 
make_adder(N) ->
  fun (X) -> X + N end.
Christian