我有以下代码:
(define (howMany list)
(if (null? list)
0
(+ 1 (howMany (cdr list)))))
Run Code Online (Sandbox Code Playgroud)
如果我们执行以下操作:(howMany '(1 2 3 (4 5)))我们将得到 4 结果。我该如何做才能计算列表中的整数数量。这意味着同样的问题将返回 5 作为答案,而不是 4。
在符合 R5RS 的方案版本中将文本输出到文件的简单方法是什么?我使用 MIT 的 MEEP(它使用 Scheme 进行脚本编写)并且我想将文本输出到文件。我在 Stackoverflow 上找到了以下其他答案:
将字符串附加到 IronScheme 中的现有文本文件 [原文如此]
但是,它们并不正是我想要的。
我正在使用 DrRacket 6.4 版(英语)在Scheme 中创建一个小应用程序。我想知道是否有更有效的方法来连接以下代码。[它有效,我只是不确定它是否是最干净的,因为我是计划的新手]
(display "Rolling ")
(display (number->string (- 5 (length my-rolled-dice))))
(display " dice\n")
(display "You rolled\n")
(define my-roll (make-list-of-random-numbers (- 5 (length my-rolled-dice) ) 6))
(display my-roll)
(display "\n")
Run Code Online (Sandbox Code Playgroud)
我正在寻找屏幕上的以下输出
Rolling 5 dice
You rolled
(3 1 3 6 6)
Run Code Online (Sandbox Code Playgroud)
有没有更干净的方法来编写这个,或者这是否像Scheme中那样干净?
我是 Racket 和 Lisp 的初学者,正在研究语法定义。我定义了一个简单的转换,如下所示:
(define-syntax hello
(syntax-rules (in)
((_ name in world) (format "Hello ~a in ~a" name world))
((_ in name) (format "Hello ~a in here" name))))
Run Code Online (Sandbox Code Playgroud)
现在,当我像这两种情况一样运行它时,它工作得很好:
(hello "me" in "world") === "Hello me in world"
Run Code Online (Sandbox Code Playgroud)
和,
(define in "inside")
(hello me in in) === "Hello me in inside"
Run Code Online (Sandbox Code Playgroud)
但是,这会导致错误,
(let ([in "Shire"])
(hello "Martin" in in)) === Error: hello: bad syntax in: (hello "Martin" in in)
Run Code Online (Sandbox Code Playgroud)
那么,为什么 hello 在 let 绑定中失败,而对于定义却工作得很好呢?另外,我在哪里可以获得有关这种差异的更多具体信息?谢谢。
这是一个初学者的问题。然而,我花了两个多小时试图找出错误(我也进行了搜索)但没有成功。
(define a (lambda (l i) (
(cond ((null? l) l)
(else (cons (cons (car l) i) (a (cdr l) i))))
)))
Run Code Online (Sandbox Code Playgroud)
该函数a应该将原子i与 的每一项配对l。例如:
(a '(1 2 3) 4)应该返回((1 4) (2 4) (3 4))
但是,当我尝试使用调用该函数时,我得到:
The object () is not applicable
Run Code Online (Sandbox Code Playgroud)
我的函数有什么错误?
我正在使用mit-scheme --load a.lisp加载文件。然后我通过在交互模式下键入来调用函数 a。
我已经成功安装了 DrRacket 的 SICP 包;我通过使用incGUI 中的操作符对其进行了测试。我按照这里的说明进行操作。
但是,当我racket在 bash 中运行时,使用inc运算符会出现错误。
欢迎使用 Racket v6.6。
>(包括 42)
公司:未定义;无法引用未定义的标识符上下文...:/usr/share/racket/collects/racket/private/misc.rkt:88:7
显然,我没有正确安装它。那我该怎么办?
编辑 1:此外,对于 DrRacket 的 GUI 和bash ,1+和-1+运算符都会给出类似的错误。racket
我正在努力研究将任意数量的列表压缩在一起的函数的语法。我目前有:
(define (zip . [lsts : (Listof Any) *])
(apply map (inst list Any) lsts))
评估时会导致以下错误:
错误:struct:exn:fail:syntax /Applications/Racket v6.6/collects/racket/private/kw.rkt:929:25:类型检查器:“apply”中函数的参数错误:
域:(-> ab ... bc) (a 列表) (b 列表) ... b
(-> ac) (a 对 (a 列表))
参数: (-> Any * (Listof Any)) (Listof (Listof Any)) *
in: (#%app apply map (#%表达式列表) lsts)
因为这些评价还不错:
(apply map (inst list Any) '(("asd" 1 2) ("cat" 3 4)))
;;(("asd" "cat") (1 3) (2 4))
(define (test . [lsts : (Listof Any) *])
lsts)
(test '(1 …
我无法理解方案中似乎不一致的破坏性操作。这就是为什么 bar 在下面的例子中没有改变
(define foo '(a b))
(define bar foo)
(set! foo '(c d))
foo
>(c d)
bar
>(a b)
Run Code Online (Sandbox Code Playgroud)
然而,这使得 bar 通过改变 foo 而改变
(define foo '(a b))
(define bar foo)
(set-car! foo 'c)
foo
>(c b)
bar
>(c b)
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,在两个例子中 bar 都指向其汽车中的 foo,但不知怎的,旧的 foo 在使用 set 时并没有改变!在第一个中。我无法在任何地方找到关于这种情况如何以及为何发生的答案。
如何将字符串转换为可在 Racket 提示符中计算的"(+ ( - 5 2) 8)"s 表达式?(+ (- 5 2) 8)我尝试过string->symbol,但它返回了'|(+ ( - 5 2) 8)|,这不是我想要的。
考虑SICP中的这两段:
\n\n\n\n\n这种构造称为案例分析,Lisp 中有一种特殊的形式\n 来表示这种案例分析。它称为 cond(\n 代表 \xe2\x80\x9cconditional\xe2\x80\x9d),其用法如下:
\n\n...
\n\n这使用特殊形式 if,这是一种受限制的条件类型,\n 当案例分析中恰好有两种情况时可以使用该形式。
\n
在这种情况下类型是什么意思(条件的限制类型)?是不是意味着:
\n\nscheme ×10
racket ×6
lisp ×3
sicp ×2
io ×1
macros ×1
meep ×1
r5rs ×1
read-write ×1
typed-racket ×1