monads
This commit is contained in:
62
haskell/sessions/16_03/scratchpad/a.hs
Normal file
62
haskell/sessions/16_03/scratchpad/a.hs
Normal file
@ -0,0 +1,62 @@
|
||||
z :: Int -> Int
|
||||
z a = 3 * a -- 3x3 = 9 => 3*3
|
||||
|
||||
y :: (Int -> Int) -> [Int] -> [Int]
|
||||
-- y(\x -> 3*x) [1,2,3] -> [3,6,9]
|
||||
y z lst = map z lst -- f(x) = 3x => f x = 3x
|
||||
|
||||
isEven :: Int -> Bool
|
||||
isEven x = if x `mod` 2 == 0 then True else False
|
||||
|
||||
-- ~~mod(x, 2)~~ => x mod 2
|
||||
-- isEven x = x `mod` 2 == 0
|
||||
|
||||
isAmongus :: String -> String
|
||||
-- traverse each character and check if "amongus" is in the string, return the rest of the string
|
||||
isAmongus [] = [] -- base case if the string is empty (or we reached the end of the string)
|
||||
isAmongus (x : xs) -- recurse on the rest of the string
|
||||
| (take 7 (x : xs) == "amongus") = drop 7 (x : xs)
|
||||
| otherwise = isAmongus xs
|
||||
|
||||
b :: [Int] -> Int
|
||||
b lst = sum (y z lst) -- = 18
|
||||
|
||||
allTogether :: [Int] -> Int
|
||||
allTogether list = sum (map (3 *) list)
|
||||
|
||||
foo :: Integer -> Integer
|
||||
foo 0 = 16
|
||||
foo 1
|
||||
| "Haskell" > "C++" = 3
|
||||
| otherwise = 4
|
||||
foo n
|
||||
| n < 0 = 0
|
||||
| n `mod` 17 == 2 = -43
|
||||
| otherwise = n + 3
|
||||
|
||||
bogus :: [a] -> Bool
|
||||
bogus (x:_) = True
|
||||
bogus _ = False
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
print $ b [1, 2, 3]
|
||||
putStrLn "AAAAAAAAA"
|
||||
print $ allTogether [1, 2, 3]
|
||||
|
||||
putStrLn "Finding amongus"
|
||||
print $ isAmongus "amongus"
|
||||
print $ isAmongus "aaaaaaaaaaaaaa"
|
||||
print $ isAmongus "askldhjewkjrhwekjrhwekjrhekjlfhbjerkshtjkernamogusejqwklwqhekqjwehqwjkamongusasdasdsad"
|
||||
|
||||
-- foos
|
||||
print $ foo 0
|
||||
|
||||
print $ bogus [1,10..]
|
||||
|
||||
-- compose 2 functions with a dot
|
||||
print $ (y z . y z) [1, 2, 3]
|
||||
print $ (y z . y z . y z) [1, 2, 3]
|
||||
print $ (.) (y z) (y z) [1, 2, 3]
|
||||
print $ y z $ y z [1, 2, 3]
|
Reference in New Issue
Block a user