common-lisp

Understanding Peter Norvig's permutation solution in PAIP

為{幸葍}努か 提交于 2021-02-20 19:00:28
问题 Peter Norvig's PAIP book contains this code as a solution to the permutation problem (some sections are removed for brevity) (defun permutations (bag) ;; If the input is nil, there is only one permutation: ;; nil itself (if (null bag) '(()) ;; Otherwise, take an element, e, out of the bag. ;; Generate all permutations of the remaining elements, ;; And add e to the front of each of these. ;; Do this for all possible e to generate all permutations. (mapcan #'(lambda (e) (mapcar #'(lambda (p)

SLIME on Emacs with paredit in repl - how to prevent execution of incomplete but balanced expressions?

时光总嘲笑我的痴心妄想 提交于 2021-02-20 06:38:46
问题 I use paredit on emacs with SLIME's repl. This means that at any point during my typing on the repl, my s-expressions are balanced. However, they may not be complete, and I might want to continue typing inside them in another line, as follows: CL-USER> (defun print-hello () ) When I start a new line by pressing the enter key, however, the SLIME repl executes my incomplete expression. I want it to wait for me to complete the expression, as follows: CL-USER> (defun print-hello () (format t

SLIME on Emacs with paredit in repl - how to prevent execution of incomplete but balanced expressions?

我的未来我决定 提交于 2021-02-20 06:35:34
问题 I use paredit on emacs with SLIME's repl. This means that at any point during my typing on the repl, my s-expressions are balanced. However, they may not be complete, and I might want to continue typing inside them in another line, as follows: CL-USER> (defun print-hello () ) When I start a new line by pressing the enter key, however, the SLIME repl executes my incomplete expression. I want it to wait for me to complete the expression, as follows: CL-USER> (defun print-hello () (format t

Common LISP: convert (unknown) struct object to plist?

时光毁灭记忆、已成空白 提交于 2021-02-19 08:07:09
问题 (defstruct (mydate (:constructor make-mydate (year month day))) (year 1970) (month 1) (day 1)) (defvar *date1* (make-mydate 1992 1 1)) The problem is more general, but say I would like to convert an object like date1 to a "document" I can persist to a database (e.g. mongoDB, using package cl-mongo). So I write (defun mydate->document (mydate) (cl-mongo:$ (cl-mongo:$ "year" (mydate-year mydate)) (cl-mongo:$ "month" (mydate-month mydate)) (cl-mongo:$ "day" (mydate-day mydate)))) REPL--> (mydate

for/continue in scheme/lisp

旧街凉风 提交于 2021-02-19 01:16:57
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows

for/continue in scheme/lisp

扶醉桌前 提交于 2021-02-19 01:14:03
问题 I'm writing a small interpreter for a C-like language in Scheme (R5RS) and trying to convert something like: for (i = 0; i < 100; i++) { if (isprime(i)) continue; else /* do something with i */ } to valid Scheme (the isprime function is just an example and not important). However, after trying for some time, I have not been able to find an efficient/simple way to add the equivalent of a continue statement to a do loop in Scheme. What would be even better would be a "for" macro which allows

example of using external libraries or packages in Common Lisp

喜欢而已 提交于 2021-02-18 16:54:49
问题 In Common Lisp, quicklisp is a popular library management tool. I'm going to use that tool and I'm going to try and use CL-WHO. I use the SBCL 1.0.57 implementation. I'm going to answer my own question below. As a beginner, it's not clear how ASDF and quicklisp actually work together. And so it's not clear how to actually use packages or libraries that you've downloaded through quicklisp in an external source file. The quicklisp FAQ, at least at this moment, does not help. In python, it's

Understanding sharp quote in common lisp

落花浮王杯 提交于 2021-02-17 04:30:49
问题 In my experiments below I've abbreviated where the REPL returns an error, & added [num] so these can be referenced in discussion. I'm a bit confused as to why my attempts to call a function stored in a variable are failing. It seems to me that the syntax is more complex than it needs to be. Why can I issue neither (f 3) nor even (#'f 3) ? Is sharp quote not allowed as the first element of a form? Why is funcall required here? [235]> (setf f #'abs) ; I'm ok with this #<SYSTEM-FUNCTION ABS>

Understanding sharp quote in common lisp

主宰稳场 提交于 2021-02-17 04:29:48
问题 In my experiments below I've abbreviated where the REPL returns an error, & added [num] so these can be referenced in discussion. I'm a bit confused as to why my attempts to call a function stored in a variable are failing. It seems to me that the syntax is more complex than it needs to be. Why can I issue neither (f 3) nor even (#'f 3) ? Is sharp quote not allowed as the first element of a form? Why is funcall required here? [235]> (setf f #'abs) ; I'm ok with this #<SYSTEM-FUNCTION ABS>

evaluate an argument of Common Lisp macro

回眸只為那壹抹淺笑 提交于 2021-02-11 06:30:44
问题 I want to make a macro, which behaviour will depend from one of it's arguments. For example: (defclass myvar () ((l :initarg :l :reader l))) (defparameter mv1 (make-instance 'myvar :l 10)) (defmacro mac1 (v) `(progn ,@(loop for x upto (eval `(l ,v)) collect `(format t "~A~%" ,x)))) (mac1 mv1) 1 2 3 4 5 6 7 8 9 10 This works fine. But if I'm trying to do the same with local variable: (let ((mv2 (make-instance 'myvar :l 10))) (mac1 mv2)) I'm getting the error: The variable MV2 is unbound. It is