Docsity

Homework 7 Resolution - Computer Science - 2009 | CIS 252, Assignments of Computer Science

Toggle sidebar
bg1
Homework 7 CIS 252 (Spring 2009) — Introduction to Computer Science
Coverage
This assignment focuses on material in Chapters 9 and 10 of Haskell: The
Craft of Functional Programming.
Logistics
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.
Exercises
1. (10 points) Fill in the blanks below to write a one-line function
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 vin 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"]
2. (10 points) Write a Haskell function
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
3. (10 points) Fill in the blank below to write a one-line function
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 sand returns True or False, indicating
whether or not sis 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]
4. (10 points) Fill in the blanks below to write a one-line function
altSum :: [Int] -> Int
altSum ns = foldr ___________ ___________ ns
Page 1
pf2

Partial preview of the text

Download Homework 7 Resolution - Computer Science - 2009 | CIS 252 and more Assignments Computer Science in PDF only on Docsity!

Homework 7 CIS 252 (Spring 2009) — Introduction to Computer Science

Coverage

This assignment focuses on material in Chapters 9 and 10 of Haskell: The Craft of Functional Programming.

Logistics

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.

Exercises

  1. (10 points) Fill in the blanks below to write a one-line function

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"]

  1. (10 points) Write a Haskell function

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

  1. (10 points) Fill in the blank below to write a one-line function

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]

  1. (10 points) Fill in the blanks below to write a one-line function

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.

  1. (20 points) Fill in the blanks below to write functions map’ and filter’ that compute the same functions as map and filter, re- spectively. map’ :: (a -> b) -> [a] -> [b] map’ f xs = foldr g [] xs where g x r = _______________________________

filter’ :: (a -> Bool) -> [a] -> [a] filter’ p xs = foldr g [] xs where g x r | _________ = __________________ | otherwise = __________________

  1. (10 points) Fill in the blanks below to write a one-line function composeList :: [a -> a] -> a -> a composeList fs = foldr ___________ ___________ fs such that composeList [ f 1 , f 2 ,..., fn] computes the function composition f 1 ◦ f 2 ◦... ◦ fn.
  2. (15 points) Every polynomial a 0 + a 1 x + a 2 x^2 + · · · + an xn^ can be represented as a list [a 0 , a 1 , a 2 , ..., an]. For example, 1 + 4 x^2 + 2 x^3 + 6 x^4 can be represented as the list [1,0,4,2,6].

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