blob: 35880ea5931896f3d0ed7076039cacd29431dfbc (
plain) (
tree)
|
|
* notes
** 1
*** 1
**** 5
#+BEGIN_QUOTE
In general, when modeling phenomena in science and engineering, we
begin with simplified, incomplete models. As we examine things in
greater detail, these simple models become inadequate and must be
replaced by more refined models.
#+END_QUOTE
** guile
*** use module
#+BEGIN_SRC scheme
(use-modules (some thing))
#+END_SRC
* exercises
** 1
*** 4
#+BEGIN_SRC scheme
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
#+END_SRC
If ~b~ is greater than 0, do ~a + b~; otherwise do ~a - b~.
*** 5
code at [[./one/five.scm]]
If the interpreter uses *applicative order* to evaluate the
expression:
#+BEGIN_SRC scheme
(test 0 (p))
#+END_SRC
The parameters are evaluated before applying the compound procedure
~test~; 0 evaluates to 0, ~(p)~ never finishes evaluating as the
compound procedure ~p~ recursively calls itself again and again
infinitely.
If same expression is evaluated by the interpreter using *normal
order*, the expression will be expanded to
#+BEGIN_SRC scheme
(if (= 0 0)
0
(p)))
#+END_SRC
and will evaluate to ~0~.
*** 6
If I've understood it correctly, scheme uses applicative-order
evaluation, meaning, it evaluates the operands before appling the
procedure.
In the case when ~new-if~ used in the ~sqrt-iter~ procedure, the
operands/arguments for the ~new-if~ -- ~(good-enough? guess x)~,
~guess~, ~(sqrt-iter (improve guess x) x)~ -- are evaluated. Due
to the last operand, which is a call to the ~sqrt-iter~ procedure,
we get into infinite loop of evaluating the ~sqrt-iter~ procedure
again and again.
*** 7
#+BEGIN_SRC scheme
$ guile
scheme@(guile-user)> (sqrt 0.1)
$1 = 0.31622776601683794
#+END_SRC
#+BEGIN_EXAMPLE
0.31622776601683794
0.0001 = 0.316245562280389
0.00001 = 0.31622776651756745
0.000001 = 0.31622776651756745
0.0000001 = 0.31622776651756745
0.00000001 = 0.31622776651756745
0.000000001 = 0.31622776651756745
0.0000000001 = 0.31622776601683794
#+END_EXAMPLE
|