当我学习计划和球拍时,我发现自己一次又一次地重复这种模式.我有一个递归函数,其中函数的一些参数改变但一些参数没有.我构建了一个外部函数,它接受所有参数并在其中定义一个内部函数,该函数仅接受更改的参数并重复该参数.
作为一个具体的例子,一个案例基于"小计划者"的功能练习
;inserts an item to the right of an element in a list
(define (insert-to-right new old lat)
(define (insert-to-right lat)
(cond
[(null? lat) lat]
[(eq? old (car lat) ) (cons old (cons new (cdr lat)))]
[else (cons (car lat) (insert-to-right (cdr lat)))]))
(insert-to-right lat))
Run Code Online (Sandbox Code Playgroud)
是否可以构建一个宏定义*和一个运算符(例如一个垂直条),以便我输入:
(define* (insert-to-right new old | lat)
(cond
[(null? lat) lat]
[(eq? old (car lat) ) (cons old (cons new (cdr lat)))]
[else (cons (car lat) (insert-to-right (cdr lat)))]))
Run Code Online (Sandbox Code Playgroud)
然后,这会扩展为第一种形式,所有参数都传递给外部函数,但只有垂直条传递给内部循环后的参数.
我需要找到列表的最大值和最小值,然后添加一个新列表.到目前为止这是我的代码:
(define alist '(18 39 57 -4 0)
(define (nMax alist)
(if (null? (cdr alist))
(car alist)
(if (> (car alist) (nMax (cdr alist)))
(car alist)
(nMax (cdr alist)))))
(define (nMin alist)
(if (null? (cdr alist))
(car alist)
(if (< (car alist) (nMin (cdr alist)))
(car alist)
(nMin (cdr alist)))))
Run Code Online (Sandbox Code Playgroud)
现在我被卡住了.我怎样才能在新列表中添加nMin
+ nMax
?我的输出应该是这样的:
'(57 -4)
Run Code Online (Sandbox Code Playgroud) 用原始表达方式,我的意思是+ - * / sqrt
,除非有其他我错过了.我想知道如何编写一个Scheme表达式,只使用这些函数找到第6个根.
我知道我可以找到平方根的立方根,但立方根似乎不是原始表达式.
像这样:
> (my-append (list 1 2) 3)
'(1 2 3)
Run Code Online (Sandbox Code Playgroud)
我知道,append
在racket
实际上是连接两个列表.而cons
只是一个元素添加到了头一个列表,而不是尾巴
有没有人有这个想法?
第4章,HtDP.
注意:我也在其他问题中看到了这一点.
是出于清晰的原因还是算法的原因,我不知道,基本情况返回空而不是列表本身是空的.
例:
; List-of-numbers -> List-of-numbers
; compute the weekly wages for all given weekly hours
(define (wage* alon)
(cond
[(empty? alon) empty] ;<---- see here
[else (cons (wage (first alon)) (wage* (rest alon)))]))
; Number -> Number
; compute the wage for h hours of work
(define (wage h)
(* 12 h))
Run Code Online (Sandbox Code Playgroud)
我认为这是正确的.
; List-of-numbers -> List-of-numbers
; compute the weekly wages for all given weekly hours
(define (wage* alon)
(cond
[(empty? alon) alon] ;<---- see here …
Run Code Online (Sandbox Code Playgroud) 以下两个代码块都应该(在我看来)是无限循环
这有效
(define call/cc call-with-current-continuation)
(define l 0)
(define i 0)
((lambda ()
(call/cc
(lambda (k)
(set! l k)))
(write i)
(newline)
(set! i (+ i 1))
(l "ignore")))
Run Code Online (Sandbox Code Playgroud)
这不起作用:
(define call/cc call-with-current-continuation)
(define l 0)
(define i 0)
(begin
(call/cc
(lambda (k)
(set! l k)))
(write i)
(newline)
(set! i (+ i 1))
(l "ignore"))
Run Code Online (Sandbox Code Playgroud)
唯一的区别是一个使用lambda,一个使用begin块.为什么第二块代码不起作用?
谢谢
我正在尝试了解球拍的图案匹配文件,并且遇到一些类似以下的问题,我无法解析它。
http://docs.racket-lang.org/reference/match.html
例:
> (match '(1 2 3)
[`(,1 ,a ,(? odd? b)) (list a b)])
'(2 3)
Run Code Online (Sandbox Code Playgroud)
它没有解释此示例,“标识符与符号匹配”如何?我猜它与'(1 2 3)
模式匹配'(1, a, b)
并且b是奇数,但是为什么`(,1 ,a ,(? odd? b))
不呢`(1 a (? odd? b))
,它在列表成员之间需要逗号吗?特别是`(,
?为什么这样呢?真弦!
谢谢!
我正在尝试在Racket中实现一些SICP图形程序,但有两个问题:
当我需要使用'let'时,我不能使用初学者语言.当我尝试更改语言,或使用"高级"语言打开新文件时,我收到此错误:
module: identifier already imported from a different source
Run Code Online (Sandbox Code Playgroud)
我尝试加载图像模块时出错(需要2htdp/image).
这是怎么回事?另外,有没有更好的方法来训练图像中的图像?
如何在DrRacket中将字符串列表转换为字符串?例如,如果我
'("44" "444")
将其转换为"44 444"
?
我尝试了string-join
,但它需要一个分隔符,如果我放一个它用分隔符替换空格,如果我使用""
分隔符,它只是摆脱它.
我需要编写一个软件,给定一个字符串列表,该软件返回一个由输入列表元素的逗号分隔串联而成的新颖字符串。
(comma-con (list "a" "b" "c")) ;=> "a,b,c"
Run Code Online (Sandbox Code Playgroud)
我很想编写Scheme惯用代码。
到目前为止,我写道:
(define (comma-con l)
(foldr
(? (x y) (if (string? y) (string-append x "," y) x))
'()
l))
Run Code Online (Sandbox Code Playgroud)
可以,但是似乎有点脏。有没有一种更好的方法来编写它,例如避免使用“ if”?