6 Replies - 1481 Views - Last Post: 26 February 2013 - 09:35 PM

#1 Borsif  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-February 13

Noob Scheme Procedure

Posted 26 February 2013 - 06:10 AM

I've found other ways to do this procedure but do not understand how this one in particular works. Could someone explain it step by step?

DrScheme, version 4.2.5

> (define (sum-square-max a b c)
  (if (and (>= a c) (>= b c))
    (+ (* a a) (* b B)/>/>)
    (sum-square-max c a B)/>/>))


Also, I tried ending the procedure with
(sum-square-max c a B)/>/>))
and Scheme sits there thinking for eternity, why?

Is This A Good Question/Topic? 0
  • +

Replies To: Noob Scheme Procedure

#2 Borsif  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-February 13

Re: Noob Scheme Procedure

Posted 26 February 2013 - 06:48 AM

I meant that I tried ending it with (sum-square-max a b c)))
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7626
  • View blog
  • Posts: 12,856
  • Joined: 19-March 11

Re: Noob Scheme Procedure

Posted 26 February 2013 - 08:25 AM

(define (sum-square-max p q r)
  (if (and (>= p r) (>= q r))
    (+ (* p p) (* q q))
    (sum-square-max r p q)))




define a function called sum-square-max taking three arguments, p, q, r, such that if p and q are both greater than r, return the sum of the squares of p and q, and if both are not greater than r, return the result of calling sum-square-max on r p and q. This is always going to return the sum of the squares of the two largest elements in the set p, q, r since there will always be some combination of p, q, r for which some r <= p, q, and this will only return a result in that case.

Quote

I tried ending it with (sum-square-max a b c)))
and Scheme sits there thinking for eternity, why?


At a guess, did you provide enough closing parens? If you don't, Scheme will patiently wait for you to finish the expression
Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Noob Scheme Procedure

Posted 26 February 2013 - 08:48 AM

As Jon pointed out the terminating condition is that the first two arguments are each larger than the third. The point of switching the argument order around is to ensure that that will eventually happen. So if you call (sum-square 1 2 3), that will call (sum-square 3 1 2) and that will call (sum-square 2 3 1). The last one will meet the if-condition, so the call will terminate.

If you change the recursive call to (sum-square a b c), you're no longer changing the argument positions. So calling (sum-square 1 2 3) will call (sum-square 1 2 3), which will call (sum-square 1 2 3), which will call (sum-square 1 2 3) ad infinitum. It never changes, so the terminating condition is never met. Thus you get an infinite loop.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7626
  • View blog
  • Posts: 12,856
  • Joined: 19-March 11

Re: Noob Scheme Procedure

Posted 26 February 2013 - 08:56 AM

Quote

If you change the recursive call to (sum-square a b c), you're no longer changing the argument positions. So calling (sum-square 1 2 3) will call (sum-square 1 2 3), which will call (sum-square 1 2 3), which will call (sum-square 1 2 3) ad infinitum. It never changes, so the terminating condition is never met. Thus you get an infinite loop.


Should have seen that... come on coffee, wake me up!

This post has been edited by jon.kiparsky: 26 February 2013 - 08:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 Borsif  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-February 13

Re: Noob Scheme Procedure

Posted 26 February 2013 - 09:28 PM

So by the third line "sum-square-max" has already been defined in a sense and in line 4 it is using that definition to feed back into line 2 with alternate argument positions? I think this solution is beyond the supposed capability of where I am at learning lol but I get it now (I think) thanks!
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7626
  • View blog
  • Posts: 12,856
  • Joined: 19-March 11

Re: Noob Scheme Procedure

Posted 26 February 2013 - 09:35 PM

(define (sum-square-max p q r)   ;  specifies name of function and arguments
  (if (and (>= p r) (>= q r))          ;  specifies condition for selecting between
    (+ (* p p) (* q q))                ;  evaluating and returning an answer by a particular process
    (sum-square-max r p q)))           ;  or shuffling the arguments and trying again




Notice that this is pretty limited in its scope: you couldn't extend this logic, as it stands, to cover
sum-square-max-of-four or of arbitrary argument length. You'd need a helper function to select the two largest elements of a given list.

First of all, do you see how you'd write that, if you had that max-2-from-list function?
And do you see how you could write that function?

Notice that you could easily define a sum-square-max-of-list function even if you didn't have the max-2-from-list in hand: you'd just write a function that takes in a list and returns the first two elements from it, and pretend that that's correct. Once you're sure that the main function was working correctly, you could go back and write the helper function, and then you'd be set.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1