【问题标题】:Let-Form Bind Values In SchemeLet-Form 在 Scheme 中绑定值
【发布时间】:2019-11-10 21:42:01
【问题描述】:

我应该重新实现以前实现的称为“产品”的过程。我将使用 let-form 绑定值并使用(ProductLet 60 40) 对其进行测试。我的代码不能正常工作,我不知道为什么。

(define (ProductLet a b)
(let ((p a)(q b))
    p q
    (* p q))
  )                  
(ProductLet 60 40)

【问题讨论】:

  • 你期望它产生什么?

标签: scheme racket


【解决方案1】:

您的代码看起来不错 - 好吧,我将删除 pq 正文中的前两行 let,它们实际上在那里什么都不做:

(define (ProductLet a b)
  (let ((p a) (q b))
    (* p q)))

(ProductLet 60 40)
=> 2400

注意let 的主体就像一个过程的主体:所有的表达式都会被执行,但只有最后一个表达式的值会被返回。例如,如果你想显示需要显式打印的变量,因为在执行过程时它们不会被返回:

(define (ProductLet a b)
  (let ((p a) (q b))
    (displayln p)
    (displayln q)
    (* p q)))

(ProductLet 60 40)
60
40
2400

【讨论】:

    猜你喜欢
    • 2015-06-19
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多