Commit d0797d6b authored by epsi sayidina's avatar epsi sayidina

notes: haskell bind

parent c4dfaa1d
import MyFunc
main = do
putStrLn ":: Basic"
putStrLn $ unboxStr (Just "Hello World")
putStrLn ""
-- -- --
putStrLn ":: One Argument"
-- plain
putStrLn $ say1 "World"
-- functor
putUnbox $ say1 <$> (Just "World")
print $ say1 <$> ["World", "Lady"]
-- applicative
putUnbox $ (Just say1) <*> (Just "World")
print $ [say1] <*> ["World", "Lady"]
putStrLn ""
-- -- --
putStrLn ":: Two Arguments"
-- plain
putStrLn $ say2 "Hello" "World"
-- curry
putStrLn $ (say2 "Hello") "World"
-- or even
putStrLn $ (say2 "Hello") $ "World"
-- functor
putUnbox $ (say2 "Hello") <$> (Just "World")
print $ (say2 "Hello") <$> ["World", "Lady"]
-- applicative
putUnbox $ Just (say2 "Hello") <*> (Just "World")
print $ [say2 "Hello"] <*> ["World", "Lady"]
-- fmap is <$>
putUnbox $ fmap (say2 "Hello") (Just "World")
print $ fmap (say2 "Hello") ["World", "Lady"]
putStrLn ""
import MyFunc
main = do
-- -- --
putStrLn ":: Maybe Result"
-- plain
putUnbox $ say3 "World"
-- bind
putUnbox $ (Just "World") >>= say3
putUnbox $ say3 =<< (Just "World")
putStrLn ""
-- -- --
putStrLn ":: Chaining"
putUnbox $ Just "Hello " >>= say4 "world," >>= say4 " How are you ?"
putUnbox $ say4 " How are you ?" =<< say4 "world," =<< Just "Hello "
putStrLn ""
-- -- --
putStrLn ":: list1 :: [String] -> [String]"
print $ list1 ["World", "Lady"]
putStrLn ""
-- -- --
putStrLn "say1 :: String -> String"
print $ ["World"] >>= say1
print $ say1 =<< ["Lady"]
print $ ["World, ", "Lady"] >>= say1
print $ ["World"] >>= say2 "Hello"
putStrLn ""
-- -- --
putStrLn "list3 :: String -> [String]"
print $ ["World", "Lady"] >>= list3
print $ [] >>= list3
-- -- --
putStrLn "list4 :: String -> String -> [String]"
print $ ["World", "Lady"] >>= list4 "Hello" >>= list5 "Good Morning"
print $ (list4 "Hello" =<< ["World", "Lady"]) >>= list5 "Good Morning"
import MyFunc
import Control.Monad
sequ1 :: [String]
sequ1 =
["World", "Lady"]
>>= list4 "Hello"
>>= list5 "Good Morning"
sequ2 :: [String]
sequ2 = do
x <- ["World", "Lady"]
y <- list4 "Hello" x
list5 "Good Morning" y
main = do
print $ sequ2
module MyFunc
( unboxStr
, putUnbox
, say1, say2, say3, say4
, list1, list3, list4, list5
) where
-- tools
unboxStr :: Maybe String -> String
unboxStr (Just text) = text
unboxStr Nothing = ""
putUnbox :: Maybe String -> IO ()
putUnbox (Just text) = putStrLn text
putUnbox Nothing = return ()
-- basic
say1 :: String -> String
say1 str = "Hello " ++ str
say2 :: String -> String -> String
say2 str1 str2 = str1 ++ " " ++ str2
-- with failsafe
say3 :: String -> Maybe String
say3 str =
if (length str) /= 0
then Just ("Hello " ++ str)
else Nothing
say4 :: String -> String -> Maybe String
say4 text greet =
if ((length text) /= 0) && ((length greet) /= 0)
then Just (greet ++ text)
else Nothing
list1 :: [String] -> [String]
list1 str = map ("Hello " ++) str
list3 :: String -> [String]
list3 str =
if (length str) /= 0
then ["Hello " ++ str]
else []
list4 :: String -> String -> [String]
list4 greet text =
if ((length text) /= 0) && ((length greet) /= 0)
then [greet ++ " " ++ text]
else []
list5 :: String -> String -> [String]
list5 greet text =
if ((length text) /= 0) && ((length greet) /= 0)
then [text ++ " " ++ greet]
else []
......@@ -61,7 +61,13 @@ main = do
myPair <- pairStrIO ("myKey", "myValue")
putStrLn myPair
pairStrIO ("myKey", "myValue") >>= putStrLn
putStrLn =<< pairStrIO ("myKey", "myValue")
myMapResult <- mapM pairStrIO colorSchemes
putStrLn $ show myMapResult
putStrLn ""
(mapM pairStrIO colorSchemes) >>= (putStrLn . show)
(putStrLn . show) =<< (mapM pairStrIO colorSchemes)
putStrLn ""
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment