帮助理解Scheme的语法,过程和调用 [英] Help understanding Scheme's syntax, procedures and calls

查看:86
本文介绍了帮助理解Scheme的语法,过程和调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解一个函数式语言代码片段,所以我可以用b $ b来解释它的语法和工作原理到我的非英语背景

neighbor,who is做她的决赛。


这个代码片段的意思是天堂的名字是什么? (英文

散文,如果可能的话)。


它看起来像一个名为scheme的语言片段


(定义(此n)

(if(= n 0)

0

(= n(此( - n 1) )))))

(定义(f1 ab)

(如果> ba)

0

(+ b(f1 a(+ b 1)))))

(定义(即n)

(f1 n1)


a)描述在评估

表达式时发生的处理(这4)

b)解释为什么表达式(=(this n)(即n)当n是一个正整数时,总是求值为

true。

c)用上面的语言写一段代码,加起来所有的

给定范围内的整数,不包括指定的两个数字

。例如,如果指定的范围是4-9,则代码

应该添加5à8。


建议的答案:


a)


这4个电话开始

因为n-1 = 3递归这3个电话已经开始

As n-1 = 2 a这2个电话开始

当n-1 = 1 a这1个电话开始

当n-1 = 0时这个0呼叫开始了返回为n = 0

这1个电话通过添加1 + 0来解决

这2个电话通过添加2 + 1来解决

这3个电话通过添加3 + 3

来解决。当4个电话通过添加4 + 6解决时,最终返回10个。


我不再掌握建议答案的模式比问题,

并且更不能向任何人解释它。


b)


This和That函数具有相同的输出,并且

此外,如果n <0,则两个函数都会导致无限递归。当n $ / b $ b是正整数时,This函数计算

(n +?(3+(2+(1+(0))))并且该函数计算

(1+(2 +(3 +?(n =(0))))。两者总是会产生相同的答案。

列表(= ab )当a = b时,只评估为真,因为a等于b

列表总是在n> 0时评估为真。


也许这个答案将会当我理解代码时更有意义

片段。


c)


解决方案1(没有现有功能)


(定义(内部范围ab)

(if(> =(+ a 1)b)

0

(+(= a 1)(内部范围(+ a 1)b))))

解决方案2使用现有函数。并假设一个< ; b


(定义(内部范围2 ab)

( - (此b)(本a)b))

解决方案3使用现有功能并处理> b情况

(定义(内部范围3 ab)

(如果(< ab)

( - (b)(t他的a)b)

( - (这个)(这个b)a)))

0的作用是什么?解决方案1中的字符和最初的

片段?括号后面的语法规则是什么?


他们注意到代码是由Dr Scheme测试的。在
www.plt-scheme.org



谢谢

I''m trying to understand a functional language code fragment so I can
explain its syntax and workings to my non English-speaking background
neighbour, who is doing her finals.

What in heaven''s name is this code fragment intending? (In English
prose if possible).

It looks like a fragment from a language called "scheme"

(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))
(define (that n)
(f1 n1)

a) Describe the processing that occurs during the evaluation of the
expression (this 4)
b) Explain why the expression (=(this n)(that n) always evaluates to
true when n is a positive integer.
c) Write a fragment of code in the above language that adds up all the
integers within a given range, not including the two numbers
specified. For example, if the specified range was 4 à 9 then code
should add 5à 8.

Suggested answers:

a)

This 4 call started
As n-1=3 a recursive This 3 call is started
As n-1=2 a This 2 call starts
As n-1=1 a This 1 call starts
As n-1=0 a This 0 call is started and is returned as n=0
This 1 call is resolved by adding 1+0
This 2 call is resolved by adding 2+1
This 3 call is resolved by adding 3+3
Finally 10 is returned when This 4 call is resolved by adding 4 + 6.

I no more grasp the pattern of the suggested answer than the question,
and am much less in a position to explain it to anyone.

b)

