Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. targil 1
  2. a) type evrything x::zzzz
  3.  
  4. b) try to be functional
  5.  
  6.  
  7.  
  8. Look up group in hoogle
  9.  
  10. 1) Implement by yourself
  11.  
  12. 2) Find n’th generation in the sequence
  13.  
  14. 1 / 1 1 / 2 1 / 1 2 1 1/ … and its histogram
  15.  
  16.  
  17.  
  18. And /Or/Not tree
  19.  
  20. Inner nodes And /Or/Not
  21.  
  22. And/Or arity >=2 Not arity 1
  23.  
  24. Leaf nodes Bool
  25.  
  26. 3) Make instance of Eq,Show
  27.  
  28. 4) eval:: booleanTree -> Bool
  29.  
  30.  
  31. targil 2
  32. 1)
  33.  
  34. For those who still like imperative programming
  35.  
  36.  
  37.  
  38. for :: a -> (a -> Bool) -> (a -> a) -> (a -> IO ()) -> IO ()
  39.  
  40. An example of how this function would be used might be
  41.  
  42. for 1 (<5) (+1) print
  43.  
  44. prints the numbers 1 to 4 on the screen.
  45.  
  46.  
  47.  
  48. 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.
  49.  
  50.  
  51.  
  52. 2)
  53.  
  54. Write your own binary numbers (not unary)
  55.  
  56. and addition for them
  57.  
  58.  
  59.  
  60. 3)
  61.  
  62. Define a data type that is like a list but can have list of lists …
  63.  
  64. (ordinary lists won’t allow this)
  65.  
  66. Such as [ a, [[b],[c]],[d],[e]]
  67.  
  68. and write a function to flatten it
  69.  
  70. [a,b,c,d,e]
  71.  
  72. targil 3
  73.  
  74.  
  75. 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.
  76.  
  77. Test upto 1000
  78.  
  79. 2) Defined a graph as a set of nodes and a set of edges, where each edge is a pair of nodes.
  80.  
  81. Test bi-partiteness
  82.  
  83. 3) Understand (how they work, what they do, their types ...) and internalize the following functions
  84.  
  85.  
  86.  
  87. map f = foldr ((:) . f) []
  88.  
  89.  
  90.  
  91. filter pred = foldr ((++) . sel) []
  92.  
  93. where
  94.  
  95. sel x
  96.  
  97. | pred x = [x]
  98.  
  99. | otherwise = []
  100.  
  101.  
  102.  
  103. reverse = foldl (flip (:)) []
  104.  
  105.  
  106.  
  107. foldr op u xs = foldl (flip op) u (reverse xs)
  108.  
  109.  
  110.  
  111. pascal = iterate (\row -> zipWith (+) ([0] ++ row) (row ++ [0])) [1]
  112.  
  113.  
  114.  
  115. import Control.Monad
  116.  
  117. powerSet :: [a] -> [[a]]
  118.  
  119. powerSet = filterM $ const [True, False]
  120.  
  121.  
  122.  
  123. myInits = map reverse . scanl (flip (:)) []
  124.  
  125. targil 4
  126. For each of 1-4 write a function that passes compilation, (instead of error)
  127.  
  128.  
  129.  
  130.  
  131.  
  132. class Fluffy f where
  133.  
  134. furry :: (a -> b) -> f a -> f b
  135.  
  136.  
  137.  
  138. newtype EitherLeft b a = EitherLeft (Either a b)
  139.  
  140. newtype EitherRight a b = EitherRight (Either a b)
  141.  
  142.  
  143.  
  144. -- Exercise 1
  145.  
  146. instance Fluffy (EitherLeft t) where
  147.  
  148. furry = error "todo"
  149.  
  150.  
  151.  
  152. -- Exercise 2
  153.  
  154. instance Fluffy (EitherRight t) where
  155.  
  156. furry = error "todo"
  157.  
  158.  
  159.  
  160. class Misty m where
  161.  
  162. banana :: (a -> m b) -> m a -> m b
  163.  
  164. unicorn :: a -> m a
  165.  
  166.  
  167.  
  168. -- Exercise 3
  169.  
  170. -- (use banana and/or unicorn)
  171.  
  172. furry' :: (a -> b) -> m a -> m b
  173.  
  174. furry' = error "todo"
  175.  
  176.  
  177.  
  178.  
  179.  
  180. newtype State s a = State {
  181.  
  182. state :: (s -> (s, a))
  183.  
  184. }
  185.  
  186.  
  187.  
  188. -- Exercise 4
  189.  
  190. instance Misty (State s) where
  191.  
  192. banana = error "todo"
  193.  
  194. unicorn = error "todo"
  195.  
  196.  
  197.  
  198.  
  199.  
  200. fix :: (a -> a) -> a
  201.  
  202. fix f = let {x = f x} in x
  203.  
  204.  
  205.  
  206. fixed point instead of recursion
  207.  
  208.  
  209.  
  210. -- Exercise 5
  211.  
  212. write map using fix
  213.  
  214.  
  215.  
  216. writer monad
  217.  
  218.  
  219.  
  220. -- Exercise 6
  221.  
  222. add a trace
  223.  
  224. targil 5
  225. 1) given 2 categories and a mapping f between them
  226.  
  227. decide if f is a functor
  228.  
  229. 2) use parsec to write a lambda expression interpreter
  230.  
  231.  
  232.  
  233. if needed use simplifying assumptions so as not to check too many cases
  234.  
  235. and worry about too much syntax
  236.  
  237. Last targil
  238. This is the targil, (excluding final project)
  239.  
  240. 1) to write an interpreter for typed lambda calculus
  241.  
  242. kiss, namely only a single base type, very few error messages ...
  243.  
  244. a) use parsec
  245.  
  246. b) need a type checker
  247.  
  248. c) then if passes test, run
  249.  
  250. use of the internet is recommended for ideas on how, but you will have to simplify |
  251.  
  252. quiz 1 (MY SOL ONLY)
  253.  
  254. ---------------1----------------------
  255. data ColorTree a = Empty | Node a [ColorTree a]
  256.  
  257. r = Node "Red" [Node "Blue" [Empty], Node "Green" [Empty], Node "Red" [Empty]]
  258.  
  259. l = Node "Red" [Node "Blue" [Empty], Node "Red" [Empty]]
  260.  
  261. t = Node "Blue" [l,r]
  262. --------------2-----------------------
  263. isGray :: ColorTree String -> Bool
  264. isGray Empty = False
  265. isGray x = (elem' "Red" x) && (elem' "Blue" x) && (elem' "Green" x)
  266.  
  267. elem' :: String -> ColorTree String -> Bool
  268. elem' _ Empty = False
  269. elem' m (Node c x) = c == m || or (map (elem' m) x)
  270. -------------3------------------------
  271.  
  272. map' :: (a->b) -> [a] ->[b]
  273. map' f [] = []
  274. map' f [x] = foldr (\_ b -> b) [f x] []
  275. map' f (x:xs) = foldr (\_ b -> b) [f x] [] ++ map' f xs
  276.  
  277. --changes:
  278. -- 1: didnt need to use the `$` operator [] was enough.
  279. -- 3: I was expacting that `foldr f x []` will return f(x)
  280. -- so i changed it to `foldr (\_ b -> b) [f x]`
  281.  
  282. -- I think my grade should be 96 because the mistake in q1 was just a syntax problem (-1 point).
  283. -- And for q3 had a smaller change, but the logic of my answer wasn't changes at all (-3 points).
  284. -- 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 :) !
  285.  
  286.  
  287. quiz 2
  288. class Fluffy f where
  289.  
  290.  
  291.  
  292. furry :: (a -> b) -> f a -> f b
  293.  
  294.  
  295.  
  296. 1.a
  297.  
  298. instance Fluffy ((->) t) where
  299.  
  300. furry = error "todo"
  301.  
  302.  
  303.  
  304. 1.b
  305.  
  306. write a program (not completely trivial) using furry of ->
  307.  
  308.  
  309.  
  310. ----------------------------------------------------------------------------
  311.  
  312. newtype State s a = State {
  313.  
  314. state :: (s -> (s, a))
  315.  
  316. }
  317.  
  318.  
  319.  
  320. class Misty m where
  321.  
  322.  
  323.  
  324. banana :: (a -> m b) -> m a -> m b
  325.  
  326. unicorn :: a -> m a
  327.  
  328.  
  329.  
  330.  
  331.  
  332. 2.a
  333.  
  334. instance Misty (State t) where
  335.  
  336. banana = error "todo"
  337.  
  338. unicorn = error "todo"
  339.  
  340.  
  341.  
  342. 2.b
  343.  
  344. write a program (not completely trivial) using banana and unicorn of state
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement