Csc 4330/6330, Programming Language Concepts (Spring 2018)

LISP Evaluator (Due: 29 April (Sunday))

Write a Prolog program (lisp.pl) to evaluate LISP expressions, somewhat like those in HW1.

LISP expressions will be represented as Prolog function terms. Here are some sample LISP expressions:

34
+(20,30)
*(+(1,2),/(8,4))
*(car([2,4,+(2,4),8]),/(27,9))
+(car([2,3,4]),car(cdr(cdr([9,8,7,6]))))
LISP expressions as Prolog terms are defined as follows:
  1. A number is a LISP expression.
  2. If E1 and E2 are LISP expressions, then so are
    1. +(E1,E2)
    2. -(E1,E2)
    3. *(E1,E2)
    4. /(E1,E2)
  3. If L is a Prolog list of LISP expressions then
    1. car(L) is a LISP expression.
    2. cdr(L) is a Prolog list of LISP expressions.

Here is a sample run of the program:

MacBook-Pro:prolog raj$ swipl
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.26)
Copyright (c) 1990-2016 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- ['lisp.pl'].
true.

?- evalLISP(34,N).
N = 34.

?- evalLISP(+(20,30),N).
N = 50.

?- evalLISP(*(+(1,2),/(8,4)),N).
N = 6.

?- evalLISP(*(car([2,4,+(2,4),8]),/(27,9)),N).
N = 6 .

?- evalLISP(+(car([2,3,4]),car(cdr(cdr([9,8,7,6])))),N).
N = 9 .

?- halt.
MacBook-Pro:prolog raj$