Help understanding Scheme's syntax, procedures and calls

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Aug 13 06:51:13 EDT 2004


franbarlow at mail.com (Fran) writes:

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

For the record these three functions should have been written.

(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 n 1))

Which would have been obvious if I'd bothered to read the detailed stuff later
instead of assuming.  Peter's translation is correct.  It's basically testing
reasoning about recursive functions.  You need to know that, unlike Python,
every expression returns a value and every '(...)' is an expression to be
evaluated.  So the 'if' statement returns the result of whichever of the arms
it takes, which in turn becomes the result of the function.

Eddie



More information about the Python-list mailing list