From: Fran
Subject: Help understanding Scheme's syntax, procedures and calls
Date: 
Message-ID: <95f168b0.0408120118.79dbdafc@posting.google.com>
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

From: Jens Axel Søgaard
Subject: Re: Help understanding Scheme's syntax, procedures and calls
Date: 
Message-ID: <411b3e98$0$241$edfadb0f@dread11.news.tele.dk>
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)))))

You probably mean:

  (define (this n)
  	(if (= n 0)
  	    0
  	    (= n (this (- n 1)))))

 > a)	Describe the processing that occurs during the evaluation of the
 > expression (this 4)

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

The way to understand this is to start DrScheme.
Enter

  (define (this n)
  	(if (= n 0)
  	    0
  	    (= n (this (- n 1)))))

  (this 4)

in the definition window the top one.

Then click at the foot (the stepper).
[If it doesn't work, choose one if the HTDP teaching
languages in the Language menu.]

You will now see (in green)

   (this 4)

and an arrow towards (in purple)

   (if (= 4 0)
     0
     (= 4 (this (- 4 1))))

Click at step, and notice that (= 4 0) is green
and false below is purple.

   (if false
       0
       (= 4 (this (- 4 1))))

Click at step.

(Don't worry if you miss a step, you can also go a step back)


       (= 4 (this (- 4 1)))

Click at step

       (= 4 (this 3))

Click at step.

       (= 4 (if (= 3 0)
  	    0
  	    (= 3 (this (- 3 1)))))

Continue until, you can predict ahead what is going to happen.

    The rule is: (this 3) becomes the
    body of the definition of this, where
    the parameter n is replaced with 3.

-- 
Jens Axel Søgaard
From: Fran
Subject: Re: Help understanding Scheme's syntax, procedures and calls
Date: 
Message-ID: <95f168b0.0408121220.535197aa@posting.google.com>
Jens Axel S�gaard <······@soegaard.net> wrote in message news:<·······················@dread11.news.tele.dk>...
> 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)))))
> 
> You probably mean:
> 
>   (define (this n)
>   	(if (= n 0)
>   	    0
>   	    (= n (this (- n 1)))))
> 
>  > a)	Describe the processing that occurs during the evaluation of the
>  > expression (this 4)
>  
>  > 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.
> 
> The way to understand this is to start DrScheme.
> Enter
> 
>   (define (this n)
>   	(if (= n 0)
>   	    0
>   	    (= n (this (- n 1)))))
> 
>   (this 4)
> 
> in the definition window the top one.
> 
> Then click at the foot (the stepper).
> [If it doesn't work, choose one if the HTDP teaching
> languages in the Language menu.]
> 
> You will now see (in green)
> 
>    (this 4)
> 
> and an arrow towards (in purple)
> 
>    (if (= 4 0)
>      0
>      (= 4 (this (- 4 1))))
> 
> Click at step, and notice that (= 4 0) is green
> and false below is purple.
> 
>    (if false
>        0
>        (= 4 (this (- 4 1))))
> 
> Click at step.
> 
> (Don't worry if you miss a step, you can also go a step back)
> 
> 
>        (= 4 (this (- 4 1)))
> 
> Click at step
> 
>        (= 4 (this 3))
> 
> Click at step.
> 
>        (= 4 (if (= 3 0)
>   	    0
>   	    (= 3 (this (- 3 1)))))
> 
> Continue until, you can predict ahead what is going to happen.
> 
>     The rule is: (this 3) becomes the
>     body of the definition of this, where
>     the parameter n is replaced with 3.


Thanks so much for your help. I'll follow your suggestion and give it a go.

FRAN