Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the functions in Exercises.hs. There are 17 of these (labeled like #1), worth 5 points each. The first five concern Functor, and mostly require

Implement the functions in Exercises.hs. There are 17 of these (labeled like #1), worth 5 points each. The first five concern Functor, and mostly require implementing fmap for types I define. The remaining problems are about Applicative. Some are about writing functions that work for any Applicative, while others are about using particular Applicatives to solve some problems. For that latter group of problems, you are strongly encouraged but not required to make use of the operations that come with an Applicative functor, rather than working directly (i.e., via pattern-matching) on elements in applications of the functor.

---------------------------------------------------------------------------------Exercises.hs--------------------------------------------------

module Exercises where

---------------------------------------------------------------------- -- Functors

data MyMaybe a = MyNothing | MyJust a deriving (Eq , Show)

{- see Section 8.5 (page 99) of the Hutton book for more on instance declarations. Note that if your instance declaration is declaring the value for some method of the type class, like the fmap method of the Functor type class, then you can implement it with a recursive function right there in the instance declaration. The starting code below is an example: -}

-- #1 instance Functor MyMaybe where fmap f MyNothing = MyNothing fmap f (MyJust x) = MyJust (f x)

-- same as in Inclass.hs for live coding, week 3 data BinTree a = Leaf | Node a (BinTree a) (BinTree a) deriving (Eq , Show)

singleton :: a -> BinTree a singleton x = Node x Leaf Leaf

e1 = Node 1 (singleton 2) (Node 3 (singleton 3) (singleton 4))

-- #2 instance Functor BinTree where fmap f Leaf = Leaf fmap f (Node x l r) = Node (f x) (fmap f l) (fmap f r)

{- imagine that we want to keep a list of the values stored in a tree, as well as the tree structure itself. ValTree does this . -} data ValTree a = ValTree [a] (BinTree a) deriving (Eq , Show)

-- #3 instance Functor ValTree where fmap f xs = ((fmap f) xs) -- trees with data at the leaves, and allowing an empty tree data Tree a = Lf | Nd a [Tree a] deriving (Eq, Show)

e2 = Nd 1 [Nd 2 [],Lf,Nd 3 [Nd 4 []]]

-- #4 instance Functor Tree where fmap = undefined

{- For a call (a1 b d x):

If the b is True, then replace all the data under the functor in d with x. If b is False, just return d.

#5 -} a1 :: Functor f => Bool -> f a -> a -> f a a1 = undefined

---------------------------------------------------------------------- -- Applicatives

{- conjoin the booleans inside the given inputs. So a2 (Just True) (Just True) would be Just True, for the example where the applicative f is Maybe.

#6 -} a2 :: Applicative f => f Bool -> f Bool -> f Bool a2 b1 b2 = pure (&&) b1 b2

{- given data with functions under application functor f, return new data composing those functions.

#7 -} a3 :: Applicative f => f (b -> c) -> f (a -> b) -> f (a -> c) a3 = undefined

---------------------------------------------------------------------- -- Applicative example: logging

{- a value of type 'Logging a' is list of Strings together with an a. The list of Strings is the log. The intention is to use it as a list of log messages, generated during execution of some function -} data Logging a = Logging [String] a deriving (Show , Eq)

-- addToLog s l should return a new Logging a which has s at the front of the log -- #8 addToLog :: String -> Logging a -> Logging a addToLog = undefined

instance Functor Logging where fmap = undefined

-- when combining two Logging values, the logs should be concatenated -- #9 instance Applicative Logging where pure v = undefined (Logging l1 v1) (Logging l2 v2) = undefined

{- convert a list of Booleans to an Integer, by interpreting that list as a string of bits from least to most significant, and computing the number this bit string represents. The Integer input is the current exponent to use for exponentiating 2. You should log the addends, including 0 when the bit is 0, that you compute along the way to a final answer.

For example, the bit string

[True,False,True,True]

represents the binary number 1101 (the bit string is stored with least significant bit at the head of the list).

If you run convBin with Integer input 0, then you will generate

1 + 0 + 4 + 8

which evaluates to 13. You are supposed to log those addends, in the order listed. So the log should end up as ["1","0","4","8"] for this case.

#10 -} convBin :: [Bool] -> Integer -> Logging Integer convBin = undefined

---------------------------------- -- another Logging example

{- The datatype 'Command a' describes the set of two instructions we will perform on a value of type a: print a String (to the log) and apply a function of type a -> a to the value. -} data Command a = Print String | Op (a -> a)

type CommandList a = [Command a]

{- interpret a Command as a Logging (a -> a). If the command is Print s, then you add s to the log. If the command is Op f, then you use 'pure' to make that f be the value (of type 'a -> a') stored in the Logging.

#11 -} processCommand :: Command a -> Logging (a -> a) processCommand = undefined

{- given any Applicative f, compose a list of functions under f into a single function under f.

#12 -} composeA :: Applicative f => [f (a -> a)] -> f (a -> a) composeA = undefined

{- using (I suggest) processCommand and composeA, turn a list of commands and a value of type a into a Logging a, where you (with the help of processCommand) apply the functions in Op commands to the starting value, and you add the strings in Print commands to the log

#13 -} process :: CommandList a -> a -> Logging a process = undefined

---------------------------------------------------------------------- -- Applicative example: accumulating

{- This is like Logging, except that now we are accumulating a list of value of some type b (instead of a list of Strings). Let us call this list the accumulation. -} data Accum b a = Accum [b] a deriving (Show , Eq)

-- addToLog s l should return a new Accum a which has s at the front of the accumulation -- #14 accum :: b -> Accum b a -> Accum b a accum = undefined

-- #15 instance Functor (Accum b) where fmap = undefined

-- when combining two Accum values, the accumulations should be concatenated -- #16 instance Applicative (Accum b) where pure = undefined () = undefined

{- this function interprets the given list of Bools as a path in the tree. True means go left, False means go right.

Follow the given path in the given tree. Return the subtree you reach, accumulating the list of values stored at the nodes along the path. If the tree ends before the path does, the subtree reached is considered to be Leaf.

#17 -} followPath :: [Bool] -> BinTree a -> Accum a (BinTree a) followPath = undefined

--------------------------------------------------------------------------------PublicTest.hs----------------------------------------------------------------------------

module PublicTests where

import Tests import Exercises

t1 :: MyMaybe Integer t1 = MyJust 7 r1 = MyJust 70

t2 :: BinTree Integer t2 = Node 1 (Node 2 Leaf Leaf) (Node 3 (Node 4 Leaf Leaf) Leaf) r2 = Node 10 (Node 20 Leaf Leaf) (Node 30 (Node 40 Leaf Leaf) Leaf)

t3 :: ValTree Integer t3 = ValTree [1,2,3,4] t2 r3 = ValTree [10,20,30,40] r2

t4 :: Tree Bool t4 = Nd True [Nd False [], Nd False [Nd True []], Nd True []] r4 = Nd False [Nd True [], Nd True [Nd False []], Nd False []]

functorTest :: (Functor f, Eq (f b), Show (f b)) => String -> (a -> b) -> f a -> f b -> IO () functorTest s f t r = test ("fmap for " ++ s) (f t) r

main :: IO () main = runTests [ functorTest "MyMaybe" (*10) t1 r1, functorTest "BinTree" (*10) t2 r2, functorTest "ValTree" (*10) t3 r3, functorTest "Tree" not t4 r4, test "a1, 1" (a1 True (Just True) False) (Just False), test "a1, 2" (a1 False (Just True) False) (Just True), test "a2" (a2 (Just True) (Just True)) (Just True), test "a3" (zipWith ($) (a3 [(+4),(+5)] [(*10),(*100)]) [1,2,3,4]) [14,204,35,405], test "addToLog" (addToLog "hi" (Logging [] 3)) (Logging ["hi"] 3), functorTest "Logging" (*10) (addToLog "hi" (pure 5)) (Logging ["hi"] 50), test "Applicative Logging" ((addToLog "hi" $ pure not) (addToLog "bye" $ pure True)) (Logging ["hi","bye"] False), test "convBin" (convBin [True,False,True,True] 0) (Logging ["1","0","4","8"] 13), test "processCommand" (processCommand (Print "hi") (pure 1)) (Logging ["hi"] 1), test "composeA" (composeA (map (Just . (+)) [10,100,1000]) pure 1) (Just 1111), test "process" (process [Print "hi" , Op (* 10), Print "next", Op (+ 3)] 1) (Logging ["hi","next"] 40), test "accum" (accum "foo" (Accum ["bar"] True)) (Accum ["foo", "bar"] True), test "followPath" (followPath [False,False] e1) (Accum [1,3] (Node 4 Leaf Leaf)) ]

Here is the prompt file. image text in transcribed
PLC: Workout 4 [85 points) Dw date: Satunday, February 2x by mulighet About This Homework This worker Puctor and Applicative How to Turn In Your Solution be you plan to Cox, you first put all your file in folder where your HD (let's refer to your webiofokle). There this folder to create a tipike This the file you should ICON. The files you put your folder for the Exercise partners, txt, if you worked with a purtauet (we west estica) Partners Allowed You may work with parts for them. If you do te of you want your kell code Bub of you code file called partners.txt, listing both your H. In the det. This will help us confirm that you may dalk the How To Get Help You can post in the crown of ICON Yo wo wo to towardices. See the Google Calinked from theith for the law for times online for office br. You can find then the office hou ICON https://www.astructure.com/course/153002/pagesova-linka-office-hours 1 Reading Raul Section 12.1 and 122 of the treated leck, Programming meint by Gulumu Hutton, 2 Exercises on Functor and Applicative (85 points) Implement the functions in Exercises. The 17 of the label) worth points wch. The first five Punctur del temple in fap for testine The main problemes are also Applicative. Sot writing for that weke y applicative, while others are short wine partial Applicatives to some problem For the latter group of peol yowestrogly encouraghett required to make of the operates that come with an Applicative factors than working directly in, vi puutteematon element in application of the factor PLC: Workout 4 [85 points) Dw date: Satunday, February 2x by mulighet About This Homework This worker Puctor and Applicative How to Turn In Your Solution be you plan to Cox, you first put all your file in folder where your HD (let's refer to your webiofokle). There this folder to create a tipike This the file you should ICON. The files you put your folder for the Exercise partners, txt, if you worked with a purtauet (we west estica) Partners Allowed You may work with parts for them. If you do te of you want your kell code Bub of you code file called partners.txt, listing both your H. In the det. This will help us confirm that you may dalk the How To Get Help You can post in the crown of ICON Yo wo wo to towardices. See the Google Calinked from theith for the law for times online for office br. You can find then the office hou ICON https://www.astructure.com/course/153002/pagesova-linka-office-hours 1 Reading Raul Section 12.1 and 122 of the treated leck, Programming meint by Gulumu Hutton, 2 Exercises on Functor and Applicative (85 points) Implement the functions in Exercises. The 17 of the label) worth points wch. The first five Punctur del temple in fap for testine The main problemes are also Applicative. Sot writing for that weke y applicative, while others are short wine partial Applicatives to some problem For the latter group of peol yowestrogly encouraghett required to make of the operates that come with an Applicative factors than working directly in, vi puutteematon element in application of the factor

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 17th International Conference Dasfaa 2012 Busan South Korea April 2012 Proceedings Part 1 Lncs 7238

Authors: Sang-goo Lee ,Zhiyong Peng ,Xiaofang Zhou ,Yang-Sae Moon ,Rainer Unland ,Jaesoo Yoo

2012 Edition

364229037X, 978-3642290374

More Books

Students also viewed these Databases questions

Question

4. Devise an interview strategy from the interviewers point of view

Answered: 1 week ago