STUDY GUIDE FOR THE COP 4020 MAKEUP EXAM (for exam 2) on Higher-Order Functional Programming $Date: 2013/04/13 21:55:10 $ This exam covers topics from homework 4, but in Erlang. For topics, see the study guide for exam 2 (exam2-topics.txt). If you are planning to take this test, but have not yet talked to Gary Leavens about your exam 2, then you need to talk with him first (bring your exam 2) to get help. You will only be permitted to take the makeup for exam 2 if you have talked to Gary in person or you bring evidence (on paper) that you have done the work below. The following are several suggestions for studying for the makeup exam. These will not be handed in, but you can see Gary or Gene for feedback on your solutions (even if you just want to check they are okay). 1. Make a translation chart that shows how one translates Haskell into Erlang. You should include items such as syntax, patterns, and built-in functions. You may want to think about two kinds of translations into Erlang; literally correct ones (that exactly respect the Haskell semantics, as far as possible), and idomatic ones (that are more normal for Erlang code). There is a start below, we suggest continuing this... Haskell Erlang -- function definition % function definition, exact translation add x y = x + y add(X) -> fun(Y) -> X + Y end. % function definition, idiomatic for Erlang add(X,Y) -> X + Y. -- full recursion on lists % full recursion len [] = 0 len([]) -> 0; len (_:xs) = 1 + len xs len([_|Xs]) -> 1 + len(Xs). -- function application % exact translation, assuming add is curried add 3 4 add(3)(4) % function application, idomatic for Erlang add 3 4 add(3,4) -- lambda expressions % fun expressions (\x -> 3 + g x) fun(X) -> 3 + g(X) end -- use of map % use of lists:map, idiomatic map (\x -> 3 + g x) xs lists:map(fun(X) -> 3 + g(X) end, Xs) -- use of foldr % use of lists:foldr, idiomatic foldr (\n res -> min n res) lists:foldr(fun(N,Res) -> min(N,Res) end, n ns N, Ns) 2. In Erlang, write solutions to the exam 2 problems, by hand, on paper. Then type them in to an Erlang file and get them to compile. Test them a bit with your own test data. Compare your solutions on paper to the corrected ones, and think about what you have to remember in terms of Erlang's syntax. Follow this idea of writing out the solutions on paper first, and then testing them in Erlang later, for all the following problems. 3. In Erlang, use Erlang's lists:foldr/3 function to define the map/2 function (but without using lists:map/2, and without using any explicit recursion or any list comprehension). 4. In Erlang, solve all of the programming problems on homework 4. 5. Write a chart giving the correspondence between the Haskell Type notation and Erlang's type notation (which you can find in the Erlang Reference Manual, see http://www.erlang.org/doc/reference_manual/typespec.html#id75245). 6. In Erlang, use Erlang's lists:foldr/3 function to define the filter/2 function (but without using lists:filter/2, and without using any explicit recursion or any list comprehension). 7. In Erlang, write the function curry/1, which takes a function F of type fun((S,T) -> U) and returns a curried version of F, that is, curry(F) returns a function CurriedF of type fun((S) -> fun((T) -> U)), such that ((CurriedF(AnS))(AT)) == F(AnS,AT) for all AnS of type S and AT of type T. 8. Generalize the SalesData examples found in the "Following the Grammar" handout and in homework 6, problem 2 by writing a functional abstraction of code that follows the Sales Data grammar in Erlang. Test it by using that abstraction to generate the functions that you generalized from.