[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Assignments] [Labs]
Back to FP, Continued. On to Introduction to Logic Programming.
Held Friday, March 12
Summary
Contents
Handouts
Notes
Def fact = eq0 -> one ; recurse
one
Def one = constant 1
% in
our implementation of fp.
{one %1}
eq0
id
Def eq0 = = @ [id,%0]
@ is function composition
[...] if function construction
{eq0 = @ [id,%0]}
sub1
Def sub1 = - @ [id,%1]
{sub1 - @ [id,%1]}
* @ [id, fact@sub1]
{fact (eq0 -> %1 ; * @ [id, fact@sub1])}
[%1, %1, id]
+ @ [id,%1] @ 1
1 is a selector, which extracts the first
part of the tuple.
+ @ [1,%1]
* @ [1,2]
3
[+ @ [id,%1], * @ [1,2], 3]
< @ [1,3]
Def itfact = 2 @ (while (<= @ [1,3]) ([+ @ [1,%1], * @ [1,2], 3])) @ [%1, %1, id]
{itfact 2 @ (while (<= @ [1,3]) ([+ @ [1,%1], * @ [1,2], 3])) @ [%1, %1, id]}
iota.
iota:n gives all the numbers from 1 to n,
we just want to insert multiplication between them.
{newfact !*@iota}
fact 0 = 1 fact n = n * fact n-1
car (cons x xs) = x cdr (cons x xs) = xs
if True x y = x if False x y = y
square (plus 2 3),
(given the defintion of square as
square x = times x x
square (plus 2 3) => times (plus 2 3) (plus 2 3) => times 5 (plus 2 3) => times 5 5 => 25
square (plus 2 3) => square 5 => times 5 5 25
fact n = iif (n == 0) 1 (n * (fact (n - 1)))
fact 1 => iif (1 == 0) 1 (1 * (fact (1 - 1))) => iif false 1 (n * (fact (1 - 1))) => iif false 1 (n * (fact 0)) => iif false 1 (iif (0 == 0) 1 (0 * (fact (0-1)))) => iif false 1 (iif false 1 (0 * (fact (0-1)))) => iif false 1 (iif false 1 (0 * (fact (-1)))) => iif false 1 (iif false 1 (0 * (iif (-1 == 0) 1 (-1 * fact(-1-1))))) => ...
History
Back to FP, Continued. On to Introduction to Logic Programming.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Assignments] [Labs]
Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS302/99S/Outlines/outline.21.html
Source text last modified Mon Apr 5 14:32:24 1999.
This page generated on Mon Apr 5 14:38:34 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu