I'm not quite sure exactly I was searching for, but somehow I serendipitously stumbled upon the site learnprolognow.org a few months ago. It's the home for an introductory Prolog programming course. Logic programming offers an interesting way to think about your problems; I've been doing so much procedural and object-oriented programming in the past decade that it really took effort to think at a higher level!
I found the most interesting features to be definite clause grammars (DCG), and unification. Difference lists are very powerful and Prolog's DCG syntax makes it easy to work with them. Specifying a grammar such as:
One of the difficulties of self-study though is the lack of teacher and classmates to ask questions and verify solutions. I purchased the printed version of the course to check my work but was dismayed to see it only contains answers for the chapter exercises. I've decided to make my solutions to the practical sections available for other self-learners.
I definitely recommend setting aside some spare time and working your way through the course if you haven't been exposed to Prolog somewhere in your studies or career already.
I found the most interesting features to be definite clause grammars (DCG), and unification. Difference lists are very powerful and Prolog's DCG syntax makes it easy to work with them. Specifying a grammar such as:
s(s(NP,VP)) --> np(NP,X,Y,subject), vp(VP,X,Y).allows me to parse a statement for grammatical correctness, produce a parse tree, and determine where the point of failure is if it isn't grammatical.
np(np(DET,NBAR,PP),X,Y,_) --> det(DET,X), nbar(NBAR,X,Y),
pp(PP).
np(np(DET,NBAR),X,Y,_) --> det(DET,X), nbar(NBAR,X,Y).
np(np(PRO),X,Y,Z) --> pro(PRO,X,Y,Z).
vp(vp(V),X,Y) --> v(V,X,Y).
vp(vp(V,NP),X,Y) --> v(V,X,Y), np(NP,_,_,object).
nbar(nbar(JP),X,3) --> jp(JP,X).
pp(pp(PREP,NP)) --> prep(PREP), np(NP,_,_,object).
jp(N,X) --> n(N,X).
jp(jp(ADJ,JP),X) --> adj(ADJ), jp(JP,X).
det(det(DET),X) --> [DET], {lex(DET,det,X)}.
pro(pro(PRO),X,Y,Z) --> [PRO], {lex(PRO,pro,X,Y,Z)}.
v(v(V),X,Y) --> [V], {lex(V,v,X,Y)}.
prep(prep(PREP)) --> [PREP], {lex(PREP,prep)}.
n(n(N),X) --> [N], {lex(N,n,X)}.
adj(adj(ADJ)) --> [ADJ], {lex(ADJ,adj)}.
?- s(X,[the,man,shoots,he],Y).The power of unification is highlighted by the
X = s(np(det(the), nbar(n(man))), vp(v(shoots))),
Y = [he]
append/3
predicate, a "function" whose primary purpose is to concatenate two lists. By providing partial lists and pattern structures, append can not only be used to concatenate, but also split a list, double it, and even test an element for membership.is_member(X,Y) :- append(_,[X|_],Y).I have to say working through the course has made me a better programmer. It exposed me to new ways of thinking about old problems which broadened my "programmer toolset." I feel more comfortable working with immutable variables and recursion, too-- both concepts which I knew but subconsciously shied away from.
?- is_member(4,[1,2,3,4,5]).
Yes
One of the difficulties of self-study though is the lack of teacher and classmates to ask questions and verify solutions. I purchased the printed version of the course to check my work but was dismayed to see it only contains answers for the chapter exercises. I've decided to make my solutions to the practical sections available for other self-learners.
I definitely recommend setting aside some spare time and working your way through the course if you haven't been exposed to Prolog somewhere in your studies or career already.
Thank you very much. I couldn't find the solutions anywhere.
ReplyDeleteYou're welcome. Good luck with your studies!
ReplyDeleteThanks for the solutions, you did the world a favor! :]
ReplyDeleteThanks a ton for your solutions to the practical sessions!
ReplyDeleteYou are a god among men, a man among mice, a mouse among inferior creatures!
ReplyDeleteinv (List1, ResultingList).
ReplyDeleteinv ([2 , 1 , 0 , 0.25 ] , R).
R = [ 0.5, 1, inf, 4.0 ]. % 1/2 = 0.5, 1/1 = 1, etc.
super thanks!!
ReplyDeleteA BIG thank you from the Netherlands! You've just made my learning experience a lot better! :-)
ReplyDeletethanks man >>A BIG thank you from EGYPT
ReplyDeletewill bw happe to connect u
Thank you so much! I'm struggling to write my project for school and this'll help me so much =) Prolog looks awesome!! Greetings from Turkey =)
ReplyDeleteI have also started working through LPN, and I found an interesting hitch in the Chapter 4 Practical section, specifically under Section 4.5.
ReplyDeletemysubset/2, as it stands, will give a positive match for mysubset([a,a],[a]), even though technically [a,a] is not a subset of [a]. Noting this, my solution rewrites member/2 such that it removes the matched element and returns the rest in a third argument.
% member_rem(member, list, giveback).
member_rem(X, Y) :- member_rem(X, Y, _) % member_rem/2 acts like member/2
member_rem(H, [H|T], T).
member_rem(X, [Y|Ys], [Y|G]) :- member_rem(X, Ys, G).
% mysubset_rem(subset, superset).
mysubset_rem([], _).
mysubset_rem([X|Xs], Y) :- member_rem(X, Y, G), mysubset_rem(Xs, G).
% mysuperset_rem(superset, subset).
mysuperset_rem(X, Y) :- mysubset_rem(Y, X).
For those who need a commentary: the "giveback" in member_rem/3 is another variable that is kept until member_rem matches the first case, after which it 'returns'--in massive quotes because I am an imperative programmer to the death--the tail of that list, i.e. missing the head element.
I'm sure that the writers of the book probably meant the same solution that you got, which I had found as well, but I just think it's interesting that it should be deficient in such a subtle way.
Thank you so much! You made my day!
ReplyDeleteThanks!
ReplyDeleteExcellent. Thanks for this. I'm working my way through the course online and I was looking for the solutions.
ReplyDeleteThanks from France :)
ReplyDeleteThank you very much !!!! You really help interested students :)
ReplyDeleteIn the Learn Prolog Now book, 3.4 practical session question 2, if the user wants to count the number of transit and find all the possible traveling routes by type of vehicle.
ReplyDeleteHow to do? Thanks.
where are the solutions?
ReplyDeleteBig thanks from Belgium ;)
ReplyDeleteThank you so much!!! I was struggling especially with 3.4 practical session (travel(valmont, losAngeles,X) problem. Very kind of you to share this knowledge with the rest of us.
ReplyDeleteGreetings from Greece,
Christos G.
Hi Timothy!
ReplyDeleteThank you for the solutions. :) I just wanted to quickly point out that you are missing the answer to Exercise 3.1. It was a text-based question, so I thought you may have skipped it by accident (your answers to Chapter 3's exercises start off with the Matryoshka dolls question, which is 3.2, not 3.1). Anyway, thank you very much!
Your solution to 3.2 doesn't seem to work. Can't figure that one out :/
ReplyDelete