common-lisp

Equivalent of Clojure's “assoc-in” and “get-in” in Common lisp

家住魔仙堡 提交于 2021-02-10 12:01:26
问题 In Clojure you can update a map (dict) with assoc-in and create key path automatically if it don't exist. (assoc-in {:a 1 :b 3} [:c :d] 33) ; {:a 1, :c {:d 33}, :b 3} Same for get-in: you can specify a path of keys (or list indices) and it will return the value specified by the path, nil if it does not exist. (get-in {:a 1, :c {:d 33}, :b 3} [:c :d]) ; 33 (get-in {:a 1, :c {:d 33}, :b 3} [:c :e]) ; nil I like to have the sugar in Common lisp,so I monkeyed a 'assoc-in' and I tested it on a

How to convince Lisp SBCL to do inline fixnum arithmetic?

為{幸葍}努か 提交于 2021-02-08 05:02:03
问题 I've found some techniques in other SO answers, but apparently I've been unable to convince SBCL to do inline fixnum arithmetic: (declaim (optimize (speed 2) (safety 1))) (declaim (ftype (function (fixnum fixnum) double-float) fixnumtest) (inline fixnumtest)) (defun fixnumtest (i j) (declare (type fixnum i j)) (let* ((n (the fixnum (+ i j))) (n+1 (the fixnum (1+ n)))) (declare (type fixnum n n+1)) (/ 1.0d0 (the fixnum (* n n+1)) ) ) ) (defun main () (format t "~11,9F~%" (fixnumtest 2 3)) )

How to convince Lisp SBCL to do inline fixnum arithmetic?

℡╲_俬逩灬. 提交于 2021-02-08 05:01:36
问题 I've found some techniques in other SO answers, but apparently I've been unable to convince SBCL to do inline fixnum arithmetic: (declaim (optimize (speed 2) (safety 1))) (declaim (ftype (function (fixnum fixnum) double-float) fixnumtest) (inline fixnumtest)) (defun fixnumtest (i j) (declare (type fixnum i j)) (let* ((n (the fixnum (+ i j))) (n+1 (the fixnum (1+ n)))) (declare (type fixnum n n+1)) (/ 1.0d0 (the fixnum (* n n+1)) ) ) ) (defun main () (format t "~11,9F~%" (fixnumtest 2 3)) )

(compose) in Common Lisp

巧了我就是萌 提交于 2021-02-07 06:17:46
问题 We find this function builder to realize composition in P.Graham's "ANSI Common Lisp" (page 110). The arguments are n>0 quoted function names. I don't understand it completely, so I'll quote the code here and specify my questions underneath it: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The argument list to compose is reversed and unpacked, its (now first)

(compose) in Common Lisp

好久不见. 提交于 2021-02-07 06:16:43
问题 We find this function builder to realize composition in P.Graham's "ANSI Common Lisp" (page 110). The arguments are n>0 quoted function names. I don't understand it completely, so I'll quote the code here and specify my questions underneath it: (defun compose (&rest fns) (destructuring-bind (fn1 . rest) (reverse fns) #'(lambda (&rest args) (reduce #'(lambda (v f) (funcall f v)) rest :initial-value (apply fn1 args))))) The argument list to compose is reversed and unpacked, its (now first)

Static code analysis tool for Common Lisp?

你离开我真会死。 提交于 2021-02-07 05:43:30
问题 I'm busy learning Common Lisp, & I'm looking for a static code analysis tool that will help me develop better style & avoid falling into common traps. I've found Lisp Critic and I think it looks good, but I was hoping that someone may be able to recommend some other tools, and / or share their experiences with them. 回答1: Given the dynamic nature of Lisp, static analysis is everything from tough to impossible, depending on the type of source code. For some purposes I would recommend using the

Why sharp quote lambda expressions?

巧了我就是萌 提交于 2021-02-06 09:58:55
问题 It is a technique used frequently in On Lisp , which is on Common Lisp: > (mapcar #'(lambda (x) (+ x 10)) '(1 2 3)) (11 12 13) Why is sharp-quote needed or even possible? lambda expressions return function objects, and sharp quoting returns function objects from names. I also have heard contradictory information on whether lambda expressions are names - in particular On Lisp contradicts the standard, but his code seems to work which also contradicts the standard. In elisp it seems it's not

Why sharp quote lambda expressions?

不打扰是莪最后的温柔 提交于 2021-02-06 09:58:25
问题 It is a technique used frequently in On Lisp , which is on Common Lisp: > (mapcar #'(lambda (x) (+ x 10)) '(1 2 3)) (11 12 13) Why is sharp-quote needed or even possible? lambda expressions return function objects, and sharp quoting returns function objects from names. I also have heard contradictory information on whether lambda expressions are names - in particular On Lisp contradicts the standard, but his code seems to work which also contradicts the standard. In elisp it seems it's not

How to read sexp from file

て烟熏妆下的殇ゞ 提交于 2021-02-05 05:40:46
问题 If I write a file using (with-open-file (s "~/example.sexp" :direction :output) (write '(1 2 3) :stream s) (write '(4 5 6) :stream s) (write '(7 8 9) :stream s)) A file is created containing (1 2 3)(4 5 6)(7 8 9) But when I attempt to open and read it using (setf f (open "~/example.sexp")) (read :input-stream f) I get an ":INPUT-STREAM is not of type STREAM" error. (type-of f) returns STREAM::LATIN-1-FILE-STREAM which looks like it is at least close to what I need. What is the difference? How

How to read sexp from file

孤街醉人 提交于 2021-02-05 05:38:05
问题 If I write a file using (with-open-file (s "~/example.sexp" :direction :output) (write '(1 2 3) :stream s) (write '(4 5 6) :stream s) (write '(7 8 9) :stream s)) A file is created containing (1 2 3)(4 5 6)(7 8 9) But when I attempt to open and read it using (setf f (open "~/example.sexp")) (read :input-stream f) I get an ":INPUT-STREAM is not of type STREAM" error. (type-of f) returns STREAM::LATIN-1-FILE-STREAM which looks like it is at least close to what I need. What is the difference? How