![]() |
![]() |
|
home—lectures—exams—hws—breeze (snow day)
Due Nov.30 (Fri) at the start of class.
Submit:
a hardcopy with
and just the additional tests for N4
We continue to build on the language implementation started in hw08. 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 hw08/hw08-soln. You don't need to turn in any hardcopy of unchanged-code (but submit a fully-working copy in the drop-box).
(extra-credit, 5pts)
Generalize
Clearly,
Re-write
By the way, note that natural numbers have a structural definition (the same way lists and trees did): a natural number is either 0, or 1 added to some other (slightly smaller) natural number. Your code for processing a natural number should reflect this data definition, in the same way your code for processing a list or a Ancestor-tree or a Expression-tree all follow the shape of that data.
(26pts) N4 adds (non-recursive) functions and function-application to our language:
Expr ::= … | FuncExpr | FuncApplyExpr FuncExpr ::= func(Id, Expr) FuncApplyExpr ::= <Expr(Expr)> |
Here is an example of a function; it happens to compute a number’s absolute value:
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)>; |
To evaluate a function-application
Observe that our programs can now evaluate to either of two types:
numbers or functions.
In Java,
we'll need a class which can represent either,
as the return type for our
Make test cases for
Note: You won't be able to evaluate function-applications for recursive functions yet (see N5), but we can still write the test cases! (You can comment out that one test case for now, since it'll trigger a run-time exception otherwise.)
(define (make-adder n) (lambda (m) (+ n m))) ; Two examples of applying this function: ; (make-adder 3) ; evals to (lambda (m) (+ 3 m)) ((make-adder 3) 4) ; evals to 7 |
Observe:If you wanted, you could certainly writeapply-twice in N4.
Note that we're restricting N4 to only deal with unary functions (functions of one argument).
To think about: If we wanted to 'fake' functions of 2 arguments in N4, how could we do it? For example, you might think about how to write a function that effectively takes in two numbers i,j and returns 2·i+j. Think about howmake-adder does this.
The interpreter project is based on the first chapters of Programming Languages and Interpretation, by Shriram Krishnamurthi. As a result, this problem of the 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
Well, not quite “the opposite way”, since the input function might
loop forever or throw an exception, in which case
2 We of course use the standard definition of equality for functions: “f = g” means ∀x f(x)=g(x). ↩
home—lectures—exams—hws—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2012.Dec.03 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |