on-lisp

Flatten a list using common lisp

僤鯓⒐⒋嵵緔 提交于 2019-12-02 13:10:16
问题 I was reading the book On Lisp by Paul Graham. In Chapter 4, Utility Functions, he gives examples of small functions that operate on lists, which would be helpful while writing a larger program. One of them is flatten . Given a nested list at any arbitrary level as argument, flatten will remove all the nested elements and put them on the top level. Below is my attempt at implementing flatten: (defun flatten (lst) (labels ((rflatten (lst1 acc) (dolist (el lst1) (if (listp el) (rflatten el acc)

Flatten a list using common lisp

泄露秘密 提交于 2019-12-02 07:03:02
I was reading the book On Lisp by Paul Graham. In Chapter 4, Utility Functions, he gives examples of small functions that operate on lists, which would be helpful while writing a larger program. One of them is flatten . Given a nested list at any arbitrary level as argument, flatten will remove all the nested elements and put them on the top level. Below is my attempt at implementing flatten: (defun flatten (lst) (labels ((rflatten (lst1 acc) (dolist (el lst1) (if (listp el) (rflatten el acc) (push el acc))) acc)) (reverse (rflatten lst nil)))) But the above function does not flatten lists

continuation in common lisp by macros — regarding an implemetation in OnLisp

穿精又带淫゛_ 提交于 2019-11-30 05:28:52
In On Lisp , p. 267, Paul Graham provides an implementation of continuation passing macros: (setq *cont* #'identity) (defmacro =lambda (parms &body body) `#'(lambda (*cont* ,@parms) ,@body)) (defmacro =defun (name parms &body body) (let ((f (intern (concatenate 'string "=" (symbol-name name))))) `(progn (defmacro ,name ,parms `(,',f *cont* ,,@parms)) (defun ,f (*cont* ,@parms) ,@body)))) (defmacro =bind (parms expr &body body) `(let ((*cont* #'(lambda ,parms ,@body))) ,expr)) (defmacro =values (&rest retvals) `(funcall *cont* ,@retvals)) The following code to traverse a tree t2 for each leaf

continuation in common lisp by macros — regarding an implemetation in OnLisp

最后都变了- 提交于 2019-11-29 04:09:42
问题 In On Lisp, p. 267, Paul Graham provides an implementation of continuation passing macros: (setq *cont* #'identity) (defmacro =lambda (parms &body body) `#'(lambda (*cont* ,@parms) ,@body)) (defmacro =defun (name parms &body body) (let ((f (intern (concatenate 'string "=" (symbol-name name))))) `(progn (defmacro ,name ,parms `(,',f *cont* ,,@parms)) (defun ,f (*cont* ,@parms) ,@body)))) (defmacro =bind (parms expr &body body) `(let ((*cont* #'(lambda ,parms ,@body))) ,expr)) (defmacro =values

Strange Lisp Quoting scenario - Graham's On Lisp, page 37

早过忘川 提交于 2019-11-27 23:32:54
问题 I'm working my way through Graham's book "On Lisp" and can't understand the following example at page 37: If we define exclaim so that its return value incorporates a quoted list, (defun exclaim (expression) (append expression ’(oh my))) > (exclaim ’(lions and tigers and bears)) (LIONS AND TIGERS AND BEARS OH MY) > (nconc * ’(goodness)) (LIONS AND TIGERS AND BEARS OH MY GOODNESS) could alter the list within the function: > (exclaim ’(fixnums and bignums and floats)) (FIXNUMS AND BIGNUMS AND