Implementing basic library functions in LISP (manually)

醉酒当歌 提交于 2019-12-12 10:07:55

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4215373/implementing-basic-library-functions-in-lisp-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!