将函数转换为 Dracket 中的列表
Transforming a function into a list in Drracket
我创建了一个创建列表的函数。我想在另一个函数中使用该列表,我该怎么做?
(define (myfunc L n)
(if (= n 0)
empty
(cons (list-ref L (random 26))
(myfunc L (- n 1)))))
我想将此函数作为一个列表进行赋值,以便在另一个函数中使用它。
从您的定义开始:
(define (myfunc L n)
(if (= n 0)
empty
(cons (list-ref L (random 26))
(myfunc L (- n 1)))))
采用函数的函数
因为 Schemes 将函数视为第一个 class 值,所以 myfunc
可以作为函数传递给另一个函数。我们可以编写第二个函数,它接受一个函数作为参数:
(define (another-function a-function L n)
(print "another-function: ")
(a-function L n)) ; call a-function just like a built in function
我们可以将 myfunc
传递给 another-function
。然后 my-func
将在 another-function
:
内被调用
racket> (another-function myfunc (range 40) 4)
"another-function: "'(0 9 13 2)
这显示了函数是如何作为参数传递的。重要的想法是 Scheme 将函数作为函数而不是列表传递。 Scheme 将函数作为值传递,而不是作为将被评估为值的源代码。
函数返回函数
为了让大家明白函数就是值,我们来看看 return 起作用的函数。这是一个 return 类似于 myfunc
的函数,除了我们可以使用不同的值而不是 26
:
(define (make-myfunc r)
(define (inner-myfunc L n) ; define a new function like myfunc
(if (= n 0)
empty
(cons (list-ref L (random r)) ; use r instead of 26
(inner-myfunc L (- n 1)))))
inner-myfunc) ; return the new function
我们可以这样使用:
racket> (define myfunc4 (make-myfunc 4))
racket> (myfunc4 (range 40) 4)
'(2 0 3 0)
采用函数和 return 函数的函数
这是一个接受一个函数和 return 另一个不同函数的函数:
(define (make-another-function a-function)
;; because the function we are returning does not call
;; itself recursively, we can use lambda instead of define.
(lambda (L n)
(print "made by make-another-function: ")
(a-function L n)))
这里正在使用:
racket> (define yet-another-function (make-another-function myfunc))
racket> (yet-another-function (range 40) 3)
"made by make-another-function: "'(1 18 16)
我创建了一个创建列表的函数。我想在另一个函数中使用该列表,我该怎么做?
(define (myfunc L n)
(if (= n 0)
empty
(cons (list-ref L (random 26))
(myfunc L (- n 1)))))
我想将此函数作为一个列表进行赋值,以便在另一个函数中使用它。
从您的定义开始:
(define (myfunc L n)
(if (= n 0)
empty
(cons (list-ref L (random 26))
(myfunc L (- n 1)))))
采用函数的函数
因为 Schemes 将函数视为第一个 class 值,所以 myfunc
可以作为函数传递给另一个函数。我们可以编写第二个函数,它接受一个函数作为参数:
(define (another-function a-function L n)
(print "another-function: ")
(a-function L n)) ; call a-function just like a built in function
我们可以将 myfunc
传递给 another-function
。然后 my-func
将在 another-function
:
racket> (another-function myfunc (range 40) 4)
"another-function: "'(0 9 13 2)
这显示了函数是如何作为参数传递的。重要的想法是 Scheme 将函数作为函数而不是列表传递。 Scheme 将函数作为值传递,而不是作为将被评估为值的源代码。
函数返回函数
为了让大家明白函数就是值,我们来看看 return 起作用的函数。这是一个 return 类似于 myfunc
的函数,除了我们可以使用不同的值而不是 26
:
(define (make-myfunc r)
(define (inner-myfunc L n) ; define a new function like myfunc
(if (= n 0)
empty
(cons (list-ref L (random r)) ; use r instead of 26
(inner-myfunc L (- n 1)))))
inner-myfunc) ; return the new function
我们可以这样使用:
racket> (define myfunc4 (make-myfunc 4))
racket> (myfunc4 (range 40) 4)
'(2 0 3 0)
采用函数和 return 函数的函数
这是一个接受一个函数和 return 另一个不同函数的函数:
(define (make-another-function a-function)
;; because the function we are returning does not call
;; itself recursively, we can use lambda instead of define.
(lambda (L n)
(print "made by make-another-function: ")
(a-function L n)))
这里正在使用:
racket> (define yet-another-function (make-another-function myfunc))
racket> (yet-another-function (range 40) 3)
"made by make-another-function: "'(1 18 16)