lisp

Why do we need `nil`?

家住魔仙堡 提交于 2019-12-01 21:42:10
问题 I do not see why we need nil [1] when to cons a sequence (so-called proper list) of items. It seems to me we can achieve the same goal by using the so-called improper list ( cons -ed pairs without an ending nil ) alone. Since Lisps [2] have already provided a primitive procedure to distinguish between a pair? and an atom (some implementations even provide atom? ), when defining a procedure on a list, e.g., length , I can do the same with just dotted-pairs, as shown below: (define len (lambda

Lisp function call error

拈花ヽ惹草 提交于 2019-12-01 21:12:13
问题 I've written a Lisp function like this: (defun power (base exponent) (if (= exponent 0) 1 (* base (power (- exponent 1))))) When I try to call it, however, I get some errors: CL-USER 2 > (power 2 3) Error: POWER got 1 arg, wanted at least 2. 1 (abort) Return to level 0. 2 Return to top loop level 0. Type :b for backtrace or :c <option number> to proceed. Type :bug-form "<subject>" for a bug report template or :? for other options. CL-USER 3 : 1 > (power 2) Error: POWER got 1 arg, wanted at

Why is #' (sharp-quote) notation unnecessary in CLISP?

你说的曾经没有我的故事 提交于 2019-12-01 21:10:23
I'm learning Lisp from the book 'Practical Common Lisp'. At one point, I'm supposed to enter the following bit of code: [1] (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10)) (2 4 6 8 10) I suppose the idea here is of course that remove-if-not wants a function that can return either T or NIL when an argument is provided to it, and this function is then applied to all symbols in the list, returning a list containing only those symbols where it returned NIL. However, if I now write the following code in CLISP: [2] (remove-if-not 'evenp '(1 2 3 4 5 6 7 8 9 10) (2 4 6 8 10) It still works! So my

Three questions w.r.t. the environment model of evaluation

﹥>﹥吖頭↗ 提交于 2019-12-01 20:13:18
问题 I am reading the SICP book Here about the imperative programming model. I could not understand the illustration in two points: W.r.t. the arrow from square to the "pair" (the two circles): What does this arrow mean? Though throughout this section, an arrow means "enclosing environment", this specific arrow seems not pointing to an environment.( square 's environment is the global env , not the "pair") Is below a correct understanding: in the value of a procedure definition, its "code text"

Three questions w.r.t. the environment model of evaluation

◇◆丶佛笑我妖孽 提交于 2019-12-01 20:12:32
I am reading the SICP book Here about the imperative programming model. I could not understand the illustration in two points: W.r.t. the arrow from square to the "pair" (the two circles): What does this arrow mean? Though throughout this section, an arrow means "enclosing environment", this specific arrow seems not pointing to an environment.( square 's environment is the global env , not the "pair") Is below a correct understanding: in the value of a procedure definition, its "code text" part (the left circle) has no interpretation of the symbols within. They are just "text". Only at

Why do we need `nil`?

拈花ヽ惹草 提交于 2019-12-01 19:24:43
I do not see why we need nil [1] when to cons a sequence (so-called proper list) of items. It seems to me we can achieve the same goal by using the so-called improper list ( cons -ed pairs without an ending nil ) alone. Since Lisps [2] have already provided a primitive procedure to distinguish between a pair? and an atom (some implementations even provide atom? ), when defining a procedure on a list, e.g., length , I can do the same with just dotted-pairs, as shown below: (define len (lambda (l) (cond ((pair? l) (+ 1 (len (cdr l)))) (else 1) ) ) ) It is obvious that we can apply this procedure

Lisp - Print out () instead of nil for empty list

六眼飞鱼酱① 提交于 2019-12-01 18:58:34
问题 I have a Lisp program that's going through nested list and deleting elements that match the element passed through to the function. My issue is, if everything in one of the nested list is deleted, I need to print out () instead of NIL. (defun del (x l &optional l0) (cond ((null l) (reverse l0)) ((if (atom x) (eq x (car l)) (remove (car l) x)) (del x (cdr l) l0)) (T (del x (cdr l) (cons (if (not (atom (car l))) (del x (car l)) (car l)) l0))))) (defun _delete(a l) (format t "~a~%" (del a l))) (

Why is #' (sharp-quote) notation unnecessary in CLISP?

≯℡__Kan透↙ 提交于 2019-12-01 18:32:08
问题 I'm learning Lisp from the book 'Practical Common Lisp'. At one point, I'm supposed to enter the following bit of code: [1] (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10)) (2 4 6 8 10) I suppose the idea here is of course that remove-if-not wants a function that can return either T or NIL when an argument is provided to it, and this function is then applied to all symbols in the list, returning a list containing only those symbols where it returned NIL. However, if I now write the following

Lisp - Print out () instead of nil for empty list

和自甴很熟 提交于 2019-12-01 18:31:11
I have a Lisp program that's going through nested list and deleting elements that match the element passed through to the function. My issue is, if everything in one of the nested list is deleted, I need to print out () instead of NIL. (defun del (x l &optional l0) (cond ((null l) (reverse l0)) ((if (atom x) (eq x (car l)) (remove (car l) x)) (del x (cdr l) l0)) (T (del x (cdr l) (cons (if (not (atom (car l))) (del x (car l)) (car l)) l0))))) (defun _delete(a l) (format t "~a~%" (del a l))) (_delete 'nest '(nest (second nest level) (third (nest) level))) This returns ((SECOND LEVEL (THIRD NIL

Can't call functions defined in macro with names generated by make-symbol

牧云@^-^@ 提交于 2019-12-01 17:39:01
问题 I'm trying to write an ELisp macro to generate a multiple functions based on some common data. For example, when I want to compute the fn names I write something like (I'm ignoring hygiene for the moment, I'm passing a symbol literal into the macro so evaluation shouldn't matter): (cl-defmacro def-fns (sym) "SYM." (let ((s1 (make-symbol (concat (symbol-name sym) "-1"))) (s2 (make-symbol (concat (symbol-name sym) "-2")))) `(progn (defun ,s1 () (+ 1 2 3)) (defun ,s2 () "six")))) which I expect