Guest User

Untitled

a guest
Mar 14th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.65 KB | None | 0 0
  1. ;recursive functional
  2. (let ! (proc (n)
  3.     (? (= n 0)
  4.         1
  5.         (* n (! (+ n (- 1))))
  6.     )
  7. ))
  8.  
  9. ;recursively-defined iterative functional
  10. (let ! (proc (n)
  11.     (let iter-fact (proc (a n acc)
  12.         (? (= a n)
  13.             (* acc n)
  14.             (iter-fact (+ a 1) n (* a acc))
  15.         )
  16.     ))
  17.     (iter-fact 1 n 1)
  18. ))
  19.  
  20. ;iteratively-defined (procedural)
  21. (let ! (proc (n)
  22.     ;these are just INITIAL values, the loop cannot change them in this scope, it will bind to the same value in its own scope and change within that
  23.     (let a 1)
  24.     (let acc 1)
  25.     (let loop (proc emp
  26.         (? (= a n)
  27.             (* acc n)
  28.             (begin
  29.                 (let acc (* a acc))
  30.                 (let a (+ a 1))
  31.                 (loop)
  32.             )
  33.         )
  34.     ))
  35.     (loop)
  36. ))
Add Comment
Please, Sign In to add comment