Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- swipl
- [test].
- man(john).
- Find the movies released after 1990:
- movie(m, y), y > 1990.
- Use semicolon (;, represents or) to cycle through results
- Find the movies released before 2000, but after 1995
- movie(m, y), y < 2000, y > 1995
- [moviesdb].
- [user].
- |: released_after(M, Y) :- movie(M, Z), Z > Y.
- (ctrl d to exit editor mode)
- ?- 4 = 2 + 2.
- false.
- (Returns false because not = not an equality operator, it's a unification operator)
- ?- 4=:= 2+2.
- true.
- ?- A = 4, B = 2 + 2, A =:= B.
- (first returns true because variable matches/unifies any complex term, second returns true because variable unifies with complex terms)
- ?- A=4, B is 2+2.
- A=B, B=4. (is is the assignment operator)
- List is head and tail.
- [H | T] = [a, b, c, d]
- H = a
- T = b, c, d
- [H | T] = [a].
- H = a,
- T = [].
- [_,_,X|T] = [john, sam, 13, abc].
- X = 13,
- T = [abc].
- square(IN, OUT) :- OUT is IN * IN.
- write_list([]).
- write_list([H|T]) :- write(H), nl, write_list(T). (prints the head, prints newline, goes to the tail of the list)
- len([],0).
- len([H|T],L) :- len(T, L1), L is L1 + 1.
- RECURSION: needs a basecase
- len([], 0). (base case, list is empty)
- len([_|T], OUT) :- len(T, OUT1), OUT is OUT1 + 1.
- ^ Keeps calling itself on tail of the list. Last call returns 0, 0 + 1 is then put in output variable.
- is = assignment operator
- Singleton variable warning means var name is not used. Replace H with _ to avoid this
- Now we want to write the function reCons([a,b,c], OUT).
- OUTput should be = [a,b,c].
- Base case? reCons([], []).
- reCons([H|T],[H|T1]) :- reCons(T,T1).
- ^ When we get to the end of the list, T1 is going to be unified with empty list when it hits the base case
- Everytime it goes back it puts the current H on the list
- To do recursion, we keep going to the tail of the list, till we hit the base case
- Exercise!
- Create a function square([1,2,3],LIST).
- output: LIST=[1,4,9].
- square([],[]).
- square([H|T],[H1|T1]) :- square(T,T1), H1 is H * H.
- ^ H1 is the head of the output list. We calculate a new list that is unified with H1
- Output of exercise:
- ?- [user].
- |: square([],[]).
- |: square([H|T],[H1|T1]) :- square(T,T1), H1 is H * H.
- |: ^D% user://1 compiled 0.01 sec, 2 clauses
- true.
- ?- square([1,2,3], LIST).
- LIST = [1, 4, 9].
- ?- trace.
- true.
- [trace] ....
- Exercise!
- Traverse two lists and make sure each element in the second list is the square of the element in the first list. E.g,
- ([1,2,3],[1,4,9])
- Use equality operator (=:=)
- Base case: both lists should be empty. Recursive call, go to the end
- isSquare([],[]).
- isSquare([H|T],[H1|T1]) :- isSquare(T, T1), S is H * H, S =:= H1.
- ^ Each predicate is separated by commans (and). Each predicate must be true
- isSquare([H|T],[H1|T1]) :- S is H * H, S =:= H1; S1 is H1 * H1, S1 =:= H, isSquare(T,T1).
- ^ Two conditons that are anded, OR, two conditions that are anded, then recursive call
- This works for isSquare([1,4,9],[1,2,3])
- Tail recursion: operation happens first, then recursion happens. At the beginning we need to introduce an accumulator value
- that holds the calculation
- len1([H|T],L) :- helper([H|T],L,0).
- helper([],ACC,ACC). (this is the base case)
- helper([_|T],L,ACC) :- ACC1 is ACC + 1, helper(T,L,ACC1).
- ^Initial value of accumalator is zero. Base case is when list is empty. L is the output variable, which then unifies with ACC.
- Third line: calculation happens, then is passed to the next recursive call
- End of file error means missing period (.) terminator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement