Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. module Main where
  2.  
  3. import Prelude
  4. import Data.List
  5. import Control.Apply
  6. import Data.Boolean
  7. import Data.Maybe
  8. import Control.Monad.Writer
  9. import Control.Monad.Writer.Class
  10. import Control.Monad.State
  11. import Data.Tuple
  12. import Control.Extend (class Functor)
  13. import Control.Monad.Eff (Eff)
  14. import Control.Monad.Eff.Console (CONSOLE, log)
  15. import Data.Array (reverse)
  16. import Data.Bifunctor (class Bifunctor)
  17.  
  18. main :: forall e. Eff (console :: CONSOLE | e) Unit
  19. main = do
  20. log "Hello sailor!"
  21.  
  22. --
  23.  
  24. id :: forall a. a -> a
  25. id x = x
  26.  
  27. --
  28.  
  29.  
  30. add :: Int -> Int -> Int
  31. add x y = x + y
  32.  
  33. add5 = add 5
  34.  
  35. --
  36.  
  37.  
  38. which2 :: Int -> String
  39. which2 0 = "nula"
  40. which2 1 = "jedna"
  41. which2 _ = "jiný"
  42.  
  43. which :: Int -> String
  44. which n | n == 0 = "nula"
  45. | n > 1 && n < 5 = "jedna až pět"
  46. | otherwise = "jiný"
  47.  
  48. --
  49.  
  50. data Pair a b = Pair a b
  51.  
  52. instance showPair :: (Show a, Show b) => Show (Pair a b) where
  53. show (Pair a b) = "(Pair " <> show a <> " " <> show b <> ")"
  54.  
  55. instance functorPair :: Bifunctor Pair where
  56. bimap f g (Pair a b) = Pair (f a) (g b)
  57.  
  58.  
  59. data Same a = Same a a
  60.  
  61. instance showSame :: (Show a) => Show (Same a) where
  62. show (Same a b) = "(Pair " <> show a <> " " <> show b <> ")"
  63.  
  64. instance functorSame :: Functor Same where
  65. map f (Same a b) = Same (f a) (f b)
  66.  
  67.  
  68. shift :: forall a. List a -> List a
  69. shift (x : xs) = snoc xs x
  70. shift (x) = x
  71.  
  72. zip' :: forall a b. List (Pair a b) -> List a -> List b -> List (Pair a b)
  73. zip' l (a : as) (b : bs) = zip' (snoc l $ Pair a b) as bs
  74. zip' l _ _ = l
  75.  
  76.  
  77. div' :: Int -> Int -> Maybe Int
  78. div' _ 0 = Nothing
  79. div' a b = Just (a / b)
  80.  
  81. --
  82.  
  83.  
  84. m :: Maybe Int -> Int
  85. m (Just a) = a + 1
  86. m Nothing = 9
  87.  
  88. m' :: Maybe Int -> Int
  89. m' = case _ of
  90. Just n | n == 0 -> 10
  91. | n > 0 -> n + 1
  92. _ -> 9
  93.  
  94.  
  95. --
  96.  
  97. sqs :: Int -> Int -> Int
  98. sqs a b = a' + b'
  99. where
  100. a' = a * a
  101. b' = b * b
  102.  
  103.  
  104. --
  105.  
  106. zip'' :: forall a b. List a -> List b -> List (Pair a b)
  107. zip'' ax bx = z Nil ax bx
  108. where
  109. z :: List (Pair a b) -> List a -> List b -> List (Pair a b)
  110. z l (a : as) (b : bs) = z (snoc l $ Pair a b) as bs
  111. z l _ _ = l
  112.  
  113.  
  114.  
  115. --shift :: forall a. List a -> List a
  116. --shift a = return $ tail a
  117.  
  118. --
  119.  
  120. type User =
  121. { username :: String
  122. , password :: String
  123. , scopes :: List Int
  124. }
  125.  
  126. showUser :: User -> String
  127. showUser user = "User " <> user.username <> " " <> user.password <> " " <> show user.scopes
  128.  
  129. type Users = List User
  130.  
  131. honza :: User
  132. honza = { username: "honza", password: "prdel", scopes: 1 .. 3 }
  133.  
  134. karel :: User
  135. karel = { username: "karel", password: "prdel", scopes: Nil }
  136.  
  137. users :: Users
  138. users = (honza : karel : Nil)
  139.  
  140. type Credentials r =
  141. { username :: String
  142. , password :: String
  143. | r
  144. }
  145.  
  146. allowed :: forall r. Users -> Credentials r -> Boolean
  147. allowed l { username: u1, password: p1 } = isJust $ head $ filter match l
  148. where
  149. match :: forall r'. Credentials r' -> Boolean
  150. match { username: u2, password: p2 } = u1 == u2 && p1 == p2
  151.  
  152. allowed' :: forall r. Users -> Credentials r -> Maybe User
  153. allowed' l { username: u1, password: p1 } = head $ filter match l
  154. where
  155. match :: forall r'. Credentials r' -> Boolean
  156. match { username: u2, password: p2 } = u1 == u2 && p1 == p2
  157.  
  158. -- showUser <$> allowed users { username: "honza", password: "prdel" }
  159.  
  160. --
  161.  
  162. newUser :: String -> String -> User
  163. newUser username password = { username: username
  164. , password: password
  165. , scopes: 1 .. 3
  166. }
  167.  
  168. f :: String -> String -> Maybe User
  169. f username password =
  170. lift2 newUser (Just username) (Just password) >>= \n -> pure n
  171.  
  172. f' :: Maybe String -> Maybe String -> Boolean
  173. f' username password = isJust found
  174. where
  175. found = do
  176. user <- lift2 newUser username password
  177. user' <- allowed' users user
  178. pure user'
  179.  
  180. --
  181.  
  182. -- (Just 2) >>= \n -> pure (n + 1)
  183. -- (Just 2) >>= \n -> Just (n + 1)
  184. -- bind (Just 2) (\n -> Just (n + 1))
  185.  
  186.  
  187. gcd' :: Int -> Int -> Int
  188. gcd' n 0 = n
  189. gcd' 0 m = m
  190. gcd' n m | n > m = gcd (n - m) m
  191. | otherwise = gcd n (m - n)
  192.  
  193. gcdLog :: Int -> Int -> Writer (Array String) Int
  194. gcdLog n 0 = pure n
  195. gcdLog 0 m = pure m
  196. gcdLog n m = do
  197. tell ["gcdLog " <> show n <> " " <> show m]
  198. if n > m
  199. then gcdLog (n - m) m
  200. else gcdLog n (m - n)
  201.  
  202.  
  203. --
  204.  
  205. toOrdinal :: Int -> String
  206. toOrdinal 0 = "0"
  207. toOrdinal n = case n `mod` 10 of
  208. 1 -> show n <> "st"
  209. 2 -> show n <> "nd"
  210. 3 -> show n <> "rd"
  211. _ -> show n <> "th"
  212.  
  213. reverse' :: forall a. List a -> List a
  214. reverse' l = reverse'' Nil l
  215. where
  216. reverse'' :: forall a. List a -> List a -> List a
  217. reverse'' r (x : xs) = reverse'' (Cons x r) xs
  218. reverse'' r Nil = r
  219.  
  220.  
  221. pow' :: Int -> Int -> Int
  222. pow' 0 n = 1
  223. pow' 1 n = n
  224. pow' x n = foldl (\r _ -> r * n) n (2 .. x)
  225.  
  226. root :: Int -> Int -> Int -> Int
  227. root x r n | x > n = digitalRoot r
  228. | otherwise = root x' (r + m'') (n - m'')
  229. where
  230. x' = x * 10
  231. m' = n `mod` x'
  232. m'' = m' / x
  233.  
  234. digitalRoot :: Int -> Int
  235. digitalRoot n | n < 10 = n
  236. | otherwise = root 1 0 n
  237.  
  238.  
  239.  
  240.  
  241. fib :: Int -> Writer (Array String) Int
  242. fib 0 = pure 0
  243. fib 1 = pure 1
  244. fib n = do
  245. tell [show n]
  246. a <- fib (n - 1)
  247. b <- fib (n - 2)
  248. pure (a + b)
  249.  
  250.  
  251. factorial :: State (Tuple Int Int) Int
  252. factorial = get >>= \(Tuple n r) ->
  253. if n <= 1
  254. then pure r
  255. else do
  256. put (Tuple (n - 1) (r * n))
  257. factorial
  258.  
  259.  
  260. fib' :: State (Tuple (Tuple Int Int) Int) Int
  261. fib' = get >>= \(Tuple (Tuple x1 x2) n) ->
  262. if n == 0
  263. then pure x1
  264. else do
  265. put (Tuple (Tuple x2 (x1 + x2)) (n - 1))
  266. fib'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement