Title | Practical - Program |
---|---|
Author | Jigar Thakkar |
Course | Information Management Techniques |
Institution | University of Melbourne |
Pages | 3 |
File Size | 306.8 KB |
File Type | |
Total Downloads | 64 |
Total Views | 140 |
program...
10/2/2016
How this prolog crossword solver works? - Stack Overflow
1
1
help
Howthisprologcrosswordsolverworks?
IamtryingtowriteacrosswordsolverIhavegotthiscodebutIcan'tUnderstandsomepartsofit: size(5). black(1,3). black(2,3). black(3,2). black(4,3). black(5,1). black(5,5). words([do,ore,ma,lis,ur,as,po, so,pirus, oker,al,adam, ik]) . :- use_module(library(lists),[nth1/3, select/3]). crossword(Puzzle) :words(WordList), word2chars(WordList,CharsList), make_empty_words(EmptyWords) , fill_in(CharsList,EmptyWords), word2chars(Puzzle,EmptyWords). word2chars([],[]). word2chars([Word|RestWords] ,[Chars|RestChars] ) :atom_chars(Word,Chars), word2chars(RestWords,RestChars). fill_in([],[]). fill_in([Word|RestWords],Puzzle) :select(Word,Puzzle,RestPuzzle), fill_in(RestWords,RestPuzzle). make_empty_words(EmptyWords) :size(Size), make_puzzle(Size,Puzzle), findall(black(I,J),black(I,J),Blacks) , fillblacks(Blacks,Puzzle), empty_words(Puzzle,EmptyWords). make_puzzle(Size,Puzzle) :length(Puzzle,Size), make_lines(Puzzle,Size). make_lines([],_). make_lines([L|Ls],Size) :length(L,Size), make_lines(Ls,Size). fillblacks([],_). fillblacks([black(I,J)|Blacks],Puzzle) :nth1(I,Puzzle,LineI), nth1(J,LineI,black), fillblacks(Blacks,Puzzle). empty_words(Puzzle,EmptyWords) :empty_words(Puzzle,EmptyWords,TailEmptyWords), size(Size), transpose(Size,Puzzle,[],TransposedPuzzle), empty_words(TransposedPuzzle,TailEmptyWords,[] ). empty_words([],Es,Es). empty_words([L|Ls],Es,EsTail) :empty_words_on_one_line(L,Es,Es1) , empty_words(Ls,Es1,EsTail). empty_words_on_one_line([], Tail, Tail). empty_words_on_one_line([V1,V2|L],[[V1,V2|Vars]|R],Tail) :var(V1), var(V2), !, more_empty(L,RestL,Vars), empty_words_on_one_line(RestL,R,Tail) . empty_words_on_one_line([_| RestL],R, Tail) :empty_words_on_one_line(RestL,R,Tail) . more_empty([],[],[]). more_empty([V|R],RestL,Vars) :( var(V) -> Vars = [V|RestVars], more_empty(R,RestL,RestVars) ; RestL = R, Vars = [] ). transpose(N,Puzzle,Acc,TransposedPuzzle) :( N == 0 -> TransposedPuzzle = Acc ; nth_elements(N,Puzzle,OneVert),
http://stackoverflow.com/questions/27677596/how-this-prolog-crossword-solver-works
1/3
10/2/2016
How this prolog crossword solver works? - Stack Overflow
M is N - 1, transpose(M,Puzzle,[OneVert|Acc], TransposedPuzzle) ). nth_elements(_,[],[]). nth_elements(N,[X|R],[NthX| S]) :nth1(N,X,NthX), nth_elements(N,R,S).
Thiscodeisusedforsolvingcrosswordslikethis:
Whataresymbols
;
-> usedfor?
Mymainproblemisunderstandingtherules,transpose andmore_empty .Anyexplanationtohelpmeunderstandthecodewould beappreciated. prolog editedJan8'15at5:26
askedDec28'14at15:05
AliNfr 433
1
6
29
1 It'snotaduplicateitjusthasthesamecodebutwithdifferentquestionsaskedaboutit!Inthe stackoverflow.com/questions/27675219/… Aquestionabouthowtomodifythecodeforgettingsomekindof resultisaskedbutthisquestionisaskingaboutsomepartsofcodebeinghardtounderstand! – AliNfr Dec28'14at16:58 2 illformatted?!IhaveGotthiscodefromareliablesourceabooknamed THE FIRST 1 0 PROLOG PROGRAMMING CONTESTS howcanyoujudgeaboutthecodelikethis?It'sprologandit'shardto understandbutit'snotillformatted.IhadsomeotherquestionaboutthecodesoIpostedadifferent question.Sowhat'stheproblem? – AliNfr Dec28'14at17:02 3 Theindentationinthebookisimpeccable.Youhaveaddedextraconfusinglinesandbadindentationasin transpose/4 andmanymore. –false Dec28'14at17:22 4 Foranexplanatinoof , , ; ,and ->,see,forexample, ControlPredicates.Areyouattemptingtowork throughPrologcontestproblemswithoutfirstlearningthefundamentalsofthelanguagesyntax? –lurker Dec29'14at14:21 3 Dumpingthatmuchcodeandasking"howdisworks"isneveragoodwaytoaskaquestion.Also:youhave messeduptheidentation.Also:isyourquestionabout(Condition>Action;Else)?Orabouttransposing? Oraboutunification? –Boris Jan7'15at5:28
3Answers
InadditiontothecorrectanswersofJoshandAviTshuvastatingthat a -> b ; c islike"ifa thenbelsec",Iwouldliketoexplainthat -> and ; areindividualoperatorswhichcanbe usedseparately. ; islogicaldisjunction,ie.logical"or".So x; y means"xory".Thismakestheconditional statementabitconfusingbecause a -> b ; c readslike"aimpliesborc"whichisobviously not whatitmeans!Evenifyouparenthesizeitlike"(aimpliesb)orc"yougetadifferent meaningfromtheconditionalstatementbecauseinthisincorrectinterpretation,cwillalways betried,evenif(aimpliesb)succeeds.
Thedifferenceisbecause
->
hassome"nonlogical"semantics.FromSWIPrologdocs :
:Condition>:Action IfthenandIfThenElse.The ->/2 constructcommitstothechoicesmadeatitslefthand side,destroyingchoicepointscreatedinsidetheclause(by ;/2 ),orbygoalscalledbythis clause.Unlike !/0 ,thechoicepointofthepredicateasawhole(duetomultipleclauses)is notdestroyed.Thecombination ;/2 and ->/2 actsasifdefinedas: If -> Then; _Else :- If, !, Then. If -> _Then; Else :- !, Else. If -> Then :- If, !, Then.
http://stackoverflow.com/questions/27677596/how-this-prolog-crossword-solver-works
2/3
10/2/2016
How this prolog crossword solver works? - Stack Overflow
Pleasenotethat (If -> Then) actsas (If -> Then ; fail) ,makingtheconstructfailif theconditionfails.ThisunusualsemanticsispartoftheISOandalldefactoProlog standards. (notethatintheabovequote, If , Then etc.arevariables!) Sobewareofanythingwithanimplicitcut ! answeredJan8'15at0:50
HughAllen 4,878
->
and
;
1
20
39
areProlog'scontrolflow,liketheifthen else satementinotherlanguages.So:
transpose(N,Puzzle,Acc,TransposedPuzzle) :( N == 0 -> TransposedPuzzle = Acc ; nth_elements(N,Puzzle,OneVert), M is N - 1, transpose(M,Puzzle,[OneVert|Acc], TransposedPuzzle) ).
translatestopsuedocode: def transpose(N, Puzzle, Acc) if N == 0 return Acc else OneVert = nth_elements(N, Puzzle) transpose(N-1, Puzzle, [OneVert, Acc])
or: def transpose(N, Puzzle, Acc) while N > 0 OneVert = nth_elements(N, Puzzle) Acc = [OneVert, Acc] N = N - 1 return Acc
Thatshouldgiveyousomeideawhatitdoes.Isuggestyoutranslatethe more_empty function intopsuedocodeyourself(orjuststepthroughitinyourhead),andtrytoworkitoutfromthere. answeredJan7'15at17:41
Zaz 13.4k
6
37
65
Prologisnotoneofmystronglanguages,soapologiesifI'vemadeanymistakes. – Zaz Jan7'15at17:43
TheseareProlog'sifthenelsecontrolstructure. Thesyntaxisasfollows: condition>thenstatements/decelerations;else statements/declerations answeredJan7'15at9:42
AviTshuva 81
6
http://stackoverflow.com/questions/27677596/how-this-prolog-crossword-solver-works
3/3...