1

I am writing a lisp interpreter (in C), and am at the point of implementing lambda functions and the set language features. In my interpreter the following works:

(set 'f (lambda (x) (cdr x)))
(f '(a b c))

outputting (b c). However, apparently this kind of thing is not allowed in Common Lisp as I get Input a value to be used instead of (FDEFINITION 'F) when I try this in clisp. My questions are the following:

  1. Why is this not allowed in Common Lisp?
  2. How does one achieve the same thing in Common Lisp?
  3. What should be changed in my interpreter so that this kind of thing is not allowed?

To help in your answering of the last question– currently the set primitive basically just associates an atom with a value in the environment. The above code works in my interpreter because to evaluate (f '(a b c)) it looks up f in the environment, finds that it is a lambda function, and then applies that lambda function to the argument '(a b c).

Thanks for the help.

Edit: This is not a duplicate of "What is the difference between Lisp-1 and Lisp-2?". Although differences between Lisp-1 and Lisp-2 may be the source of why this doesn't work in clisp, my question really centers on the mechanisms of the set primitive and it's underlying implementation.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Jon Deaton
  • 3,943
  • 6
  • 28
  • 41
  • 4
    because Common Lisp is a Lisp-2. functions and vars namespaces different. `set` sets a symbol's `symbol-value` i.e. its value, not function. see http://clhs.lisp.se/Body/f_fdefin.htm#fdefinition for `(setf (fdefinition foo) (lambda ....`. Also, `defun`. It looks like you've implemented a Lisp-1, where there's no distinction between variables' and functions' names. In Common Lisp we can `(setq list (list 1))` and it's OK. – Will Ness Nov 05 '17 at 22:06
  • 1
    @WillNess: why not make this an answer? – Svante Nov 05 '17 at 22:36
  • How is this a duplicate of "What's the difference between Lisp-1 and Lisp-2?"? – Jon Deaton Nov 06 '17 at 03:48
  • 1
    @JonDeaton: question 1 is answered: because Common Lisp is has different namespaces for functions and values. See the tag Lisp-2 for more answers. SET has really nothing special to do with it. It is just a way to set the value of a symbol. Question 3 is difficult to answer, since we don't know the language you are trying to implement, nor your interpreter. – Rainer Joswig Nov 06 '17 at 07:19
  • @Svante there's lots of minutia in Common lisp (fdefinition, symbol-function, ...) in which I'm not well-versed, to say the least. ---- to the OP, better stick with Lisp-1. it is much more manageable as a one-person project. Just leaf through CLHS to get a taste of what you'd be up against, start with that link to fdefinition I gave you. – Will Ness Nov 06 '17 at 08:57
  • @WillNess: Are two-namespace-Lisps more difficult to implement? – Rainer Joswig Nov 06 '17 at 13:56
  • @RainerJoswig aren't they? I thought they are. – Will Ness Nov 06 '17 at 15:46
  • There are lots of ways to make a lisp2 interpreter. One could prefix the symbol table with eg. `f` and `v` and rename all the symbols in the parser. When printing the first letter is omitted. That way you can keep the same symbol table you have today. You can make two symbol tables or one symbol table with several slots for operator and other position. Anyway how you do it its how you end up implementing `flet` and `labels` that would be proof of the pudding. – Sylwester Nov 06 '17 at 18:23
  • 1
    @WillNess: Christian Queinnec in 'Lisp in small pieces' makes relatively small changes to the interpreter code in chapter 2 to support a separate function namespace. The added complication does not seem to be a big hurdle, even for a one-man project... Generally I'd recommend the book to anyone who implements Lisp/Scheme. – Rainer Joswig Nov 06 '17 at 19:12
  • @RainerJoswig if you only introduce new function names through `labels` and such then yes, fine, but what about all the "minutia" with the `(setf (fdefinition ...` etc., it is suddenly a global symbol that gets changed, not the local lexical variable - is it? complex stuff. that's what I was referring to, not Lisp-2 *generically*. I could quote the comment but it's right above. BTW I read (some of) LiSP, and I loved it - more the Chapter 4 IIRC, with all the fancy Greek letters. :) And it's bound beautifully, on great paper too. A thing of beauty all around, in *and* out... – Will Ness Nov 06 '17 at 20:34
  • @Sylwester right; and what you describe is more complex than simple basic Lisp-1. is it *prohibitively* more so? YMMV. :) – Will Ness Nov 06 '17 at 20:35
  • @JonDeaton now you can appreciate that as the question about "the `set` primitive and it's underlying implementation" your question is probably too broad. – Will Ness Nov 06 '17 at 20:42
  • @WillNess: I don't see that setting global identifiers to something is that difficult. One more operator. Stuff like (SETF FDEFINITION) is Common Lisp specific and not needed in a Lisp-2. Lisp 1 called it LABEL. – Rainer Joswig Nov 06 '17 at 20:42
  • @RainerJoswig yes, I was fuzzy there, re: CL specifics. – Will Ness Nov 06 '17 at 20:43
  • I guess I could just make two environments- one for function/primitives and one for variables that are not functions. I could identify if something is a function if it starts with lambda or is of the primitive type. However, at this point I think I'm just going to leave it as is. – Jon Deaton Nov 06 '17 at 20:44
  • @JonDeaton no, you'd treat them differently from their position in the syntax - if it's a ``car`` of an application form then it *must be* in the functions' environment. *or* in the global environment. but what if it is not lexical, but special variable? They have their own dynamic environment. Or is that two environments? .... .... .... – Will Ness Nov 06 '17 at 20:46
  • @WillNess I agree that it perhaps is slightly more complex, but for a compiled language perhaps slightly easier. I have read Lisp in small pieces when I started learning lisp and I got to agree with Rainer that it was small changes at every step. `(setf (fdefinition ...))` and other that only works on top level is perhaps much easier than `labels`. OMG Now I want to implement a Lisp2 :-) – Sylwester Nov 06 '17 at 21:03

0 Answers0