Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- targil 1
- a) type evrything x::zzzz
- b) try to be functional
- Look up group in hoogle
- 1) Implement by yourself
- 2) Find n’th generation in the sequence
- 1 / 1 1 / 2 1 / 1 2 1 1/ … and its histogram
- And /Or/Not tree
- Inner nodes And /Or/Not
- And/Or arity >=2 Not arity 1
- Leaf nodes Bool
- 3) Make instance of Eq,Show
- 4) eval:: booleanTree -> Bool
- targil 2
- 1)
- For those who still like imperative programming
- for :: a -> (a -> Bool) -> (a -> a) -> (a -> IO ()) -> IO ()
- An example of how this function would be used might be
- for 1 (<5) (+1) print
- prints the numbers 1 to 4 on the screen.
- The desired behavior of for is: starting from an initial value i, for executes job i. It then uses f to modify this value and checks to see if the modified value f i satisfies condition p. If it doesn't, it stops; otherwise, the for loop continues, using the modified f i in place of i.
- 2)
- Write your own binary numbers (not unary)
- and addition for them
- 3)
- Define a data type that is like a list but can have list of lists …
- (ordinary lists won’t allow this)
- Such as [ a, [[b],[c]],[d],[e]]
- and write a function to flatten it
- [a,b,c,d,e]
- targil 3
- 1) The Goldbach Conjecture is a yet unproven conjecture stating that every even integer greater than two is the sum of two prime numbers. The conjecture has been tested up to 400,000,000,000,000. Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics.
- Test upto 1000
- 2) Defined a graph as a set of nodes and a set of edges, where each edge is a pair of nodes.
- Test bi-partiteness
- 3) Understand (how they work, what they do, their types ...) and internalize the following functions
- map f = foldr ((:) . f) []
- filter pred = foldr ((++) . sel) []
- where
- sel x
- | pred x = [x]
- | otherwise = []
- reverse = foldl (flip (:)) []
- foldr op u xs = foldl (flip op) u (reverse xs)
- pascal = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1]
- import Control.Monad
- powerSet :: [a] -> [[a]]
- powerSet = filterM $ const [True, False]
- myInits = map reverse . scanl (flip (:)) []
- targil 4
- For each of 1-4 write a function that passes compilation, (instead of error)
- class Fluffy f where
- furry :: (a -> b) -> f a -> f b
- newtype EitherLeft b a = EitherLeft (Either a b)
- newtype EitherRight a b = EitherRight (Either a b)
- -- Exercise 1
- instance Fluffy (EitherLeft t) where
- furry = error "todo"
- -- Exercise 2
- instance Fluffy (EitherRight t) where
- furry = error "todo"
- class Misty m where
- banana :: (a -> m b) -> m a -> m b
- unicorn :: a -> m a
- -- Exercise 3
- -- (use banana and/or unicorn)
- furry' :: (a -> b) -> m a -> m b
- furry' = error "todo"
- newtype State s a = State {
- state :: (s -> (s, a))
- }
- -- Exercise 4
- instance Misty (State s) where
- banana = error "todo"
- unicorn = error "todo"
- fix :: (a -> a) -> a
- fix f = let {x = f x} in x
- fixed point instead of recursion
- -- Exercise 5
- write map using fix
- writer monad
- -- Exercise 6
- add a trace
- targil 5
- 1) given 2 categories and a mapping f between them
- decide if f is a functor
- 2) use parsec to write a lambda expression interpreter
- if needed use simplifying assumptions so as not to check too many cases
- and worry about too much syntax
- Last targil
- This is the targil, (excluding final project)
- 1) to write an interpreter for typed lambda calculus
- kiss, namely only a single base type, very few error messages ...
- a) use parsec
- b) need a type checker
- c) then if passes test, run
- use of the internet is recommended for ideas on how, but you will have to simplify |
- quiz 1 (MY SOL ONLY)
- ---------------1----------------------
- data ColorTree a = Empty | Node a [ColorTree a]
- r = Node "Red" [Node "Blue" [Empty], Node "Green" [Empty], Node "Red" [Empty]]
- l = Node "Red" [Node "Blue" [Empty], Node "Red" [Empty]]
- t = Node "Blue" [l,r]
- --------------2-----------------------
- isGray :: ColorTree String -> Bool
- isGray Empty = False
- isGray x = (elem' "Red" x) && (elem' "Blue" x) && (elem' "Green" x)
- elem' :: String -> ColorTree String -> Bool
- elem' _ Empty = False
- elem' m (Node c x) = c == m || or (map (elem' m) x)
- -------------3------------------------
- map' :: (a->b) -> [a] ->[b]
- map' f [] = []
- map' f [x] = foldr (\_ b -> b) [f x] []
- map' f (x:xs) = foldr (\_ b -> b) [f x] [] ++ map' f xs
- --changes:
- -- 1: didnt need to use the `$` operator [] was enough.
- -- 3: I was expacting that `foldr f x []` will return f(x)
- -- so i changed it to `foldr (\_ b -> b) [f x]`
- -- I think my grade should be 96 because the mistake in q1 was just a syntax problem (-1 point).
- -- And for q3 had a smaller change, but the logic of my answer wasn't changes at all (-3 points).
- -- I must say that altough I read the moodle implementetion before the i didnt remeber it excacly in the money time so i had to think of another way.And i always coming to the lectures :) !
- quiz 2
- class Fluffy f where
- furry :: (a -> b) -> f a -> f b
- 1.a
- instance Fluffy ((->) t) where
- furry = error "todo"
- 1.b
- write a program (not completely trivial) using furry of ->
- ----------------------------------------------------------------------------
- newtype State s a = State {
- state :: (s -> (s, a))
- }
- class Misty m where
- banana :: (a -> m b) -> m a -> m b
- unicorn :: a -> m a
- 2.a
- instance Misty (State t) where
- banana = error "todo"
- unicorn = error "todo"
- 2.b
- write a program (not completely trivial) using banana and unicorn of state
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement