home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / scheme / 2758 < prev    next >
Encoding:
Internet Message Format  |  1992-12-15  |  3.2 KB

  1. Path: sparky!uunet!cs.utexas.edu!usc!news.aero.org!Aero.org!doner
  2. From: doner@Aero.org (John Doner)
  3. Newsgroups: comp.lang.scheme
  4. Subject: Re: Something I don't understand about bindings
  5. Date: 15 Dec 1992 21:23:15 GMT
  6. Organization: The Aerospace Corporation
  7. Lines: 73
  8. Distribution: world
  9. Message-ID: <1glic3INNq5b@news.aero.org>
  10. References: <a2vogler.724431792@rzdspc23> <1gl348INNp83@news.aero.org> <HANCHE.92Dec15134343@ptolemy.ams.sunysb.edu>
  11. NNTP-Posting-Host: armadillo.aero.org
  12.  
  13. In article <HANCHE.92Dec15134343@ptolemy.ams.sunysb.edu>, hanche@ams.sunysb.edu (Harald Hanche-Olsen) writes:
  14. |> >>>>> On 15 Dec 1992 17:03:04 GMT, doner@Aero.org (John Doner) said:
  15. |> John> NNTP-Posting-Host: armadillo.aero.org
  16. |> 
  17. |> John> In article <a2vogler.724431792@rzdspc23>, a2vogler@rzdspc23.informatik.uni-hamburg.de (Christian Vogler) writes:
  18. |> 
  19. ...
  20. |> John> |> (define (timed-prime-test n)
  21. |> John> |>   (define start-time (runtime))
  22. |> John> |>   (define found-prime? (prime? n))
  23. |> John> |>   (define elapsed-time (- (runtime) start-time))
  24. |> John> |>   (print n)
  25. |> John> |>   (cond (found-prime?
  26. |> John> |>          (print " *** ")
  27. |> John> |>          (print elapsed-time))))
  28. |> John> |> 
  29. |> John> |> The procedure then correctly prints the elapsed time for finding
  30. |> John> |> a prime number, but why does this work?  [...]
  31. |> 
  32. |> John> You are probably interpreting the define's as defining
  33. |> John> functions, whereas they are actually binding variables to new
  34. |> John> locations and assigning numeric values to those locations.  The
  35. |> John> defines are evaluated sequentially, and all three of start-time,
  36. |> John> found-prime?, and elapsed-time have their values before the
  37. |> John> printing begins.
  38. |> 
  39. |> That is not what r4rs says.  Indeed the above code is completely
  40. |> equivalent to
  41. |> 
  42. |>   (define (timed-prime-test n)
  43. |>     (letrec ((start-time (runtime))
  44. |>          (found-prime? (prime? n))
  45. |>          (elapsed-time (- (runtime) start-time)))
  46. |>       (print n)
  47. |>       (cond (found-prime?
  48. |>          (print " *** ")
  49. |>          (print elapsed-time)))))
  50. |> 
  51. |> which should not work, in standard r4rs scheme.  Not only is the order
  52. |> of evaluation in a letrec unspecified, but as it says in the section
  53. |> of internal defines:
  54. |> 
  55. |> Just as for the equivalent \ide{letrec} expression, it must be
  56. |> possible to evaluate each \hyper{expression} of every internal
  57. |> definition in a \hyper{body} without assigning or referring to
  58. |> the value of any \hyper{variable} being defined.
  59. |> 
  60. |> I can only conclude that the original poster uses a non-r4rs
  61. |> conforming scheme.
  62. |> 
  63. |> - Harald
  64.  
  65. You're right, it says there is an equivalent letrec expression, but isn't very
  66. precise about how to determine that.  And indeed, the letrec order of evaluation is
  67. unspecified.  On the other hand, his original define (again according to r4rs) is
  68. equivalent to
  69.  
  70.     (define timed-prime-test
  71.         (lambda (n)
  72.             (define start-time (runtime))
  73.             (define found-prime? (prime? n))
  74.             .
  75.             .
  76.             .
  77.                 ))
  78.  
  79. So the sequence of expressions beginning with the inner defines is the body of
  80. a lambda expression.  And r4rs says the expressions in the body of a lambda
  81. expression are to be evaluated sequentially.
  82.  
  83. Now I don't know what to think.
  84.  
  85. John
  86.