Help understanding Scheme's syntax, procedures and calls

Eddie Corns eddie at holyrood.ed.ac.uk
Thu Aug 12 07:05:10 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)))))

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

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



More information about the Python-list mailing list