X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=assignment1.mdwn;h=ea8f2511992e97ff0caed2f8f002426dd2bc053b;hp=f0d858d6424a03e1afb1ce117409d10d6634ab28;hb=513394b4086fc02f9f8214c52d3e8bbaa9cdfd27;hpb=2ab239e41e823b7338c5c90be884ee3471e2099d diff --git a/assignment1.mdwn b/assignment1.mdwn index f0d858d6..ea8f2511 100644 --- a/assignment1.mdwn +++ b/assignment1.mdwn @@ -26,7 +26,7 @@ Your solution should have a form something like this: - let + letrec drop match lambda (n, xs). FILL_IN_THIS_PART in drop @@ -50,7 +50,7 @@ Here's a way to answer this problem making use of your answers to previous questions: - let + letrec drop match ... ; # as in problem 4 take match ... ; # as in problem 5 split match lambda (n, xs). let @@ -80,7 +80,7 @@ 10. Write a function `map` that generalizes `double`. This function expects a pair of arguments, the second being a sequence `xs` with elements of some type *t*, for example numbers. The first argument will be a function `f` that itself expects arguments of type *t* and returns some type *t'* of result. What `map` should return is a sequence of the results, in the same order as the corresponding original elements. The result should be that we could say: - let + letrec map match lambda (f, xs). FILL_IN_THIS_PART; double match lambda xs. map ((lambda x. 2*x), xs) in ... @@ -90,11 +90,29 @@ map2 ((lambda (x,y). 10*x + y), [1, 2, 3], [4, 5, 6]) # evaluates to [14, 25, 36] -EXTRA CREDIT PROBLEMS +###Extra credit problems### + +* In class I mentioned a function `&&` which occupied the position *between* its arguments, rather than coming before them (this is called an "infix" function). The way that it works is that `[1, 2, 3] && [4, 5]` evaluates to `[1, 2, 3, 4, 5]`. Define this function, making use of `letrec` and the simpler infix operation `&`. + +* Write a function `unmap2` that is something like the inverse of `map2`. This function expects two arguments, the second being a sequence of elements of some type *t*. The first is a function `g` that expects a single argument of type *t* and returns a *pair* of results, rather than just one result. We want to collate these results, the first into one list, and the second into a different list. Then `unmap2` should return those two lists. Thus if: + + g z1 # evaluates to [x1, y1] + g z2 # evaluates to [x2, y2] + g z3 # evaluates to [x3, y3] + + Then `unmap2 (g, [z1, z2, z3])` should evaluate to `([x1, x2, x3], [y1, y2, y3])`. + +* Write a function `takewhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this: + + takewhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [1, 2] + + Note that we stop "taking" once we reach `20`, even though there are still later elements in the list that are less than `10`. + +* Write a function `dropwhile` that expects a `p` argument like `filter`, and also a sequence. The result should behave like this: -*Will post shortly* + dropwhile ((lambda x. x < 10), [1, 2, 20, 4, 40]) # evaluates to [20, 4, 40] - + Note that we stop "dropping" once we reach `20`, even though there are still later elements in the list that are less than `10`. - +* Write a function `reverse` that returns the reverse of a sequence. Thus, `reverse [1, 2, 3, 4]` should evaluate to `[4, 3, 2, 1]`.