Commit b3491684 authored by epsi sayidina's avatar epsi sayidina

haskell loop with map

parent 0edb967a
......@@ -2,6 +2,7 @@
-- Actualy it is a single linked lists
--import Control.Lens
import Data.Foldable
list :: [Int]
list = [1..9] ++ [0]
......@@ -9,22 +10,60 @@ list = [1..9] ++ [0]
indices' :: [Int] -> [Int]
indices' l = [0 .. (length l) - 1]
putList :: [Int] -> IO ()
putList myList =
mapM_ myFunctionComposition myList
where
myFunctionComposition = (putStr . (": " ++) . show)
main = do
putStr "list : "
print list
putStrLn ""
putStr "list : "
putStrLn $ show list
putStr "indices : "
print $ indices' list
putStrLn $ show $ indices' list
putStrLn ""
-- index
print (list !! 3)
--putStrLn $ show (list ^? element 13)
putStrLn ""
-- loop over an array/ list
-- using side effect only (using IO)
mapM_ print list
putStrLn ""
mapM_ (putStr . (": " ++) . show ) ([1..9] ++ [0])
-- producing new list (no IO), print raw
print $ map show list
putStrLn ""
-- using side effect of newly produced list
forM_ (map show list) putStrLn
putStrLn ""
-- oneliner using mapM_
mapM_ (putStr . (": " ++) . show) ([1..9] ++ [0]) >> putStrLn ""
-- oneliner using forM_, reversed argument
forM_ ([1..9] ++ [0]) (putStr . (": " ++) . show) >> putStrLn ""
putStrLn (concatMap ((": " ++) . show ) ([1..9] ++ [0]) )
-- oneliner using map
putStrLn $ concat $ map ((": " ++) . show) ([1..9] ++ [0])
putStrLn $ concatMap ((": " ++) . show) ([1..9] ++ [0])
-- let clause
let
myFunctionComposition = (putStr . (": " ++) . show)
myList = ([1..9] ++ [0])
in mapM_ myFunctionComposition myList
putStrLn ""
-- function
putList ([1..9] ++ [0])
putStrLn ""
-- Accessing Associative Array in Haskell
import System.Process
-- google material colors
-- https://material.io/guidelines/style/color.html
......@@ -24,13 +22,23 @@ putPairLn :: (String, String) -> IO ()
putPairLn (key, value) = do
putStrLn(key ++ " | " ++ value)
-- function
pairStr :: (String, String) -> String
pairStr (key, value) = key ++ " | " ++ value
main = do
print (colorSchemes !! 2)
putStrLn ""
-- loop over a hash dictionary of tuples
-- loop over a hash dictionary of tuples
-- using side effect only (using IO)
mapM_ print colorSchemes
putStrLn ""
-- producing new list (no IO), print raw
print $ map fst colorSchemes
putStrLn ""
mapM_ (print . fst) colorSchemes
putStrLn ""
......@@ -38,6 +46,10 @@ main = do
mapM_ (putStrLn . snd) colorSchemes
putStrLn ""
-- using side effect only (using IO)
mapM_ putPairLn colorSchemes
putStrLn ""
-- producing new list first
mapM_ putStrLn (map pairStr colorSchemes)
putStrLn ""
-- Passing Argument with Map in Haskell
import System.Process
type Pair = (String, String)
pair :: Pair
......@@ -34,7 +32,6 @@ dumpHash1 :: String -> [Pair] -> IO ()
dumpHash1 text dictionary = do
-- loop over a hash dictionary of tuples
mapM_ (dumpPair text) dictionary
-- where
-- IO action procedure
dumpHash2 :: String -> [Pair] -> IO ()
......@@ -53,9 +50,22 @@ dumpHash3 text dictionary = do
putStrLn(text ++ ": " ++ key ++ " | " ++ value)
) dictionary
-- IO action procedure
dumpHash4 :: String -> [Pair] -> IO ()
dumpHash4 text dictionary = do
-- loop over a hash dictionary of tuples
mapM_ (\(key, value) -> do
let message = text ++ ": " ++ key ++ " | " ++ value
putStrLn message
-- uncomment to debug in terminal
-- putStrLn ("Debug [" ++ message ++ "]")
) dictionary
main = do
dumpPair "Test" ("Key", "Value")
putStrLn ""
dumpHash3 "Name" colorSchemes
dumpHash4 "Name" colorSchemes
putStrLn ""
-- Passing Argument with Map in Haskell
import Data.Foldable
type Pair = (String, String)
pair :: Pair
pair = ("key", "value")
-- google material colors
-- https://material.io/guidelines/style/color.html
colorSchemes :: [Pair]
colorSchemes =
[("blue50", "#e3f2fd")
,("blue100", "#bbdefb")
,("blue200", "#90caf9")
,("blue300", "#64b5f6")
,("blue400", "#42a5f5")
,("blue500", "#2196f3")
,("blue600", "#1e88e5")
,("blue700", "#1976d2")
,("blue800", "#1565c0")
,("blue900", "#0d47a1")
]
-- function
pairMessage :: String -> Pair -> String
pairMessage text (key, value) =
text ++ ": " ++ key ++ " | " ++ value
-- function: loop over a hash dictionary of tuples
hashMessage1 :: String -> [Pair] -> [String]
hashMessage1 text dictionary =
map (pairMessage text) dictionary
-- function: loop over a hash dictionary of tuples
hashMessage2 :: String -> [Pair] -> [String]
hashMessage2 text dictionary =
map (pairMessage' text) dictionary
where
pairMessage' text (key, value) =
text ++ ": " ++ key ++ " | " ++ value
-- function: loop over a hash dictionary of tuples
hashMessage3 :: String -> [Pair] -> [String]
hashMessage3 text dictionary =
map ( \(key, value) ->
text ++ ": " ++ key ++ " | " ++ value
) dictionary
-- function
dumpHash5 :: String -> [Pair] -> IO ()
dumpHash5 text dictionary = do
-- loop over a hash dictionary of tuples
let messages = map ( \(key, value) ->
text ++ ": " ++ key ++ " | " ++ value
) dictionary
mapM_ putStrLn messages
-- function
dumpHash6 :: String -> [Pair] -> IO ()
dumpHash6 text dictionary = do
-- loop over a hash dictionary of tuples
forM_ messages putStrLn
where messages = map ( \(key, value) ->
text ++ ": " ++ key ++ " | " ++ value
) dictionary
-- function
dumpHash7 :: String -> [Pair] -> IO ()
dumpHash7 text dictionary = do
-- loop over a hash dictionary of tuples
forM_ messages debugStrLn
where
debugStrLn message = do
putStrLn message
-- uncomment to debug in terminal
-- putStrLn ("Debug [" ++ message ++ "]")
messages = map ( \(key, value) ->
text ++ ": " ++ key ++ " | " ++ value
) dictionary
main = do
putStrLn $ pairMessage "Test" ("Key", "Value")
putStrLn ""
mapM_ putStrLn (hashMessage3 "Name" colorSchemes)
putStrLn ""
dumpHash7 "Name" 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