

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Homework 7 CIS 252 (Spring 2009) — Introduction to Computer Science
This assignment focuses on material in Chapters 9 and 10 of Haskell: The Craft of Functional Programming.
This homework is officially due in the bin in CST 3-212 by noon on Thurs- day, March 26. However, it comes with an automatic extension: anything submitted by noon on Friday, March 27 will be accepted as being on time.
You may work singly or in pairs on this assignment.
As always, you’re expected to follow the design recipe discussed in lec- ture.
Turn in a hard copy of your source code and a transcript demonstrating convincingly that your code is correct. Also, submit your code (but not transcript) electronically.
applyEach :: [a -> b] -> a -> [b] applyEach fs v = map _________________________________
such that applyEach fs v returns the list obtained by applying each element of fs to v in turn. For example, your function should have the following behavior:
Main> applyEach [ (\ x -> x* 5), ((+) 100)] 2000 [10000,2100] Main> applyEach [id, reverse, tail] "hello" ["hello","olleh","ello"]
isPrefix :: String -> String -> Bool
such that isPrefix s1 s2 returns True if s1 is a prefix of s2, and returns False otherwise. For example:
Main> isPrefix "cis" "cis352" True Main> isPrefix "cisw" "cis352" False
makePreFuns :: [String] -> [String -> Bool] makePreFuns ss = map _________________________________
such thatmakePreFuns strings returns a list of functions, where the ith^ function in that list has the following behavior:
It accepts a string s and returns True or False, indicating whether or not s is a prefix of the ith^ element of strings.
Note: You can use applyEach to test this function. For example, your function should have the following behavior:
Main> applyEach (makePreFuns ["hi", "hello", "nowhere"]) "h" [True,True,False] Main> applyEach (makePreFuns ["hi", "hello", "nowhere"]) "he" [False,True,False] Main> applyEach (makePreFuns ["hi", "hello", "nowhere"]) "n" [False,False,True]
altSum :: [Int] -> Int altSum ns = foldr ___________ ___________ ns
Page 1
Homework 7 CIS 252 (Spring 2009) — Introduction to Computer Science
such that altSum [a 1 , a 2 , ..., an] computes the alternating sum a 1 − a 2 + a 3 − a 4 +... an. For example, altsum [8,2,6,3] should return 9, because 8 − 2 + 6 − 3 = 9.
filter’ :: (a -> Bool) -> [a] -> [a] filter’ p xs = foldr g [] xs where g x r | _________ = __________________ | otherwise = __________________
For the purposes of this question, we’ll limit ourselves to polyno- mials whose coefficients are integers, and introduce the following type synonym (be sure to include it in your file):
type Poly = [Float]
Use foldr to write a Haskell function
evalPoly :: Float -> Poly -> Float
such that evalPoly m p returns the value of the polynomial p when x is m. For example, evalPoly 3 [1,0,4,2,6] should return 577 (1 + 0 ∗ 3 + 4 ∗ 32 + 2 ∗ 33 + 6 ∗ 34 = 577). Specifically, your code should look like the following, with each of the blanks filled in with an argument to foldr:
evalPoly :: Float -> Poly -> Float evalPoly m xs = foldr h ________ ___________ where h ____ _____ = _____________
Hint: Horner’s rule uses repeated factoring as an efficient way to evaluate a polynomial at a given point, based on the following equality:
a 0 + a 1 x + a 2 x^2 + · · · + an xn = a 0 + x · (a 1 + x · (a 2 + x · (a 3 + · · · + x · an)) · · ·) = a 0 + x · (a 1 + x · (a 2 + x · (a 3 + · · · + x · (an + x · 0 ))) · · ·)
(See http://en.wikipedia.org/wiki/Horner_scheme.)
Page 2