Progress
This commit is contained in:
parent
250335f16b
commit
3f403f946b
File diff suppressed because it is too large
Load diff
17
src/posts/0010-Haskell-Now/io_bind.hs
Normal file
17
src/posts/0010-Haskell-Now/io_bind.hs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import Data.Maybe
|
||||||
|
import Text.Read (readMaybe)
|
||||||
|
|
||||||
|
getListFromString :: String -> Maybe [Integer]
|
||||||
|
getListFromString str = readMaybe $ "[" ++ str ++ "]"
|
||||||
|
askUser :: IO [Integer]
|
||||||
|
askUser =
|
||||||
|
putStrLn "Enter a list of numbers (sep. by commas):" >>
|
||||||
|
getLine >>= \input ->
|
||||||
|
let maybeList = getListFromString input in
|
||||||
|
case maybeList of
|
||||||
|
Just l -> return l
|
||||||
|
Nothing -> askUser
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = askUser >>=
|
||||||
|
\list -> print $ sum list
|
14
src/posts/0010-Haskell-Now/list_monad.hs
Normal file
14
src/posts/0010-Haskell-Now/list_monad.hs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import Control.Monad (guard)
|
||||||
|
|
||||||
|
allCases = [1..10]
|
||||||
|
|
||||||
|
resolve :: [(Int,Int,Int)]
|
||||||
|
resolve = do
|
||||||
|
x <- allCases
|
||||||
|
y <- allCases
|
||||||
|
z <- allCases
|
||||||
|
guard $ 4*x + 2*y < z
|
||||||
|
return (x,y,z)
|
||||||
|
|
||||||
|
main = do
|
||||||
|
print resolve
|
30
src/posts/0010-Haskell-Now/maybe_monad_1.hs
Normal file
30
src/posts/0010-Haskell-Now/maybe_monad_1.hs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
deposit value account = account + value
|
||||||
|
withdraw value account = account - value
|
||||||
|
|
||||||
|
eligible :: (Num a,Ord a) => a -> Bool
|
||||||
|
eligible account =
|
||||||
|
let account1 = deposit 100 account in
|
||||||
|
if (account1 < 0)
|
||||||
|
then False
|
||||||
|
else
|
||||||
|
let account2 = withdraw 200 account1 in
|
||||||
|
if (account2 < 0)
|
||||||
|
then False
|
||||||
|
else
|
||||||
|
let account3 = deposit 100 account2 in
|
||||||
|
if (account3 < 0)
|
||||||
|
then False
|
||||||
|
else
|
||||||
|
let account4 = withdraw 300 account3 in
|
||||||
|
if (account4 < 0)
|
||||||
|
then False
|
||||||
|
else
|
||||||
|
let account5 = deposit 1000 account4 in
|
||||||
|
if (account5 < 0)
|
||||||
|
then False
|
||||||
|
else
|
||||||
|
True
|
||||||
|
|
||||||
|
main = do
|
||||||
|
print $ eligible 300 -- True
|
||||||
|
print $ eligible 299 -- False
|
20
src/posts/0010-Haskell-Now/maybe_monad_2.hs
Normal file
20
src/posts/0010-Haskell-Now/maybe_monad_2.hs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
deposit :: (Num a) => a -> a -> Maybe a
|
||||||
|
deposit value account = Just (account + value)
|
||||||
|
|
||||||
|
withdraw :: (Num a,Ord a) => a -> a -> Maybe a
|
||||||
|
withdraw value account = if (account < value)
|
||||||
|
then Nothing
|
||||||
|
else Just (account - value)
|
||||||
|
|
||||||
|
eligible :: (Num a, Ord a) => a -> Maybe Bool
|
||||||
|
eligible account = do
|
||||||
|
account1 <- deposit 100 account
|
||||||
|
account2 <- withdraw 200 account1
|
||||||
|
account3 <- deposit 100 account2
|
||||||
|
account4 <- withdraw 300 account3
|
||||||
|
account5 <- deposit 1000 account4
|
||||||
|
Just True
|
||||||
|
|
||||||
|
main = do
|
||||||
|
print $ eligible 300 -- Just True
|
||||||
|
print $ eligible 299 -- Nothing
|
20
src/posts/0010-Haskell-Now/maybe_monad_3.hs
Normal file
20
src/posts/0010-Haskell-Now/maybe_monad_3.hs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
deposit :: (Num a) => a -> a -> Maybe a
|
||||||
|
deposit value account = Just (account + value)
|
||||||
|
|
||||||
|
withdraw :: (Num a,Ord a) => a -> a -> Maybe a
|
||||||
|
withdraw value account = if (account < value)
|
||||||
|
then Nothing
|
||||||
|
else Just (account - value)
|
||||||
|
|
||||||
|
eligible :: (Num a, Ord a) => a -> Maybe Bool
|
||||||
|
eligible account =
|
||||||
|
deposit 100 account >>=
|
||||||
|
withdraw 200 >>=
|
||||||
|
deposit 100 >>=
|
||||||
|
withdraw 300 >>=
|
||||||
|
deposit 1000 >>
|
||||||
|
return True
|
||||||
|
|
||||||
|
main = do
|
||||||
|
print $ eligible 300 -- Just True
|
||||||
|
print $ eligible 299 -- Nothing
|
9
src/posts/0010-Haskell-Now/monad_composition.hs
Normal file
9
src/posts/0010-Haskell-Now/monad_composition.hs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import Control.Monad ((>=>))
|
||||||
|
|
||||||
|
f :: Int -> [Int]
|
||||||
|
f n = [n, n+1]
|
||||||
|
|
||||||
|
g :: Int -> [String]
|
||||||
|
g n = [show n,">"++show (n+1)]
|
||||||
|
|
||||||
|
main = print $ (f >=> g) 2
|
Loading…
Reference in a new issue