lfe

Arity of + and - operators in Lisp Flavored Erlang


As I am just starting with LFE I've been trying out some examples from the book (SICP, LFE version) while reading the chapters. In exercise 1.4 page 76 I see the expressions #'+/2 and #'-/2.

So I enter into the REPL (funcall #'+/2 1 2) and get (as expected) 3. Similarly (funcall #'+/1 1) gives 1. But (funcall #'+/3 1 2 3) gives an error. Why is this? I could of course do (funcall #'+/2 1 (funcall #' +/2 2 3)) but I would really like to understand if and why #'+/3 is invalid, as it seems to be.

Lastly, somewhat tangentially, if I were implementing my own function FUNC how would I go about implementing the ability to accept arbitrary number of arguments in this way (funcall #'FUNC/arity ....). If you feel this should be a separate question, please help me with the terminology since I am a bit lost.


Solution

  • I'm also just getting started with LFE, but from what I gather, it doesn't support variadic functions. This makes me think that the math operators only accept 2 arguments. The fact that this works: (+ 1 2 3 4 5) makes me think there's a macro being run on that.

    LFE macros apparently can have any number of arguments according to this thread, though I haven't taken a deep dive into macros yet.

    My (n00b) advice is: if you think you need a variadic function, try passing a list to your function instead of variable arguments. If you still really think you need a variadic function, look into making it a macro.

    I suspect it's sound advice to say: avoid writing macros, unless there really isn't a better option. They're harder to analyze, debug, and consume in every language I've ever used, and while they're better in LISP than in most other languages, I think that advice still holds.