![]() |
![]() |
|
home—lectures—exams—hws—breeze (snow day)
Due Nov.30 (Wed) 23:59 I will accept #4 by Friday night only if you submit #1-3 on Wednesday night.
We continue to build on the language implementation started in hw07. You can implement this homework in either Java or Racket. Please indicate in your submitted file, what sections of code are udpated and what is unchanged from hw07/hw07-soln. You don't need to turn in any hardcopy of unchanged-code (but submit a fully-working copy in the drop-box).
Write the following Prolog predicates.
Do not use
Note that xsb Prolog contains several list functions which you are NOT to use for this assignment
(e.g.
As ever, your program should follow good style, including appropriate white space, meaningful variable names, and as well as a header comment with your name, the name of the assignment, etc.. (You can have comments describing how a predicate works if you want; however, you can also assume your program is being read by somebody who understands the fundamentals of Prolog.)
N3 is just like N2, except we now
allow one variable to shadow another.
For example, we might have a
Thus, when substituting,
only substitute “free occurrences” of an
(5pts)
Fill in the following blanks:
Rewrite (that is: re-indent)
the following two N3 expressions so that
each
let x = 5 in (let x = (x plus 1) in (x plus 2);); let y = let z = 4 in (let y = 99 in z;); in (let z = 5 in ((let z = 10 in y;) plus (y plus z));); |
You can put all your answers in comments near the top of your main file (N4.rkt or Expr.java)
(5pts)
Make the necessary changes to N2
to enable shadowing.
(You are encouraged to build on your solution,
but you can also use the posted solutions.)
(30pts) N4 adds (non-recursive) functions and function-application to our language:
Expr ::= … | FuncExpr | FuncApplyExpr FuncExpr ::= func(Id, Expr) FuncApplyExpr ::= <Expr(Expr)> |
Just like numbers are self-evaluating,
so are
In
func( x, if x isNeg then (-1 times x) else x;) |
A
<func( x, if x isNeg then (-1 times x) else x;)(-5)> let abs = func(x, if x isNeg then (-1 times x) else x;) in <abs(-5)>; |
Observe that our programs can now evaluate to either of
two types: numbers or functions.
In Java, we'll need to have a class which can represent
either,
as the return type for our
Make test cases for
(define (make-adder n) (lambda (m) (+ n m))) ; Two examples of applying this function: ; (make-adder 4) ; evals to (lambda (m) (+ 4 m)) ((make-adder 3) 4) ; evals to 7 |
Here are some examples of the list predicates, for the prolog question:
last([1,2,3], 3). Yes last([1,2,3], 4). No last([1,2,3], Y). Y=3 last([], Y). No last(Y, 3). Y=[3]. nextToLast([1,2,3], 2). Yes nextToLast([1,2,3], 3). No nextToLast([1,2,3], Y). Y=2 nextToLast([1], Y). No nextToLast(Y, 3). Y=[3, _h114], % does not have to be 114, 'course. Y=[_h116, 3, _h114]. lastTwoReversed([1,2,3], Y). Y=[3,2] lastTwoReversed([1], Y). No reverseLastTwo([1,2,3,4], Y). Y=[1,2,4,3] reverseLastTwo([1,2], Y). Y=[2,1] reverseLastTwo([1], Y). No
The interpreter project is based on the first chapters of Programming Languages and Interpretation, by Shriram Krishnamurthi. As a result, this homework assignment is covered by the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License. Although we're using a different dialect of racket than that book, you might find it helpful to skim it.
1
nor any “binding occurrences”:
The first
home—lectures—exams—hws—breeze (snow day)
©2011, Ian Barland, Radford University Last modified 2011.Nov.28 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |