Lazy Coding PPL
Lazy Coding PPL
c)
d) Applicative order
e) lazy normal-orderLazy evaluation is
used for all arguments in Miranda and
)
(driver output)
Interactive Eg
( define squares (lambda (s)
( cons enter a number\n
( let ( (n (car s)) )
( if (eof-object? n) '()
(cons (* n n) (cons
#\newline (squares (cdr s) )
)
)
)
)
)
(define output (squares input) )
2.2. Unfortunately, while they
successfully encapsulate the imperative
nature of interaction at a terminal,
streams dont work very well for graphics
or random access to files.
3. LOGIC PROGRAMMING
3.1. Logic programming systems allow the
programmer to state a collection of
axioms from which theorems can be proven.
3.2. The user of a logic program states a
theorem, or goal, and the language
terms of unifiability.
4.13. It is possible for two variables to
be unified without instantiating them.
4.14. difference between functions and
Prolog predicates. The former have a
clear notion of inputs (arguments) and
outputs (results); the latter do not.
4.15. List : book
4.16. To handle arithmetic, Prolog
provides a built-in predicate, is , that
unifies its first argument with the
arithmetic value of its second argument:
4.17. Order of Evaluation EG: book.
Efficiency
4.18. Infinite Regression------book dgm
a) . In effect, the Prolog interpreter
gets lost in an infinite branch of the
search tree, and never discovers finite
branches to the right. We could avoid
this problem by exploring the tree in
breadth-first order, but that strategy
was rejected by Prologs designers
because of its expense: it can require
substantially more space, and does not
lend itself to a stack-based
implementation.
4.19. IMPERATIVE CONTROL FLOW
a) The cut is a zero-argument predicate
written as an exclamation point: ! .
member(X, [X | _]) :- !.
member(X, [_ | T]) :- member(X, T).
The cut on the right-hand side of the
write_everybody_happy :- happy(X),
write(X),nl,
fail.
write_everybody_happy :- true.
If you consult this little program and the ask Prolog the
query write_everybody_happy, Prolog will look up in its database all
objects that make happy(X) true and will print them out on the screen.
So, if the database looked as follows
happy(harry).
happy(ron).
happy(hermione).
happy(hagrid).
Prolog would write on the screen
harry
ron
hermione
hagrid
yes
d)
h)
get acts as :
repeat.
repeat :- repeat.
5.
DATABASE MANIPULATION
5.1. Prolog programs as data
5.2. Adding to database while pgm running:
a)
?-assert(rainy(chennai))
5.3. Removing
a)
?-retract(rainy(chennai))
b)
?-retractall
c)
5.4. FUNCTOR
Individual terms in Prolog can be created, or their contents
extracted, using the built-in predicates functor , arg , and =.. .
The goal functor(T, F, N) succeeds if and only if T is a term
with functor F and arity N :
?- functor(foo(a, b, c), foo, 3).
Yes
?- functor(foo(a, b, c), F, N).
F = foo
N=3
5.6. DISADVANTAGES
a)
Parts of Logic not covered (eg: disjunction with more
than one non-negated term.)
b)
Evaluation order
c)