Programming Paradigms CSI2120 - Winter 2019: Jochen Lang EECS, University of Ottawa Canada
Programming Paradigms CSI2120 - Winter 2019: Jochen Lang EECS, University of Ottawa Canada
Paradigms CSI2120
– Winter 2019
Jochen Lang
EECS, University of Ottawa
Canada
Logic Programming in Prolog
• Syntax: !
– goal that always succeeds and has a side effect (on the way
backtracking works)
• Red Cuts
– change the solution space (see the previous root example).
• Green Cuts
– remove choices that wouldn't work anyway
– efficiency gain
– reduced memory footprint
?- aList(X, Y, [1,2,3,4]).
X= 1,
Y= [2,3,4].
• List membership
listMember(X,[X|L]). % Searched for element is
% head – boundary case
listMember(X,[Y|L]) :-
listMember(X,L). % loop over list
appendList([],Y,Y).
appendList([A|B],Y,[A|W]) :-
appendList(B,Y,W).
read_line('\n', []) :- !.
read_line(C, [C | RestOfLine]) :-
C \= '\n',
get_char(NextC),
read_line(NextC, RestOfLine).
• Similar to SWI library predicate
read_line_to_codes(user_input, ListChar).