Both the This and the That functions have the same output, and
furthermore both functions result in infinite recursion if n<0. When n
is a positive integer, the This function calculates
(n+?(3+(2+(1+(0)))) and the that function calculates
(1+(2+(3+?(n=(0)))). Both will always result in the same answer. The
list (=a b) only evaluates to true when a=b, as a does equal b the
list always evaluates to true for n>0.

Perhaps this answer will make more sense when I understand the code
fragment.

c)

Solution 1 (without existing functions)

(define (internal-range a b)
(if(>=(+ a 1)b)
0
(+(= a 1)(internal-range(+ a 1)b))))

Solution 2 using existing functions. And assuming a<b

(define (internal-range2 a b)
(-(this b) (this a)b))

Solution 3 using existing functions and dealing with a>b case
(define (internal-range3 a b)
(if (< a b)
(-(this b) (this a)b)
(-(this a) (this b)a)))

What is the role of the "0" character in solution 1 and the initial
fragment? What is the syntax rule being followed by the parentheses?

They note that the code was tested by "Dr Scheme" at
www.plt-scheme.org


Thanks

推荐答案

Fran写道:
Fran wrote:
我是'我试图理解一个功能语言代码片段,这样我就可以向我的非英语背景的邻居解释它的语法和工作原理。谁正在做她的决赛。

什么在天堂的名字是这个代码片段的意图吗? (英文
散文,如果可能的话)。

它看起来像一个名为scheme的语言片段

(define(this n)
>(if(= n 0)
0
(= n(this( - n 1)))))
(定义(f1 ab)
(如果> ba )
0
(+ b(f1 a(+ b 1)))))
(定义(即n)
(f1 n1)
I''m trying to understand a functional language code fragment so I can
explain its syntax and workings to my non English-speaking background
neighbour, who is doing her finals.

What in heaven''s name is this code fragment intending? (In English
prose if possible).

It looks like a fragment from a language called "scheme"

(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))
(define (that n)
(f1 n1)


[愚蠢的免责声明]我不知道Scheme但我认为以上是

相当于下面的python(你似乎有一些问题,计算

方括号:-)


def this(n):

如果n == 0:

返回0

否则:

返回n +此(n-1)


def f1(a,b):

如果b> a:

返回0

否则:

返回b + f1(a,b + 1)


def(n):

返回f1(n,1)


#试试吧out

打印此(4)

打印''实验'证明"''

ALL_POSITIVE_INTEGERS =范围(1 ,10)

for ALL in ALL_POSITIVE_INTEGERS:

print"%s:" %i,这(i),(i)


否则我建议您下载Scheme并试试吧。也许

他们甚至有一个调试器,所以你可以在

时间单步执行指令...


Peter


PS:更多帮助肯定会导致取消资格。


[silly disclaimer] I don''t know Scheme but I''d suppose the above to be
equivalent to the following python (you seem to have some problems counting
the brackets :-)

def this(n):
if n == 0:
return 0
else:
return n + this(n-1)

def f1(a, b):
if b > a:
return 0
else:
return b + f1(a, b+1)

def that(n):
return f1(n, 1)

# try it out
print this(4)
print ''experimental "proof"''
ALL_POSITIVE_INTEGERS = range(1, 10)
for i in ALL_POSITIVE_INTEGERS:
print "%s:" % i, this(i), that(i)

Otherwise I''d suggest that you download Scheme and just try it out. Perhaps
they even have a debugger so you can step through the instructions one at a
time...

Peter

PS: More help would certainly lead to disqualification.


fr ******** @ mail.com (Fran)写道:
fr********@mail.com (Fran) writes:
我是试图理解一个功能语言代码片段,这样我就可以向我的非英语背景的邻居解释它的语法和工作原理。她正在做她的决赛。
这个代码片段的意思是天堂的名字是什么? (英文
散文,如果可能的话)。
它看起来像一个叫做scheme的语言的片段。
(定义(此n)
(if(= n 0)
0
(= n(this( - n 1)))))


如果给出这几乎肯定是错误的。第一个问题可能只是一个

转录错误,因为(= n 0)应该是(= n 0)。第二个

是最后一行没有意义。看起来有人对于if语句如何工作感到困惑。由于这看起来很可疑,因为

作业我只是给了一个提示。如果语句的作用类似于

(如果expr1

(如果exp1为真则返回expr)

(如果exp1为假则返回expr))

因为每个arm都是一个表达式来评估它意味着你在最后一行评估''=''作为一个

函数因此它返回一个布尔值,这将是因为

你在一段时间后感到悲伤。

(定义(f1 ab)
(如果> ba)
0
( + b(f1 a(+ b 1)))))
I''m trying to understand a functional language code fragment so I can
explain its syntax and workings to my non English-speaking background
neighbour, who is doing her finals. What in heaven''s name is this code fragment intending? (In English
prose if possible). It looks like a fragment from a language called "scheme" (define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
As given this is almost certainly wrong. The first problem is possibly just a
transcription error in that (=n 0) should probably be (= n 0). The second one
is the that the last line doesn''t make sense. It looks like someone is
confused about how if statements work. Since this looks suspiciously like
homework I''m only giving a hint. If statements work like
(if expr1
(expr to return if exp1 is true)
(expr to return if exp1 is false))
Since each arm is an expression to evaluate it means you evaluate ''='' as a
function in the last line hence it returns a boolean, which is going to cause
you grief after a short while.
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))




这在语法上是错误的(提示:if语句的第一个表达式)


问题在你修复函数之前是没有意义的。


偶然有一个comp.lang.scheme。

Eddie



This is wrong syntactically (hint: the first expression for the if statement)

The questions wouldn''t make sense until you fixed the functions.

There is a comp.lang.scheme incidentally.

Eddie


不幸的是它看起来更像是''破解计划''。


Fran写道:
Unfortunately it looks more like ''broken scheme''.

Fran wrote:
(define(this n)
(if(= n 0)
0
(= n(this( - n 1)))))
<无线电通信/>
看起来不错,但是:

(定义(f1 ab)
(如果> ba)
0
(+ b(f1) a(+ b 1)))))


有6(s和7)s。我希望秒行应该是

(if(> ba)

(定义(那个)
(f1 n1)
(define (this n)
(if (=n 0)
0
(= n (this (- n 1)))))
That looks fine, however:
(define (f1 a b)
(if >b a)
0
(+ b (f1 a (+ b 1)))))
Has 6 (s and 7 )s. I expect that the seconds line should read
(if (> b a)
(define (that n)
(f1 n1)




再次在(和)中存在不平衡,我认为第二行应该是

(f1 n 1)),注意那之间的空格' 'n''和''1''。


这个人的作业是否有机会?



Again there is an imbalance in the ( and ), I think the second line should read
(f1 n 1)), note the space between then ''n'' and the ''1''.

Is this someone''s homework by any chance?


这篇关于帮助理解Scheme的语法,过程和调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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