Вопрос

I'm currently working on a clisp assignment making a basic address book. However, the caveat to this assignment is I can only use cons, car, cdr, cond, if, eq, assoc and setf to complete the assignment...

I decided early on to simply implement my own list and append procedures to simplify the process, but that isn't working particularly well. Frankly, I'm lost on how to generate a non-dotted list using only the above procedures.

Here's what I'm attempting to do:

(defun create-entry (name email phone-number address)
  (list (cons 'name name)
        (cons 'email email)
        (cons 'phone-number phone-number)
        (cons 'address address)))

Obviously I can't use list here, and my only solution thus far has ben to cons name and email, cons phone-number to that and cons address to that which isn't exactly what I'm looking for.

TL;DR Is there a way to implement the list procedure using only cons, car, cdr, cond, if, eq, assoc and setf.

Это было полезно?

Решение

A list is just a bunch of cons cells, linked one to the next, with nil as the last cdr. Therefore you could implement your create-entry function like this:

(defun create-entry (name email phone-number address)
  (cons (cons 'name name)
    (cons (cons 'email email)
      (cons (cons 'phone-number phone-number)
        (cons (cons 'address address)
          nil)))))

Alternatively, you could implement your own version of the list function like this:

(defun your-own-list (&rest l) l)

(defun create-entry (name email phone-number address)
  (your-own-list 
    (cons 'name name)
    (cons 'email email)
    (cons 'phone-number phone-number)
    (cons 'address address)))

But that feels like it's going against the spirit of the assignment.

Другие советы

Already checked but just so you know, eventually you'd do:

(defun create-entry (name email phone-number address)
  `((name  . ,name)
    (email . ,email)
    (phone-number . ,phone-number)
    (address . ,address)))

which uses quasiquote as convenient syntax to create structured lists.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top