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:
- A number is a LISP expression.
- If E1 and E2 are LISP expressions, then so are
- +(E1,E2)
- -(E1,E2)
- *(E1,E2)
- /(E1,E2)
- If L is a Prolog list of LISP expressions then
- car(L) is a LISP expression.
- 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$