INPUT :( A(B(D(E)(F)))(C)(K))我目前有两个函数,它给我一个OUTPUT:
一个
乙
C
ķ
乙
d
d
Ë
F
Ë
零
但是我需要这样的输出:
a:bck
b:d
c:
k:
d:ef
E:
F:
要么
一个
BSK
d
EF
(defun print-children (s)
(cond ((null (caar (cdr s))) nil)
(t (print (caar (cdr s))) (print-children (cdr s)))))
(defun print-tree (s)
(cond ((null s) nil)
((atom (car s)) (print (car s)) (print-children s) (print-tree (cdr s)))
(t (print-tree (car s)))))
Run Code Online (Sandbox Code Playgroud) 为什么从字符串中读取会产生无意义的结果?
CL-USER> (read-from-string "(+ 4 5 6)")
(+ 4 5 6)
9
CL-USER> (+ 4 5 6)
15
CL-USER> (eval (read-from-string "(+ 4 5 6)"))
15
CL-USER>
Run Code Online (Sandbox Code Playgroud) 所以我读到DrRacket对于常见的lisp来说是一个很好的IDE.我下载了它并将语言设置为R5RS并编写了以下函数定义:
(defun f (x)
(+ 5 5))
Run Code Online (Sandbox Code Playgroud)
但是它返回了一个错误
defun: undefined;
cannot reference undefined identifier
Run Code Online (Sandbox Code Playgroud)
我不知道如何修复它,考虑到defun是常见的lisp中一个完善的关键字.
好吧,我只是全新的Common Lisp编程语言,我已经从昨天开始自己学习这种语言,这也是出于兴趣.现在当我遇到函数和循环时,在了解它们之后我开始开发Prime数字Common Lisp中的问题.我的代码如下:
(defun prime (num)
(setq c 1)
(setq a 2)
(loop
(setq a (+ 1 a))
(if (= (mod num a) 0)
(setq c (+ c 1))
)
(when (> (+ a 1) 17) (return a))
)
)
(if (= c 1)
(return-from prime num)
)
)
(loop for x from 1 to 20
do (prime x)
)
Run Code Online (Sandbox Code Playgroud)
现在我遇到这个代码的问题是,每当我尝试执行此代码时,我得到的错误如下:
***IF:变量C没有值
但是我已经宣布了它的价值已经出现了.所以我想知道的是,即使我已经宣布了这个错误,为什么会出现这个错误.
所以看了3个小时的youtube视频,并花了很长时间阅读Lisp,我还没有看到这些"魔术宏"允许人们编写DSL,甚至可以做一些简单的事情,比如4 + 5
没有把它嵌入到一些大括号中.
这里有一些讨论:常见的lisp:输入数学表达式有一种不太痛苦的方法吗?但语法看起来并不好看,仍需要一些封闭的绒毛来定义宏的开始和结束位置.
所以这是挑战,定义一个中缀宏,然后使用它而不必用某种附加语法将其括起来.即
Some macro definition here
1 + 2
Run Code Online (Sandbox Code Playgroud)
不
Some macro definition here
(my-macro 1 + 2)
Run Code Online (Sandbox Code Playgroud)
不
Some macro definition here
ugly-syntax 1 + 2 end-ugly-syntax
Run Code Online (Sandbox Code Playgroud)
如果在Lisp中这是不可能的,那么大概是关于什么的呢?这有点像说"你有很棒的强大的宏,允许很酷的DSL,只要这些DSL包含在Lisp语法中".