# Help understanding Scheme's syntax, procedures and calls

Fran franbarlow at mail.com
Thu Aug 12 11:20:12 CEST 2004

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

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

```