2

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Man, I don't know how I goofed it up. I thought I'd done something similar to your first suggestion, but ended up with a list within a list within a list etc. Thanks!
1

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.

1 Comment

nice! :) (though quasiquote isn't part of the allowed repertoire here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.