3 ms·
It is hard to beat using Prolog for whipping out interpreters. Like Lisp, you get to use symbols in terms, plus you also get to use Prolog's ability to use inf
by GregBuchholz 10y ago
It is hard to beat using Prolog for whipping out interpreters. Like Lisp, you get to use symbols in terms, plus you also get to use Prolog's ability to use infix operators to create terms:
eval(X,X) :- number(X).
eval(X+Y,Z) :- eval(X,X1), eval(Y,Y1), Z is X1+Y1.
eval(X*Y,Z) :- eval(X,X1), eval(Y,Y1), Z is X1*Y1.
main :- write("Enter an arithmetic expression followed by a dot \".\" and a newline\n"),
prompt(_Old,>),
repl.
repl :- read(Input),
eval(Input,Answer),
print(Answer), nl,
repl.
You might like:
https://www.metalevel.at/lisprolog/ https://www.metalevel.at/lisprolog/
http://www.swi-prolog.org/ http://www.swi-prolog.org/
https://www.amazon.com/Art-Prolog-Advanced-Programming-Techniques/dp/0262192500/ https://www.amazon.com/Art-Prolog-Advanced-Programming-Techn...
- agumonkey 10y agoEvaluator; Parsers too. Prolog is an amazing idea (even with its limitation). But when you come from the bit array banging side of computer programming it's such an alien idea.
- GregBuchholz 10y agoFor those interested in parsing with Prolog, check out Definite Clause Grammars and The Craft of Prolog. http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse29 http://www.learnprolognow.org/lpnpage.php?pagetype=html&page... https://www.amazon.com/Craft-Prolog-Logic-Programming/dp/0262512270/ https://www.amazon.com/Craft-Prolog-Logic-Programming/dp/026...
- segmondy 10y agoJust a logically way of thinking. Many people don't know it, but Java is verified with prolog. https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.10 https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.ht...
- agumonkey 10y agoHaaaa I remember reading the spec, before I knew prolog .. didn't realize what I was reading. Neat.
- Immortalin 10y agoYou can always try Erlang's pattern matching..