Help understanding Scheme's syntax, procedures and calls

Discussion in 'Python' started by Fran, Aug 12, 2004.

  1. Fran

    Fran Guest

    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
     
    Fran, Aug 12, 2004
    #1
    1. Advertising

  2. Fran

    Peter Otten Guest

    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.
     
    Peter Otten, Aug 12, 2004
    #2
    1. Advertising

  3. Fran

    Eddie Corns Guest

    (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
     
    Eddie Corns, Aug 12, 2004
    #3
  4. Unfortunately it looks more like 'broken scheme'.

    Fran wrote:
    > (define (this n)
    > (if (=n 0)
    > 0
    > (= n (this (- n 1)))))


    That looks fine, however:

    > (define (f1 a b)
    > (if >b a)
    > 0
    > (+ b (f1 a (+ b 1)))))


    Has 6 (s and 7 )s. I expect that the seconds line should read
    (if (> b a)

    > (define (that n)
    > (f1 n1)


    Again there is an imbalance in the ( and ), I think the second line should read
    (f1 n 1)), note the space between then 'n' and the '1'.

    Is this someone's homework by any chance?
     
    Peter Hickman, Aug 12, 2004
    #4
  5. On 2004-08-12, Peter Hickman <> wrote:

    > Is this someone's homework by any chance?


    According to the OP, it's part of a final exam.

    --
    Grant Edwards grante Yow! Yow! Those people
    at look exactly like Donnie
    visi.com and Marie Osmond!!
     
    Grant Edwards, Aug 12, 2004
    #5
  6. Grant Edwards wrote:
    > On 2004-08-12, Peter Hickman <> wrote:
    >
    >
    >>Is this someone's homework by any chance?

    >
    >
    > According to the OP, it's part of a final exam.
    >


    So they are completely shot then?
     
    Peter Hickman, Aug 12, 2004
    #6
  7. (Fran) wrote in message news:<>...
    <Scheme question>

    The right place where to ask this question is comp.lang.scheme.
    They are pretty gentle with newbies, so don't worry ;)

    Michele Simionato
     
    Michele Simionato, Aug 12, 2004
    #7
  8. Fran

    Fran Guest

    (Eddie Corns) wrote in message news:<cffit6$shd$>...
    > (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


    Thanks for the help. It's not homework but from an old exam paper, but
    the girl's English isn't absolutely fluent and I'm looking for a
    simple way to explain the expressions and functions.

    FRAN
     
    Fran, Aug 12, 2004
    #8
  9. Fran

    Fran Guest

    Peter Hickman <> wrote in message news:<411b577a$0$20523$>...
    > Unfortunately it looks more like 'broken scheme'.
    >
    > Fran wrote:
    > > (define (this n)
    > > (if (=n 0)
    > > 0
    > > (= n (this (- n 1)))))

    >
    > That looks fine, however:
    >
    > > (define (f1 a b)
    > > (if >b a)
    > > 0
    > > (+ b (f1 a (+ b 1)))))

    >
    > Has 6 (s and 7 )s. I expect that the seconds line should read
    > (if (> b a)
    >
    > > (define (that n)
    > > (f1 n1)

    >
    > Again there is an imbalance in the ( and ), I think the second line should read
    > (f1 n 1)), note the space between then 'n' and the '1'.
    >
    > Is this someone's homework by any chance?


    Thanks for the help. It's not homework but from an old exam paper, but
    the girl's English isn't absolutely fluent and I'm looking for a
    simple way to explain the expressions and functions.

    FRAN
     
    Fran, Aug 12, 2004
    #9
  10. Fran

    Fran Guest

    Grant Edwards <> wrote in message news:<411b7bdd$0$65612$>...
    > On 2004-08-12, Peter Hickman <> wrote:
    >
    > > Is this someone's homework by any chance?

    >
    > According to the OP, it's part of a final exam.


    It's not a final exam (that won't come until October-November) but
    from an old exam paper. The girl's English isn't absolutely fluent and
    I'm looking for a simple way to explain the expressions and functions.

    FRAN
     
    Fran, Aug 12, 2004
    #10
  11. Fran

    Eddie Corns Guest

    (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
     
    Eddie Corns, Aug 13, 2004
    #11
  12. > Thanks for the help. It's not homework but from an old exam paper, but
    > the girl's English isn't absolutely fluent and I'm looking for a
    > simple way to explain the expressions and functions.


    If there are points to be won for brevity then the answer is 'two functions (one
    with a helper) to compute factorials'
     
    Peter Hickman, Aug 13, 2004
    #12
  13. Fran

    Fran Guest

    Peter Hickman <> wrote in message news:<411cba6f$0$9687$>...
    > > Thanks for the help. It's not homework but from an old exam paper, but
    > > the girl's English isn't absolutely fluent and I'm looking for a
    > > simple way to explain the expressions and functions.

    >
    > If there are points to be won for brevity then the answer is 'two functions (one
    > with a helper) to compute factorials'



    Thanks greatly ...

    Admirably concise.

    FRAN
     
    Fran, Aug 14, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    798
    Views:
    10,520
    Raffael Cavallaro
    Nov 1, 2003
  2. Mark Wilson

    Fwd: Python syntax in Lisp and Scheme

    Mark Wilson, Oct 4, 2003, in forum: Python
    Replies:
    0
    Views:
    287
    Mark Wilson
    Oct 4, 2003
  3. Mark Wilson

    Re: Python syntax in Lisp and Scheme

    Mark Wilson, Oct 10, 2003, in forum: Python
    Replies:
    2
    Views:
    329
    Paul Foley
    Oct 10, 2003
  4. Mark Wilson

    Re: Python syntax in Lisp and Scheme

    Mark Wilson, Oct 12, 2003, in forum: Python
    Replies:
    1
    Views:
    328
    Alex Martelli
    Oct 12, 2003
  5. Replies:
    7
    Views:
    414
    Erann Gat
    Oct 13, 2003
Loading...

Share This Page