Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 581: Programming Languages Winter 2005: Dr. Burnett Homework #3: Haskell and functional definitional programming Due Date: beginning of class, Mon., 1/31 Work out solutions for the problems below and run them using Haskell. Turn them in electronically -- by emailing a single source file (ASCII) to [email protected], except for the last problem which you can turn in via hardcopy or a separate electronic file. Error-checking: Error-checking should be for appropriate values. In this and further assignments, unless otherwise indicated, you are not expected to check for the correct number of arguments, or for the correct type. For example, if you were writing FACTORIAL, you would check to be sure the argument was greater than zero, but not if it was actually a number. Functional programming style: In this and further assignments, unless otherwise indicated all problems must be programmed using a functional style. (We are not trying to write C programs using Lisp syntax). Therefore, even if you are brave enough to wander into monadic functions such as “do”, you cannot use them. 1. [5 pts] You’ve seen this one before in Lisp. Now do it in Haskell. Here’s the description again for you: Write a function CADADR which, given a list with enough elements to prevent running out of them at an inconvenient time, returns the car of the cdr of the car of the cdr of the list. 2. [10 pts] You’ve seen these before in Lisp also. Now write MY-APPEND and MYMEMBER in Haskell. They need to be defined for lists only (not strings or other sequence types), and need to work with exactly 2 arguments. Don’t use built-ins that provide these functions. Also, you might be able to find clever ways to write them, but please don’t -- just stick with the straightforward recursive approach in this problem. 3. [5 pts] (a) Write a function that makes a copy of a list. (b) In Haskell, is there ever any reason to use such a function? If yes, give an example. If no, say why not. (Put the answer into your file as a comment.) 4. [20 pts] (a) Write a function that removes the nth element of a list. Use recursion. (b) Could this be done instead using map? If yes, provide the call that does this. If no, say why map cannot be used for this (as a comment in your file). (c) Could this be done instead using filter? If yes, provide the call that does this. If no, say why filter cannot be used for this (as a comment in your file). (d) Could this be done instead using fold? If yes, provide the call that does this. If no, say why fold cannot be used for this (as a comment in your file). 5. [5 pts] Prove that if f is associative and st is an identity for f, then: foldr f st (x ++ y) = f (foldr f st x) (foldr f st y) For example, if f were (+) and st were 0, you would be looking at something like: foldr (+) 0 ([1, 2] ++ [3, 4, 5]) = (+) (foldr (+) 0 [1, 2]) (foldr (+) 0 [3, 4, 5])