lisp

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)) )

Common Lisp学习笔记

老子叫甜甜 提交于 2021-02-07 22:47:47
全局变量以*号包裹 ------------------------------------------------------------- 列表:(list 元素1 元素2 元素3 ...) 属性表:(list 符号1 元素1 符号2 元素2 ...) 从属性表取值:(getf 属性表 符号) 遍历列表:(dolist 元素 列表),用PHP表述就相当于foreach(列表 as 元素) 压入元素:(push 元素 变量) ------------------------------------------------------------- 定义函数:(defun 函数名 (参数表) 函数体) 定义变量:(defvar 变量名 变量值) 变量赋值:(setf 变量名 变量值) ------------------------------------------------------------- format格式化参数的含义 # ~{ ... ~}:拆分一个属性表的键值对 # ~a:占位符 # ~t:制表符 # ~%:换行符 来源: oschina 链接: https://my.oschina.net/u/103341/blog/121425

Practical use of fold/reduce in functional languages

喜欢而已 提交于 2021-02-07 12:16:31
问题 Fold (aka reduce ) is considered a very important higher order function. Map can be expressed in terms of fold (see here). But it sounds more academical than practical to me. A typical use could be to get the sum, or product, or maximum of numbers, but these functions usually accept any number of arguments. So why write (fold + 0 '(2 3 5)) when (+ 2 3 5) works fine. My question is, in what situation is it easiest or most natural to use fold ? 回答1: The point of fold is that it's more abstract.

Practical use of fold/reduce in functional languages

无人久伴 提交于 2021-02-07 12:15:39
问题 Fold (aka reduce ) is considered a very important higher order function. Map can be expressed in terms of fold (see here). But it sounds more academical than practical to me. A typical use could be to get the sum, or product, or maximum of numbers, but these functions usually accept any number of arguments. So why write (fold + 0 '(2 3 5)) when (+ 2 3 5) works fine. My question is, in what situation is it easiest or most natural to use fold ? 回答1: The point of fold is that it's more abstract.

(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)

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

【SICP练习】5 练习1.9

ぃ、小莉子 提交于 2021-02-06 07:59:42
 以下是第一个加起两个正整数的方法,其中 inc 将参数加 1 , dec 将参数减 1 。 (define (+ a b) (if(= a 0) b (inc (+ (dec a) b)))) 用代换模型展示 (+ 4 5) 如下: (+ 4 5) (inc (+ 3 5)) (inc (inc (+ 2 5))) (inc (inc (inc (+ 1 5)))) (inc (inc (inc (inc (+ 0 5))))) (inc (inc (inc (inc 5)))) (inc (inc (inc 6))) (inc (inc 7)) (inc 8) 9 如上所示,在代换模型展示中包含了伸展和收缩两个阶段,并且伸展阶段所需的额外存储量和计算所需的步数都正比于参数 a 。因此这是一个线性递归过程。 以下是另一个加起两个正整数的方法。 (define (+ a b) (if(= a 0) b (+ (dec a) (inc b)))) 同样用代换模型展示 (+ 4 5) 如下: (+ 4 5) (+ 3 6) (+ 2 7) (+ 1 8) (+ 0 9) 9 在这个过程中并没有任何增长或者收缩,而其计算过程可用固定数目的状态变量( a )描述。这是一个线性迭代过程。 版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http:/

【SICP练习】2 练习1.6

こ雲淡風輕ζ 提交于 2021-02-06 07:47:19
 练习 1.6 这道题通过由一个新版本的 if 来引出,主要讨论的还是应用序和正则序的问题。我看到“将 if 提供为一种特殊形式”时还满头雾水,并不太清楚什么特殊形式。当再返回看 if 的语法时才发现,这在第 12 页 if 的一般表达式下面一段。如果 <predicate> 得到真值,解释器就去求值 <consequent> 并返回其值。注意,在此处已经返回其值了,并没有进行后续运算。 而通过 cond 写出来的常规过程的 if ,在解释器采用应用序求值的情况下,如果第一次运算 good-enough? 时为真,则直接返回了 guess 。 原文中的求平方根的程序: (define (new-if predicate then-clauseelse-clause) (cond(predicate then-clause) (elseelse-clause))) (define (sqrt-iter guess x) (new-if(good-enough? guess x) guess (sqrt-iter (improve guess x) x))) (define(sqrt-iter guess x) (if(good-enough? guess x) guess (sqrt-iter (improve guess x) x))) 于是博主进行了如下测试: (sqrt