0

This Prolog predicate, I believe, is meant to take a string, which is expressed as a list, a translate it into the accepting language of an fsm. This comes from my AI course, which does everything in Prolog, a language I'm not too experienced with:

str2fsm( [], [], [q0]).
str2fsm( [H|T], Trans, [Last]) :-
  mTL( T, [H], [[q0, H, [H]]], Trans, Last).

/* mTL( +More, +LastSoFar, +TransSoFar, ?Trans, ?Last) */

mTL( [], L, Trans, Trans, L).
mTL( [H|T], L, TransSoFar, Trans, Last) :-
  mTL( T, [H|L], [ [L,H,[H|L]] | TransSoFar], Trans, Last).

As I said, I'm quite new to Prolog and, despite my attempts to learn how to code with it, I'm simply not used to declarative programming, and understanding the reasoning behind the code is really difficult for me. Any insight as to how this code functions step-by-step would be greatly appreciated (or even a more general overview about how I should be thinking about coding in Prolog). Thanks!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
iaskdumbstuff
  • 409
  • 3
  • 16
  • 1
    You need to know about the [modes](https://www.swi-prolog.org/pldoc/man?section=modes) annotation (the one in the comment `%` as it has never made it into the language proper) and how to understand a clause (maybe my notes will help: [notes](https://github.com/dtonhofer/prolog_notes/blob/master/prolog_clause_explainer.md)). Use [SWISH](https://swish.swi-prolog.org/) to see how the above works. Insert [format](https://eu.swi-prolog.org/pldoc/doc_for?object=format/2) calls for printing out vars. Read online books like [Learm Prolog Now](http://www.learnprolognow.org/lpnpage.php?pageid=online). – David Tonhofer Apr 01 '20 at 07:29
  • see https://stackoverflow.com/questions/11539203/how-do-i-append-lists-in-prolog/11551001#11551001, https://stackoverflow.com/questions/10620011/explanation-of-a-prolog-algorithm-to-append-two-lists-together/10620282#10620282 as an example. – Will Ness Apr 01 '20 at 16:00
  • but you really should start from simpler examples. also, surely there are *some* explanations wherever this code came from? – Will Ness Apr 01 '20 at 16:33

0 Answers0