Functions as argument in higher order function

42 views
Skip to first unread message

Lothar Pfeiler

unread,
Jan 20, 2015, 4:36:05 AM1/20/15
to lisp-flavo...@googlegroups.com
Hi all,

I'm new to lfe and to LISP-2 languages. I tried to use lists:map in lfe, but to pass a function to it, the only thing that worked was to wrap it in a lambda, e.g.

(: lists map (lambda (x) (double x)) '(1 2 3))

Is there a better way similar to

(: lists map #'double '(1 2 3))  ???

Thanks, for your help.

Best Regards,
Lothar

Robert Virding

unread,
Jan 20, 2015, 12:29:34 PM1/20/15
to lisp-flavo...@googlegroups.com
You need to add the arity as well. If it's a function in the same module you can write

(: lists map #'double/1 '(1 2 3))

and if it's in another module you need the module name as well

(: lists map #'my-lib:double/1 '(1 2 3))

Btw both of these transform to the forms (fun double 1) and (fun my-lib double 1) respectively. You can now write a function call to another module with the module:name as one atom so:

(lists:map #'double/1 '(1 2 3))

A bit nicer.

That you need the arity of the function is more an erlang property as you can have functions with the same name but a different no. of args, the arity, and they are different functions. So you have to be precise and say which one you mean. Functions cannot a variable no. of args.

Robert

Lothar Pfeiler

unread,
Jan 20, 2015, 1:23:25 PM1/20/15
to lisp-flavo...@googlegroups.com
Thanks Robert,

and I really appreciate the possibility to have functions with the same name, but different arity. I just haven't thought of adding the arity. It makes perfect sense.

Lothar
Reply all
Reply to author
Forward
0 new messages