Commit 644cf7a3 by Marius Frinken

marius' stuff for the third tutorial session

parent 4e713c13
 %sometimes you may want to "tag" things onto a existing data structure % e.g. process(ListOfTriples) needs a list of triples %but you only have a list of pairs process(ListOfTriples):- %does something write(ListOfTriples). %now we need to merge .i.e tag on a dummy value 0 in our naive case here tag_on([(A,B)],[(A,B,0)]). tag_on([(A,B)|Tail],ListOfTriples):- tag_on(Tail,TempListOfTriples), ListOfTriples = [(A,B,0)|TempListOfTriples],!. tag_and_process(List):- tag_on(List,NewList), process(NewList). %Prolog offers multiple ways of sorting, this is one comp_triples(Delta, (C1,_,_),(C2,_,_)):- compare(Delta,C1,C2). my_sort(ListOfTriples, SortedListOfTriples):- predsort(comp_triples,ListOfTriples,SortedListOfTriples). %testcase test_my_sort(X):- my_sort([(29,"E","E"),(13,"E","E"),(54,"E","E"),(9,"E","E")],X). %normal lists can bes sorted with sort(List,SortedList) %interface for your homework: % dfs(GoalValue, Tree, PathString, Cost) % % GoalValue is a String e.g. "G" % Tree is a tree e.g. tree("A",[(1,tree("B",[]))]) % PathString shall contain the correct path from the beginning straight to the goal in the end % if you do not like strings, you may implement path as list % Cost is a number that shall contain the summed up cost in the end %Only the iterative deepening differs from that interface, as we have to give a Stepsize: % idfs(GoalValue,Tree,PathString,Cost,StepSize) % also please use write or any other form of writing to show the order in whch your code expands the nodes % that helps you and us to determine whether you code actually performs a BFS, DFS and so on % (and you could verify your answer to 3.1 with that technique)