简体   繁体   English

在方案中使用局部变量

[英]Using local variables in scheme

I have been asked to translate a couple of C functions to scheme for an assignment. 有人要求我将几个C函数转换为分配方案。 My professor very briefly grazed over how Scheme works, and I am finding it difficult to understand. 我的教授非常简短地了解了Scheme的工作原理,我发现很难理解。 I want to create a function that checks to see which number is greater than the other, then keeps checking every time you input a new number. 我想创建一个函数来检查哪个数字大于另一个,然后在每次输入新数字时都进行检查。 The issue I am having is with variable declaration. 我遇到的问题是变量声明。 I don't understand how you assign a value to an id. 我不明白如何为ID分配值。

(define max 1)

(define (x x)
  (let maxfinder [(max max)]
    (if (= x 0)
        0
        (if (> max x) 
            max
            ((= max x) maxfinder(max))))))

The trouble I keep running into is that I want to initialize max as a constant, and modify x. 我一直遇到的麻烦是我想将max初始化为常量,然后修改x。 In my mind this is set up as an infinite loops with an exit when x = 0. If max is > x, which it should not be for the first time through, then set max = to x, and return x. 在我看来,这是在x = 0时设置为无穷循环并退出的。如果max> x(这不应该是第一次),则将max =设置为x,然后返回x。 I don't know what to do with the constant max. 我不知道如何处理常数max。 I need it to be a local variable. 我需要它是一个局部变量。 Thanks 谢谢

Parenthesis use is very strict. 括号的使用非常严格。 Besides special forms they are used to call procedures. 除了特殊形式,它们还用于调用过程。 eg (> max x) calls procedure > with arguments max and x . 例如(> max x)maxx参数调用过程> ((if (> x 3) - +) 6 x) is an example where the if form returns a procedure and the result is called. ((if (> x 3) - +) 6 x)是一个if形式返回过程并调用结果的示例。

  • ((= max x) ...) evaluates (= max x) and since the result is not a procedure it will fail. ((= max x) ...)求和(= max x) ,由于结果不是过程,它将失败。
  • maxfinder without parenthesis is just a procedure object. 没有括号的maxfinder只是一个过程对象。
  • (max) won't work since max is a number, not a procedure. (max)无效,因为max是一个数字,而不是一个过程。

As for you problem. 至于你的问题。 You add the extra variables you need to change in the named let. 您在命名的let中添加需要更改的额外变量。 Eg. 例如。 a procedure that takes a number n and makes a list with number 0-n. 一个采用数字n并列出数字0-n的过程。

(define (make-numbered-list n)
  (let loop ((n n) (acc '()))
    (if (zero? n)
        acc
        (loop (- n 1) (cons n acc)))))

Local variables are just locally bound symbols. 局部变量只是局部绑定的符号。 This can be rewritten 这可以改写

(define (make-numbered-list n)
  (define (loop n acc)
    (if (zero? n)
        acc
        (loop (- n 1) (cons n acc))))
  (loop n '()))

Unlike Algol dialects like C you don't mutate variables in a loop, but use recusion to alter them. 与像C这样的Algol方言不同,您不会在循环中对变量进行变异,而是使用重新排列来更改它们。

Good luck 祝好运

If i understand you correctly, you are looking for the equivalent of a C function's static variable. 如果我正确理解您的要求,那么您正在寻找的是C函数的静态变量的等效项。 This is called a closure in Scheme. 这在Scheme中称为闭包

Here's an example implementation of a function you feed numbers to, and which will always return the current maximum: 这是您向其输入数字的函数的示例实现,该函数将始终返回当前的最大值:

(define maxfinder
  (let ((max #f))                    ; "static" variable, initialized to False
    (lambda (n)                      ; the function that is defined
      (when (or (not max) (< max n)) ; if no max yet, or new value > max
        (set! max n))                ; then set max to new value
      max)))                         ; in any case, return the current max

then 然后

> (maxfinder 1)
1
> (maxfinder 10)
10
> (maxfinder 5)
10
> (maxfinder 2)
10
> (maxfinder 100)
100

So this will work, but provides no mechanism to reuse the function in a different context. 因此这将起作用,但没有提供在不同上下文中重用该功能的机制。 The following more generalised version instantiates a new function on every call: 以下更通用的版本在每次调用时实例化一个新函数

(define (maxfinder)
  (let ((max #f))                    ; "static" variable, initialized to False
    (lambda (n)                      ; the function that is returned
      (when (or (not max) (< max n)) ; if no max yet, or new value > max
        (set! max n))                ; then set max to new value
      max)))                         ; in any case, return the current max

use like this: 像这样使用:

> (define max1 (maxfinder)) ; instantiate a new maxfinder
> (max1 1)
1
> (max1 10)
10
> (max1 5)
10
> (max1 2)
10
> (max1 100)
100

> (define max2 (maxfinder)) ; instantiate a new maxfinder
> (max2 5)
5

Define a function to determine the maximum between two numbers: 定义一个函数以确定两个数字之间的最大值:

(define (max x y)
  (if (> x y) x y))

Define a function to 'end' 定义一个函数以“结束”

(define end? zero?)

Define a function to loop until end? 定义一个循环循环直到end?的函数end? computing max 计算max

(define (maximizing x)
  (let ((input (begin (display "number> ") (read))))
    (cond ((not (number? input)) (error "needed a number"))
          ((end? input) x)
          (else (maximizing (max x input))))))

Kick it off: 揭开序幕:

> (maximizing 0)
number> 4
number> 1
number> 7
number> 2
number> 0
7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM