从程序形式转换为让形式 [英] Convert from procedure form to let form

查看:38
本文介绍了从程序形式转换为让形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有我在scheme中编写的这个程序表单代码,我需要将其更改为let表单.这是程序表单代码:

have this procedure form code that I have written in scheme and I need to change it into let form. Here is procedure form code:

(define PI 3.14159265)

(define areac (lambda (d)
                (* PI (/ d 2) (/ d 2))))

(define volumec (lambda (d h)
                  (*(areac d)(/ h 3))))

(define TotalVolume (lambda()
                      (+(volumec 1 1) (volumec 2 2) (volumec 3 3) (volumec 4 4) (volumec 5 5))))

(define main (lambda()
               (TotalVolume)))

(main)

这是我迄今为止为我的 let 表单代码实现的:

Here is what I have implemented so far for my let form code:

(define volumec
  (lambda(d h)
    (let
        ((PI 3.14159265))
      (let
          ((areac
            (lambda(d)
              (*PI(/ d 2)(/ d 2))
              (*
               (/ h 3)))))))))

(define TotalVolume (lambda()
                      (+(volumec 1 1) (volumec 2 2) (volumec 3 3) (volumec 4 4) (volumec 5 5))))

(define main (lambda()
               (TotalVolume)))

(main)

我的 let 表单代码当前出现的错误是:

The current error I am getting with my let form code is:

r5rs: body: (r5rs:body)

预先感谢您对此问题的任何帮助:)

Thanks, in advance, for any help with this issue :)

推荐答案

您可以使用 let* 代替.它允许您在其他绑定中使用先前定义的绑定.例如:

You can use a let* instead. It allows you to use previously defined bindings in other bindings. For example:

(let* ((x 1)
       (y (+ x 1)))
  y)

使用常规让这行不通,因为每个绑定都忽略了其他绑定.这是因为 let 的实现方式.从语义上讲,上面的 let 可以翻译成这样:

Using a regular let this would not work because each binding is oblivious of the other bindings. This is because of the way the let is implemented. Semantically speaking the let above could be translated to something like this:

(let ((x 1)
      (y 2))
  (+ x y))

;; Is equivalent to:

((lambda (x y) (+ x y)) 1 2)

虽然 let* 会被翻译成这样:

While a let* would be translated to this:

(let* ((x 1)
      (y 2))
  (+ x y))

;; Is equivalent to:

((lambda (x) ((lambda (y) (+ x y)) 2)) 1)

我建议你通读这个页面以了解letlet*letrec.

I suggest you read through this page to understand the difference between let, let* and letrec.

在您的解决方案中使用 let* 会产生以下代码:

Using let* in your solution yields the following code:

; (define PI 3.14159265)
; 
; (define areac (lambda (d)
;                 (* PI (/ d 2) (/ d 2))))
; 
; (define volumec (lambda (d h)
;                   (*(areac d)(/ h 3))))
; 
; (define TotalVolume (lambda()
;                       (+(volumec 1 1) (volumec 2 2) (volumec 3 3) (volumec 4 4) (volumec 5 5))))
; 
; (define main (lambda()
;                (TotalVolume)))

;; Is equivalent to:

(define main (let* ((PI 3.14159265)
                    (areac (lambda (d)
                             (* PI (/ d 2) (/ d 2))))
                    (volumec (lambda (d h)
                               (*(areac d)(/ h 3))))
                    (TotalVolume (lambda()
                                   (+(volumec 1 1) (volumec 2 2) (volumec 3 3) (volumec 4 4) (volumec 5 5)))))
               (lambda()
                 (TotalVolume))))
(main)

但是,如果不允许使用 let*,则必须嵌套 let 语句.这将产生非常丑陋但功能强大的代码:

However, if you are not allowed to use let* you will have to nest your let statements. This will yield quite ugly, but functional code:

;; Is equivalent to:

(define main (let ((PI 3.14159265))
               (let ((areac (lambda (d)
                              (* PI (/ d 2) (/ d 2)))))
                 (let ((volumec (lambda (d h)
                                  (*(areac d)(/ h 3)))))
                   (let ((TotalVolume (lambda()
                                        (+(volumec 1 1) (volumec 2 2) (volumec 3 3) (volumec 4 4) (volumec 5 5)))))
                   (lambda()
                     (TotalVolume)))))))
(main)

no expression in body 错误表明您定义了一个 let 表达式,但在正文中没有任何表达式.这种 let 表达式的一个例子可能是:

The no expression in body error indicates that you have defined a let expression with no expressions in the body. An example of such a let expression could be:

(let ((x =1))
  ...)

我输入点的地方必须至少是一个表达式,它将是整个 let 表达式的计算结果.如果那里没有表达式,则不能将 let 评估为任何值,因为没有任何东西可以评估.在定义没有主体的 lambda 或没有主体的函数时,也可能出现此错误.

Where I typed the dots has to be at least one expression which will be the result of the evaluation of the entire let expression. If there is no expression there the let can not be evaluated to any value, because there is nothing to evaluate. You can also get this error when defining a lambda that has no body, or a function that has no body.

> (define (f x))
r5rs:body: no expression in body in: (r5rs:body)
> (lambda (x))
r5rs:body: no expression in body in: (r5rs:body)
> (let ((x 1)))
r5rs:body: no expression in body in: (r5rs:body)
> 

这篇关于从程序形式转换为让形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