Beruflich Dokumente
Kultur Dokumente
PROLOG
WHAT TO KNOW:
Logic Programming (progams =set of constraints, seeks all solutions)/Good for Meta Interpreters
Descriptive: describing known facts and relationships (or rules) about a specific problem
Specify Facts, Define rules, start queries and uses first order logic predicate symbols (equality, negation, logic
binary connections)
3- Arithmetic Expressions and I/O
Supported in Prolog : X+Y, X-Y, X*Y, X/Y, X // Y %integer division, X mod Y, X**Y , abs(X), ln(X) , sqrt(X)
X =:= Y % X equals Y, X =\= Y % X not equals Y, X < Y, X =< Y, X > Y, X >= Y
?- 1+2 = 1+2. true. ?- 3 = 1+2. false. ?- 1+2 = 2+1. false. ?- 3 is 1+2. true. ?- X is 1+2, X is 2+1. X = 3.
Power : pow( X, Y, Z ) :- Y > 1, Y1 is Y - 1, pow( X, Y1, Z1 ), Z is X * Z1. Or X**Y
Factorial: fact(0,1) as base case + fact(N, F) :- N > 0, N1 is N 1, fact(N1, F1), F is F1 * N.
Write/1: write(X). adds the value of X to the currently active output stream (by default the console). Read/1:
read(X) d X will be instantiated .
4 The Cut and Intro to List Processing
The Cut: ! - Goal that always succeeds and has a side effect (does not backtrack, seek to re-satisfy predicate)
Fail: fail - The fail predicate always fails. Combined with the Cut we get negation
not/1: not(x) Negation by failure. Prolog proofs the success of a goal not(F) by showing that it cannot satisfy F.
Remember that Prolog may also fail to proof F for trivial reasons, e.g., it may simply missing
facts in its database.
Not Equals or Difference: X=\= Y.
5 - List Representations & Processing
Representation: A list is represented by a particular tree with all the leaves of the tree on
branches to the left.
Dot operator: (X.Y) represents the head X (first element) and the tail Y of the list. (Needs
brackets [ ])
Accessing: use [head|tail] (ex : [st_john, moncton | [fredericton]] or [st_john | [moncton | [fredericton]]])
Member: Predicate member(X,L) : Recursive program member(X,[X|T]). member(X,[H|T]) :- member(X,T).
Insertion: listInsert(A,L,[A|L]). listInsert(A,[X|L], [X|LL]) :- listInsert(A,L,LL). (accumulate Lists in recurse)
Joining Lists: appendList([ ],Y,Y). appendList([A|B],Y,[A|W]) :- appendList(B,Y,W).
Length of List: listLength([],0). listLength([X|L],N) :- listLength(L,NN), N is NN+1.
6 More Examples
Findall/3: finds all possible Xs and puts in list in findall( X, condition, L). does not bind
variables
setof/3: setoff(Xs,Condition,L) finds all the solution and enters them in a list. eliminates
duplicates and sorts the result. Should be binded with ^
bagof(+Template, :Goal, -Bag) : Should be binded with ^. Similar to findall/3
7- Binary Trees and Graphs
Fact for a node: t(element, left, right) ( Ex: t(1,t(2,nil,nil),t(3,nil,nil)).)
Define a graph: graphA(X) :- X=g([a,b,c,d,e,f], [edge(a,b,3), edge(a,c,5), edge(a,d,7), edge(e,f,1), edge(d,f,6)]).\
SCHEME:
WHAT TO KNOW:
Functional Programming(program correspons to a function call/no loops & control statement/no variable &
assignments)/Good for games, symbolic computation, artificial intelligence, theorem proving.
Key Concept(conditons(if-then-else)/Functions as data types/Recursions/Variables as Pointers/automatic garbage
collection, lists and trees)
Interpreted language from LISP LISt Processor, continues to evolve, designed by MIT List is he
fundamental data structure
Programs start: (define ( blabla arg1 arg2) ..)
If statements (if (condition) (#t) (#f) ) or (if (condition) (#t) (if (condition) (#t) (#f)))
Conditions: cond ( ((condition) (#t)) ( (condition) (#t)) (else (#t)))
Let : temp value assignement (let ((a 3) (b 4)) (* a b) (+ a b) => 12 7
Let*: let* is similar to let but allows for sequential definitions. Same let/line variable changes.
Lists & stuff :
list expression
c))
(cons 1 2) => (1 . 2)
=> 4
(cons '(1 2) '(3 4)) =>
((1 2) 3 4)
(cons 2 '(3 4)) => (2 3 4)
(list 1 2 3) => (1 2 3)
(list '(1) '(2) '(3)) => ((1) (2)
(3))
(list '(a b c) '(d e f)) => ((a b
CAR: Content of the
Address Register ex:
(car '(a b c)) => a
(car '((a b) b c)) =>
(a b)
CDR: Content of the Decrement Register
ex: (cdr '(a b c)) => (b c) or (cdr '((a b)
b c)) => (b c)
Append List: (define (append-list L1 L2)
(if (null? L1)
L2
(cons (car L1) (append-list (cdr L1) L2))))
APPLY: apply proc arg1 ... args proc = procedure/args mustbe a list
ex: (apply +
(list 3 4))
==> 7
MAP: map proc list1 list2 ... lists = lists, proc = procedure = #args == #lists and
returning a single value.. Same length list. Applies proc per element and returns list
in order or evaluation.
TREE: ROOT = (car t) LEFT = (cadr t) RIGHT = (caddr t)
I/O : (display string), (read), (display (read))
DO: (do ((var init update) ...) (test resultIfTrue ...)
exprIfTestFalse...)
Ex:
(do ((i n (- i 1)) ; i=1, --I *NOTE* :inc/dec is done after a
fullloop.
(a1 1 (+ a1 a2)) ; a1=1, a1+=a2
(a2 0 a1))
; a2=0, a2=a1
or How I do it
((= i 1) a1)))))