-3

I had this question in an exam, how would you solve it? CONS is a fundamental Common Lisp function. Which functionality must the Common Lisp environment provide to make it work? What would happen to this code without it?

(defun test(n l1 l2) (when (plusp n) (append l1 l2) (something (1- n) l1 l2)))

prompt> (test fourtytwo '(4) '(2))

  • 1
    Are you sure you have reproduced your question correctly? It barely make sense. Why you pass symbol fourtytwo (unquoted!) as first argument when it's clear from code that you must provide a number? – Mark Karpov Jun 19 '14 at 16:26
  • 1
    Also, have you tried to find any information about structure of lists in Lisp? Top lines of Google results for "common lisp cons" include these sources: [one](http://www.lispworks.com/documentation/lw61/CLHS/Body/f_cons.htm) and [another one](http://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list-processing.html). The second link should give you enough information on the topic. – Mark Karpov Jun 19 '14 at 16:29
  • I suspect the expected answer to this question relies on some concept of "functionality" taught during the course. – molbdnilo Jun 24 '14 at 08:55

1 Answers1

0

From which perspective?

From a language implementer you need memory and a data type that takes two pointers and perhaps flags for type and gc unless it's embedded in the pointer.

For a developer it needs two arguments holding any data. Both the reader and append uses it so without you won't have cons cells and therefor not lists either.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • "From a language implementer you need memory and a data type that takes two pointers and perhaps flags for type and gc unless it's embedded in the pointer." That might suggest a little bit too much in the way of implementation detail. Lexical closures are sufficient. E.g., http://pastebin.com/3TX0Ny1S. All you need to guarantee is that `(car (cons 1 2)) => 1)` and `(cdr (cons 1 2)) => 2`. – Joshua Taylor Jun 19 '14 at 19:19
  • @JoshuaTaylor If you were to implement `cons` with environment you move the memory and pointer requirement to the implementation of closures. IMO `cons` is far easier than closures to implement. It's very much like you can implement `cond` with `if` and vice versa but one of them need to be implemented as a primitive to get it working. – Sylwester Jun 20 '14 at 06:43