On this page:
5.1 Time Estimate
5.2 Deliverables
8.14

5 Fun with Functions🔗

This assignment is designed to give you practice with variable bindings.

Submit a single Shplait file (with extension rhm).

5.1 Time Estimate🔗

This assignment should take you 30 minutes to 3 hours.

If you are struggling to complete this assignment in this amount of time, please seek TA or instructor help.

5.2 Deliverables🔗

Start from your interpreter with environments and let bindings.

Submit a single Shplait file (with extension rhm), that supports the following additional language features.


exp

 = 

... | let*:

        id_0 = exp_0

        ...

        id_n = exp_n

      exp

A let* expression binds multiple variables in order, with previous bindings being in scope for later ones. For example

let*:

  x = 1

  y = x + 1

x + y

should evaluate to 2.

def

 = 

fun id(id = exp): body

 

exp

 = 

... | id()

Add default parameters to functions. If a parameter is not provided when calling the function, the default expression should be evaluated in the function’s environment.

If a parameter is not provided and there is no default expression, an "invalid number of arguments" error should be thrown.

For example, interp(parse('f()'), mt_env, [parse_fundef('fun f(x = 0): x + 1')]), should evaluate to 1.

exp

 = 

... | id(id = exp)

Add named parameters to function calls. If a parameter is named in a function call, and the function being called doesn’t have a parameter by that name, an "invalid named argument" error should be thrown. Otherwise the expression should be bound.

For example, interp(parse('f(x = 1)'), mt_env, [parse_fundef('fun f(x): x + 1')]) should evaluate to 2, but the interp(parse('f(y = 1)'), mt_env, [parse_fundef('fun f(x): x + 1')]) should throw an error containing "invalid named argument".

Your final interpreter should support all three of these operations. Ensure you have adequate tests to cover both successful and error cases.