我是lisp的菜鸟,只用了两个星期左右......
我用setq声明了一些全局变量:
(setq myvar '(WHATEVER))
和一个应该修改我告诉它的变量的函数:
(defun MYFUN (varname)
(setq varname '(POOP))
)
Run Code Online (Sandbox Code Playgroud)
但是当我拨打电话时:(MYFUN 'myvar)
并检查myvar
现在的值,它仍然是(WHATEVER)
如何进行MYFUN
持久性更改?
所以我知道通常这个错误意味着我忘了某个地方的括号(或者那个效果)但是对于我的生活,我无法弄清楚我哪里出错了.
我似乎得到的具体错误是:(SETF (GET (NTH CNT EXOTIC-CARS) 'MAKE) (READ MYSTREAM))
- 这基本上是我第一次定义函数的第一行.
整个代码在这里:
(setq exotic-cars '(car1 car2 car3 car4 car5 car6 car7 car8 car9 car10))
(defun dostuff ()
(fetchinput)
(printlist)
(findmake)
(mysort))
(defun fetchinput ()
(with-open-file (mystream "cars.dat")
(setq cnt 0)
(loop while (<= cnt 9)
do ((setf (get (nth cnt exotic-cars) 'make) (read mystream))
(setf (get (nth cnt exotic-cars) 'model) (read mystream))
(setf (get (nth cnt exotic-cars) 'cost) (read mystream))
(setf cnt (+ cnt 1))
)
)))
(defun findmake …
Run Code Online (Sandbox Code Playgroud) 我正在尝试写入Common Lisp中的文件,但我不断收到"文件不是流"错误:
[1]> (open "file.txt" :direction :output :if-does-not-exist :create :if-exists :supersede)
#<output buffered file-stream character #P"file.txt">
[2]> (princ 'Hello "file.txt")
*** - princ: argument "file.txt" is not a stream
Run Code Online (Sandbox Code Playgroud)
即使尝试关闭该文件也会返回错误:
[4]> (close "file.txt")
*** - no-applicable-method: When calling #<standard-generic-function close>
with arguments ("file.txt"), no method is applicable.
Run Code Online (Sandbox Code Playgroud)
该文件已正确创建,因此我认为它可能是一个权限问题,但似乎并非如此.到目前为止,我已经用Google搜索了这个错误.有谁知道我做错了什么?谢谢.
PS:我正在使用CLISP 2.49(2010-07-07)运行Linux Mint 17.3 Rosa
我不明白为什么我的程序表现得像它一样.
(defvar A '((X Y Z) (J L O P) (G W U))
(defvar Z '(X W D U G))
(defvar STOP 'G)
(defun ADD_to_Z(A2)
(prog()
(cond
((equal (Member_Of_Z (list A2)) 0) )
(t (setq Z (append Z (list A2))) )
)
)
)
(defun Member_of_Z(cdr_A1)
(prog(n temp)
(setq n 0)
(setq temp cdr_A1)
repeat
(cond
((null temp) (return n))
((null (member (car temp) Z) ) (setq n (+ n 1)) (setq temp (cdr temp)))
(t (setq n (+ n …
Run Code Online (Sandbox Code Playgroud) 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) 我目前正在尝试翻译这个 python 2 代码:
import math
def worstCaseArrayOfSize(n):
if n == 1:
return [1]
else:
top = worstCaseArrayOfSize(int(math.floor(float(n) / 2)))
bottom = worstCaseArrayOfSize(int(math.ceil(float(n) / 2)))
return map(lambda x: x * 2, top) + map(lambda x: x * 2 - 1, bottom)
Run Code Online (Sandbox Code Playgroud)
进入球拍/方案代码,并且遇到困难。
这是我到目前为止:
(define (msortWorstCase n)
(cond
[(equal? 1 n) 1]
[else (let* ([top (msortWorstCase(floor (/ n 2)))] [bottom (msortWorstCase (ceiling (/ n 2)))])
(append (map (lambda (x) (* x 2)) (list top)) (map (lambda (x) (- (* x 2) …
Run Code Online (Sandbox Code Playgroud) 正如标题所说,LISP的吸引力是什么?
我已经看到它抬高到高天然后,然后在一个基座上面翻过来.我已经看到这里的人,我认识的实际程序员,大学教授试图看起来像那些明显超越了平民语言战争的开明人员而宁愿死去而不是犯下异端邪说一种语言比另一种语言更好.
我试着去学习它,我看了几个教程,在lisp和clojure的"在x分钟内学习x"页面(因为据我所知,它是在jvm上运行的更多FP投入的lisp),我理解语法以及如何在其中执行操作.在过去的几个月里,我又多次回到教程中,但是每次语法都让我想找一个高中生的孩子,并因为混乱的代码而对他大喊大叫,因为看起来像是一个C风格的if条件变得疯狂所有嵌套的括号和简单的东西似乎成了一件苦差事.lisp似乎在几行中做了什么,其他FP语言如Haskell或F#,甚至像C#这样的非FP语言只是发现地图和过滤器似乎更少以更具表现力的方式进行.
我看错了吗?是否有需要发生的点击?我是否只找到了糟糕的资源?(如果是的话请给我一些很好的资源来学习lisp/clojure,最好是一些练习)
什么在......是lisp的吸引力?在这里诚实地向上帝提问.
先感谢您!
"程序"一词是否与SICP中的"操作"一词同义?(例如,在下面的章节中.)如果它们不相同,有什么区别和原因?
更具体地说,"复合操作"和"复合程序"之间有什么区别?有没有 ?
这是本书的另一章相关章节:
在我看来,在这些上下文中,SICP中的术语"操作"指的是算术运算(因为到目前为止,在实例中没有使用其他类型的操作 - 无论它们通常是什么).
(define function1 (lambda(val)
(if (list? val)
(function2 (val))
('!list))))
Run Code Online (Sandbox Code Playgroud)
当我尝试输入'(ttt)时,我收到以下错误:
application: not a procedure;
expected a procedure that can be applied to arguments
given: (t t t)
arguments...: [none]
Run Code Online (Sandbox Code Playgroud)
我已经定义了function2,它可以在我自己调用它时工作,但是我无法在function1中调用它.
所以看了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语法中".