Help understanding Scheme's syntax, procedures and calls

Peter Otten __peter__ at web.de
Thu Aug 12 06:06:06 EDT 2004


Fran wrote:

> 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)


[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.




More information about the Python-list mailing list