6 Replies - 850 Views - Last Post: 06 September 2011 - 01:31 PM

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Defining "+" in Scheme

Posted 06 September 2011 - 06:59 AM

I am redefining the "+" in Scheme two different ways. I did so like this:

(define (+ a B)/>
	(if (= a 0)
	b
	(inc (+ dec a) B)/>)))



And like this:
(define (+ a B)/>
	(if (= a 0)
	b
	(+ (dec a) (inc B)/>)))



EDIT: It keeps capitalizing the "B" in my code snippets for some reason. It really does not matter to the question I am asking but just pointing it out.

I have to write out the steps of the evolutions of the evaluation of (+ 4 5) for both definitions of "+". I was able to follow some examples the professor gave in class and figured out how to do the evolution of the first definition. This is how I did it:

(+ 4 5)
(inc (+ 3 5))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9



I need some guidance on how do it with the second definition. Examples, the first few lines, or anything would be helpful.

Thanks in advance.

This post has been edited by mattlyons: 06 September 2011 - 07:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Defining "+" in Scheme

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Defining "+" in Scheme

Posted 06 September 2011 - 07:45 AM

Maybe it would help you to go through the steps in more detail than you did in the first example:

You start with (+ 4 5). Then you insert the arguments 4 and 5 into the body of your + function, so you get:

(if (= 4 0)
    5
    (+ (dec 4) (inc 5)))



Since (= 4 0) is false, the else-part of the if applies, so you get (+ (dec 4) (inc 5)).

If you apply the dec and inc functions that becomes (+ 3 6).

And from there you can apply +'s function definition again as we did in the first step and basically continue the same couple of steps until you reach the end.
Was This Post Helpful? 2
  • +
  • -

#3 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Defining "+" in Scheme

Posted 06 September 2011 - 08:22 AM

Thanks!

Well I think I got it after stepping through it pretty slowly. This is what I came up with:

(+ 4 5)
(+ 3 (inc 5))
(+ 2 (inc (inc 5)))
(+ 1 (inc (inc (inc 5))))
(+ 0 (inc (inc (inc (inc 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9


Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Defining "+" in Scheme

Posted 06 September 2011 - 08:25 AM

That works, but that's not the order in which scheme will actually evaluate the arguments. When applying a function, will always fully evaluate all the arguments before it actually applies the function (this is called strict evaluation). So (+ 3 (inc 5)) will become (+ 3 6) before + is applied again.
Was This Post Helpful? 1
  • +
  • -

#5 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Defining "+" in Scheme

Posted 06 September 2011 - 08:59 AM

Oh, so it would be like this?:

(+ 4 5)
(+ 3 6)
(+ 2 7)
(+ 1 8)
(+ 0 9)
9


Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2087
  • View blog
  • Posts: 3,175
  • Joined: 21-June 11

Re: Defining "+" in Scheme

Posted 06 September 2011 - 09:01 AM

Yes, exactly.
Was This Post Helpful? 0
  • +
  • -

#7 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Defining "+" in Scheme

Posted 06 September 2011 - 01:31 PM

Thanks sepp2k.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1