Loading ...
Sorry, an error occurred while loading the content.

RE: [sicp-vsg] exercise1.6 Alyssa P Hacker's new-if

Expand Messages
  • Kaushik Sridharan
    The problem here is that since new-if is not a special form, it will always evaluate all its arguments. This is not a problem if you are doing something like
    Message 1 of 3 , Nov 17, 2007
    • 0 Attachment
      The problem here is that since new-if is not a special form, it will always evaluate all its arguments.
       
      This is not a problem if you are doing something like (new-if (= 2 3) 0 5). Both the then-clause and else-clause are evaluated before new-if is called, which amounts to nothing more than evaulating the value of 0 and 5. No big deal.
       
      However, if you are doing something like this:
       
      (define (sqrt-iter guess x)
        (new-if (good-enough? guess x)
                guess
                (sqrt-iter (improve guess x)
                           x)))
       
      ...the inner sqrt-iter call in the else-clause is always evaluated, regardless of whether the guess was good enough or not. The inner call to sqrt-iter is made before the outer new-if function gets a chance to have a go at it. You thus get into an infinite self-recursion calling in sqrt-iter.
       
      On the other hand, since if is a special form, it does not evaluate it's arguments before-hand, so you manage to side-step this self-recursion issue.
       
      Hope this helps,
       
      -K
       

       

      To: sicp-vsg@yahoogroups.com
      From: pansteth@...
      Date: Sat, 17 Nov 2007 07:31:40 -0800
      Subject: Re: [sicp-vsg] exercise1.6 Alyssa P Hacker's new-if

      FYI, this has not not been an active mailing list for some time.

      I don't have the textbook nor my solutions handy, but recall
      the difficulties of working through SICP by myself.   I gave up
      eventually.

      In my opinion, it is not a good book for independent study.

      As I recall, there are some "trick" questions in those early exercises.
      You may have run into one.    As I remember, the point of one
      of them is that "if" must be a "special form"--or whatever the
      Scheme jargon is-- and  cannot  be implemented as a function.

      If I'm remembering correctly, the authors have set the reader up
      for failure by posing a ill-formed, misleading question.

      It's possible I'm remembering wrong on this particular exercise.    Even if I am, there are plenty of other examples of bad pedagogy in SICP.

      I had better success with "ANSI Common Lisp" by Paul Graham.

      There are some Google discussion groups about Scheme and Lisp
      that you might want to check out too.




      "vincent.lau" <vince_lau@...> wrote:
      about Alyssa P Hacker's new-if,i thought new-if just like original if
      in scheme at first,but when i try to run the new-if version sqrt via
      (print (sqrt 2)), there is nothing put out(i thought will be 1.4142),i
      can't figure out what happed? who can explain it?




      Yahoo! Groups Links

      <*> To visit your group on the web, go to:
      http://groups.yahoo.com/group/sicp-vsg/

      <*> Your email settings:
      Individual Email | Traditional

      <*> To change settings online go to:
      http://groups.yahoo.com/group/sicp-vsg/join
      (Yahoo! ID required)

      <*> To change settings via email:
      mailto:sicp-vsg-digest@yahoogroups.com
      mailto:sicp-vsg-fullfeatured@yahoogroups.com

      <*> To unsubscribe from this group, send an email to:
      sicp-vsg-unsubscribe@yahoogroups.com

      <*> Your use of Yahoo! Groups is subject to:
      http://docs.yahoo.com/info/terms/




      Philip Ansteth
      ANSTETH RESEARCH
      http://www.ansteth.net
      918-743-6342; 505-776-2468
      Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it now.


      Get the power of Windows + Web with the new Windows Live. Power up!
    Your message has been successfully submitted and would be delivered to recipients shortly.