2

Is there any way by which I can define functions my_list, my_cons, my_append which perform similar function as list, cons and append respectively?

Otherwise where can I find the implementation of these functions?

Thanks

2 Answers 2

2

For my_list and my_append, the solutions are:

(defun my_list (&rest arguments)
    `(,@arguments)
)

(defun my_append (a_list an_item)
    `(,@a_list ,an_item)
)

(my_append (my_list 'a 'b 'c) 'd)

I'm probably wrong but I dont know any alternative method to make pairs, so an alternative to cons do not seems possible. Still, I'm quite new to the LISP world.

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

10 Comments

Can you provide a simple explanation of how the codes work. Please.
the ` means the next symbol you'll write won't be evaluated. That's how we create the list. The , means the next symbol must be evaluated. (can only be used after a backquote) The @ removes the items from a list/pair to enumerate them. (can only be used after a ,)
and what are & and @? & is reference?
Sorry, pressed enter too fast. The &rest means variadic parameters. Which means that every arguments are in the arguments variable, in the form of a list.
Oh great! What abt cons? Any similar approach?
|
0

If you want your lists to be the same as the onces native to your application, You have to start with some primitive to construct a cons, probably cons or dotted-pair, and something to pull a cons cell apart (car, cadr). The others can be build from that.

If you want to re-implement things that are functionally (pun intended) equivalent, see http://en.wikipedia.org/wiki/Cons#Not_technically_fundamental

5 Comments

Those are scheme implementation. I think common-lisp implementations would be different.
Yes, but the concepts would still apply
Yes but can you just show me how cons can be implemented without using list and append? I think the implementations would be tougher.
The same functions can be defined in CL too, unless I'm missing something.
@Roger Nash the only difference in CL is that you would use (defun <name> (<args>) ... instead of (define (<name> <args>) ..., and you would have to (funcall z ... instead of (z ...

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.