我是初学程序员,正在浏览"Land of Lisp"这本书.
我一直在用REPL输入书中的例子.是否可以将我当前的程序保存为.lisp文件,以便我可以加载它并在以后继续处理它?我知道我可以在文本编辑器中创建.lisp文件并加载它们,但我很乐意在全屏模式下使用REPL来做示例.
我正在阅读"Lisp的土地"(顺便说一下,这是我读过的最好的技术书籍之一)我遇到了关联列表
(defparameter *edges*
'((living-room (garden west door)
(attic upstairs ladder))
(garden (living-room east door))
(attic (living-room downstairs ladder))))
Run Code Online (Sandbox Code Playgroud)
首先,Lisp中的关联列表与Java的Map(键值绑定)相同吗?
对于客厅钥匙,如何拥有多个价值?为什么不用列表包含值:
(living-room ((garden west door) (attic upstairs ladder)))
Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于Land of Lisp的好东西,所以我想我可以通过它来看看有什么可看的.
(defun tweak-text (lst caps lit)
(when lst
(let ((item (car lst))
(rest (cdr lst)))
(cond
; If item = space, then call recursively starting with ret
; Then, prepend the space on to the result.
((eq item #\space) (cons item (tweak-text rest caps lit)))
; if the item is an exclamation point. Make sure that the
; next non-space is capitalized.
((member item '(#\! #\? #\.)) (cons item (tweak-text rest t …
Run Code Online (Sandbox Code Playgroud) 我正在研究F#中的Land of Lisp一书(是的,我很奇怪).对于他们的第一个示例文本冒险,他们使用全局变量变异,我想避免它.我的monad-fu很弱,所以现在我正在做这样丑陋的状态:
let pickUp player thing (objects: Map<Location, Thing list>) =
let objs = objects.[player.Location]
let attempt = objs |> List.partition (fun o -> o.Name = thing)
match attempt with
| [], _ -> "You cannot get that.", player, objs
| thing :: _, things ->
let player' = { player with Objects = thing :: player.Objects }
let msg = sprintf "You are now carrying %s %s" thing.Article thing.Name
msg, player', things
let player = { …
Run Code Online (Sandbox Code Playgroud) 我正试图从"Land of Lisp"重写向导游戏 http://landoflisp.com/wizards_game.lisp
(def *nodes* {:living-room "you are in the living-room. a wizard is snoring loudly on the couch."
:garden "you are in a beautiful garden. there is a well in front of you."
:attic "you are in the attic. there is a giant welding torch in the corner."})
(def *edges* {:living-room '((garden west door) (attic upstairs ladder))
:garden '(living-room east door)
:attic '(living-room downstairs ladder)})
(defn describe-location [location nodes]
(nodes location))
(defn describe-path-raw [edge]
`(there is a ~(last …
Run Code Online (Sandbox Code Playgroud) 我正在从Conrad Barski的"Lisp之乡"一书中学习Lisp.现在我遇到了我的第一个绊脚石,作者说:
以这种方式调用自己不仅允许在Lisp中使用,而且经常受到强烈鼓励
在显示以下示例函数以计算列表中的项目之后:
(defun my-length (list)
(if list
(1+ (my-length (cdr list)))
0))
Run Code Online (Sandbox Code Playgroud)
当我my-length
用包含一百万个项目的列表调用此函数时,我收到堆栈溢出错误.因此,无论你休想有一个列表,长期在Lisp的(所以也许我用例是没用的),或者还有另一种方法来计算在这么长的列表项.你能否对此有所启发?(顺便说一句,我在Windows上使用GNU CLISP).
这段代码来自书:"Land of Lisp"第一版来自书.当我读它时,我认为有一个括号"("在第二行的"at-loc-p"之前没有必要,")"就在第三行的loc之后.
(defun person-at (loc pers per-locs)
(labels ((at-loc-p (pers)
(eq (cadr (assoc pers per-locs)) loc)))
(remove-if-not #'at-loc-p pers)))
Run Code Online (Sandbox Code Playgroud)
但是当我测试这个时,
(defun person-at (loc pers per-locs)
(labels (at-loc-p (pers)
(eq (cadr (assoc pers per-locs)) loc))
(remove-if-not #'at-loc-p pers)))
Run Code Online (Sandbox Code Playgroud)
它出来了:
AT-LOC-P中的必需参数与lambda列表不匹配(CCL :: FUNCNAME CCL :: LAMBDA-LIST&BODY CCL :: LABELS-FUNCTION-BODY).
[CCL类型的条件:: SIMPLE-PROGRAM-ERROR]
我不安静理解.需要帮忙.谢谢.
在329页的Lisp的土地,康拉德Barski解释的技术记忆化与下面的示例代码
(let ((old-neighbors (symbol-function 'neighbors))
(previous (make-hash-table)))
(defun neighbors (pos)
(or (gethash pos previous)
(setf (gethash pos previous) (funcall old-neighbors pos)))))
Run Code Online (Sandbox Code Playgroud)
这个想法很好:当我调用该neighbors
函数时,我将结果保存到哈希表中,以便下次neighbors
使用相同的值调用时pos
,我可以查找结果而无需再次计算它.
所以我想知道,是否不会是更容易的功能重新命名neighbors
,以old-neighbors
通过编辑和重新编译它的源代码(书的314页上给出).然后可以将memoization示例简化为
(let ((previous (make-hash-table)))
(defun neighbors (pos)
(or (gethash pos previous)
(setf (gethash pos previous) (funcall old-neighbors pos)))))
Run Code Online (Sandbox Code Playgroud)
或者,通过事先previous
变成一个全局变量*previous-neighbors*
,甚至进入
(defun neighbors (pos)
(or (gethash pos *previous-neighbors*)
(setf (gethash pos *previous-neighbors*) (funcall old-neighbors pos))))
Run Code Online (Sandbox Code Playgroud)
从而使封闭不必要.
所以我的问题是:这样做的原因是什么?
我能想象的原因:
symbol-function …
我正在尝试将以下宏从lisp的土地转换为clojure:
(defmacro tag (name atts &body body)
`(progn (print-tag ',name
(list ,@(mapcar (lambda (x)
`(cons ',(car x) ,(cdr x)))
(pairs atts)))
nil)
,@body
(print-tag ',name nil t)))
Run Code Online (Sandbox Code Playgroud)
但我一直陷入需要更多评估水平的投注.例如,以下需要评估t#:
(defmacro tag [tname atts & body]
`(do (print-tag '~tname '[~@(map (fn [[h# t#]] [h# t#]) (pair atts))] nil)
~@body
(print-tag '~tname nil true)))
Run Code Online (Sandbox Code Playgroud)
因为它产生如下东西:
(tag mytag [color 'blue size 'big])
<mytag color="(quote blue)" size="(quote big)"><\mytag>
Run Code Online (Sandbox Code Playgroud)
我希望评估属性的位置.如果我在上面使用"(eval t#)",我就会犯这样的问题:
(defn mytag [col] (tag mytag [colour col]))
java.lang.UnsupportedOperationException: Can't eval locals (NO_SOURCE_FILE:1)
Run Code Online (Sandbox Code Playgroud)
有什么建议?
为什么在Clojure中似乎发生了一个较低级别的评估?
支持功能的定义: …
在书中的'lisp之乡'我读过
因为case命令使用eq进行比较,所以它通常仅用于对符号值进行分支.除其他外,它不能用于分支字符串值.
请解释原因?
land-of-lisp ×10
common-lisp ×7
lisp ×7
clisp ×3
clojure ×2
f# ×1
jvm ×1
macros ×1
memoization ×1
monads ×1
refactoring ×1