我在这里开始了一个关于我正在开发的刽子手游戏的问题。
我觉得刽子手部分让人们对我的真实问题和问题感到困惑。我的问题是,我从递归循环中调用了各种定义的函数并得到了不正确的值。当我自己调用这些相同的函数时(不是在递归循环中),它们会按预期工作。我知道这要么是我忽略的东西,要么是我需要解决方法的变量绑定问题。
首先在这里重现问题代码:
(define (recurse a_list a_number)
(cond ((= a_number 0)
(display "Zero Condition.")
)
(else
(display "\n\n")
(display-list a_list ",")
(display "\n")
(display (car a_constant))
(display "\n")
(display "If the above letter is in the list, result should be true\n")
(display (contains? a_list (car a_constant)))
(display "\n")
(display "Enter a letter:")
(recurse (cons (symbol->string (read)) a_list) (- a_number 1))
)
)
)
Run Code Online (Sandbox Code Playgroud)
以下是我在递归循环中使用的定义:
(define (display-list a_list separater)
(if (null? a_list)
(display "")
(begin
(display (car a_list))
(if (null? …Run Code Online (Sandbox Code Playgroud) 我正在使用 DrRacket 在方案中实现我的代码。我是这门语言的新手,我的作业需要一些帮助。我的老师想让我从控制台调用一个函数,她不希望我用它的名字来调用它。
这是不允许的 => (function-name 'John (X pcf F g K :: 1 4))
而不是使用该符号,我必须不带名称调用我的函数。
这就是她想要的 => ('John (X pcf F g K :: 1 4))
有没有办法定义一个没有名字的函数并调用它?我在互联网上搜索了很多,但找不到解决方案。
I want to implement cond (using lisp macros in guile) with if, this my attempt:
(define-macro (cond . clauses)
(if (pair? clauses)
(let ((first (car clauses)) (rest (cdr clauses)))
`(if ,(car first)
(begin
,@(cdr first))
,(if (equal? (caar rest) 'else)
',(cadr rest)
`(cond ,rest))))))
Run Code Online (Sandbox Code Playgroud)
but it don't work, when I call it with this code:
(cond ((= 1 0) (display "hello"))
((= 1 1) (display "world"))
(else
(display "foo")))
Run Code Online (Sandbox Code Playgroud)
I got this error:
ERROR: In procedure car: Wrong type argument …Run Code Online (Sandbox Code Playgroud) Racket 是我正在学习的第一种方案方言,而且我还没有那么远,但是由于方案的语法最少,我相信可以安全地假设解释器对变量名中的问号的处理与任何不同其他可行的字符。
有了这个句子,为什么方案使用符号“?” 表示返回真或假的函数(称为谓词)?例如,在球拍中,有一个名为number?. number?应用于任何数字(1、5、-5、2.7 等)时返回 true,否则返回 false。我相信这number?是类似于is_the_following_argument_a_number?. 假设这是真的,表达式(number? 5)将转换为(is_the_following_argument_a_number? 5)。
在英语(此变量的编写语言)中,谓词“以下参数是数字吗?” 可以通过先移动动词:“the following argument is a number”,然后提取谓词:“is a number”,将问题翻译成陈述句形式找到。现在,我不像我在编程语言那样擅长语言,但我相信这是正确的。另外,对不起,如果这变成了一个英语问题而不是一个计划问题。
我难以理解的是,如果 lisp 社区调用number?谓词,为什么变量名不是英语谓词(我说变量名不是英语谓词,不是它的函数类型) in scheme 不是谓词)我发现我认为我认为number?翻译成的谓词是“是一个数字”,而不是整个问题“下面的参数是一个数字吗?”,只是谓词。那么,为什么 lisp 社区选择将方案中的谓词命名为英语中的问题?我认为这是因为社区错误地将陈述的价值(真或假)作为是/否问题的答案(是或否(显然))。我这样想有错吗?
我想要做的是定义一个列表,例如 (define lst '(1 2 3 4 5 6)) 然后调用 (split lst) 它将返回 '((1 3 5) (2 4 6))。
一些例子:
lst是'(1 2 3 4 5 6),它应该返回'((1 3 5) (2 4 6))lst是'(1 2 3 4 5 6 7),它应该返回'((1 3 5 7) (2 4 6))lst是'("a" "little" "bit" "of" "that" "to" "spice" "things" "up"),它应该返回'(("a" "bit" "that" "spice" "up") ("little" "of" "to" "things"))它应该在构建两个列表时交替。所以第一个索引应该在第一个列表中,第二个索引在第二个列表中,第三个索引在第一个列表中,依此类推。
这是我当前的脚本。
(define (split …Run Code Online (Sandbox Code Playgroud) Scheme 和 Racket 不保护特殊形式,因此可以重新定义它们。纯粹出于好奇,有没有办法在被覆盖后取回特殊形式?
示例情况:
$ racket
Welcome to Racket v6.11.
> (define define 1)
> (+ define define)
2
> (define x 3) ; How to get the original 'define' back?
; x: undefined;
; cannot reference undefined identifier
; [,bt for context]
>
Run Code Online (Sandbox Code Playgroud)
如果我重新定义一个特殊形式,有没有办法找回特殊形式?
有人可以解释以下表达式吗
> (+)
0
> (+ 1)
1
> (- 1)
-1
> (/ 1)
1
> (/ 2)
1/2
> (/ 3)
1/3
Run Code Online (Sandbox Code Playgroud)
如果默认参数为 1,为什么(+ 1)return 1while (/ 2)return 1/2?
不应该(+ 1)回来2吗?
我有以下代码:
(define numbers '(2 3 5 3 1 22 2))
(define (count val l)
(if (null? l)
0
(+
(if (= (first l) val) 1 0)
(count val (rest l))
)
)
)
(display (count 6 numbers))
Run Code Online (Sandbox Code Playgroud)
(对不起,如果我的代码看起来很糟糕,只需要使用这种语言一次)
编译器说:
count: contract violation
expected: procedure?
given: 6
argument position: 1st
other arguments...:
'(3 5 3 1 22 2)
Run Code Online (Sandbox Code Playgroud) 我正在运行以下代码:
(define (myadd x y)
(+ x y)
(display (+ x y))
)
(define (mymul x y)
(* x y)
(display (* x y))
)
(apply myadd '(3 (apply mymul '(3 4)))
Run Code Online (Sandbox Code Playgroud)
我试图在运行时得到答案 12,(apply myadd '(3 (apply mymul '(3 4)))但出现以下错误:
错误:+: number required, but got (apply mymul (quote (3 4))) [apply, (anon), +]
不是在 Lisp 中创建新列表list的关键字,但在 Lisp 中可以调用一个参数list。我认为大多数编程语言(如 Java 或 C++)中的关键字不能用于参数 名称,Lisp 中是否有特殊原因可以使用?