【问题标题】:How do you increment with biwascheme?你如何增加 biwascheme?
【发布时间】:2014-05-29 18:14:54
【问题描述】:

有人用过在线ide repli吗? http://repl.it/languages/Scheme

你如何增加它?

我正在尝试封装一个函数来根据 sicp video 2a 进行总结。

(define (square a ) 
    (* a a))


(define (sum term a next b)
    (if (> a b)
        0
        (+ (term a) 
            (sum term (next a ) next b))))

(define (sum-int a b)
    (define (identity a) a)
    (sum identity a (+ 1 a) b))


(define (sum-square a b)
    (sum square a (+1 ) b))

    (sum-square 1 2)

替代的平方和(不工作)

(define (sum-square a b)
    (sum square a (+ 1  a) b))

    (sum-square 1 2)  // getting 2 not a function.

工作代码:

(define (sum-int a b)
    (define (identity a) a)
    (sum identity a (lambda(a)(+ 1 a)) b))

【问题讨论】:

  • 我还没有完成学习方案,但是如果你想返回一个函数,你不需要lambda吗?
  • 哦,好吧!这行得通!

标签: scheme sicp


【解决方案1】:

你必须传递一个函数作为next参数,像这样:

(define (identity n) n)

(define (sum-int a b)
  (sum identity a add1 b))

(define (sum-square a b)
  (sum square a add1 b))

如果没有定义add1,你可以编写自己的版本:

(define (add1 n)
    (+ 1 n))

或者,您可以直接传递lambda

(define (sum-int a b)
  (sum identity a (lambda (n) (+ 1 n)) b))

(define (sum-square a b)
  (sum square a (lambda (n) (+ 1 n)) b))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2021-09-27
    • 2020-03-24
    • 2021-07-19
    • 2019-06-25
    • 2012-09-05
    • 2021-09-25
    相关资源
    最近更新 更多