September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
53
Task/Amb/Haskell/amb-2.hs
Normal file
53
Task/Amb/Haskell/amb-2.hs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
joins :: String -> String -> Bool
|
||||
joins left right = last left == head right
|
||||
|
||||
-- First desugaring (dropping the do notation)
|
||||
-- in terms of the bind operator (>>=) for the list monad
|
||||
|
||||
exampleBind :: String
|
||||
exampleBind =
|
||||
["the", "that", "a"] >>=
|
||||
(\w1 ->
|
||||
["frog", "elephant", "thing"] >>=
|
||||
\w2 ->
|
||||
["walked", "treaded", "grows"] >>=
|
||||
\w3 ->
|
||||
["slowly", "quickly"] >>=
|
||||
(\w4 ->
|
||||
if joins w1 w2
|
||||
then (if joins w2 w3
|
||||
then (if joins w3 w4
|
||||
then unwords [w1, w2, w3, w4]
|
||||
else [])
|
||||
else [])
|
||||
else []))
|
||||
|
||||
-- Second desugaring (still dropping the do notation)
|
||||
-- in terms of the concatMap, which is >>= with its arguments flipped
|
||||
|
||||
exampleConcatMap :: String
|
||||
exampleConcatMap =
|
||||
concatMap
|
||||
(\w1 ->
|
||||
concatMap
|
||||
(\w2 ->
|
||||
concatMap
|
||||
(\w3 ->
|
||||
concatMap
|
||||
(\w4 ->
|
||||
if joins w1 w2
|
||||
then (if joins w2 w3
|
||||
then (if joins w3 w4
|
||||
then unwords [w1, w2, w3, w4]
|
||||
else [])
|
||||
else [])
|
||||
else [])
|
||||
["slowly", "quickly"])
|
||||
["walked", "treaded", "grows"])
|
||||
["frog", "elephant", "thing"])
|
||||
["the", "that", "a"]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
print exampleBind
|
||||
print exampleConcatMap
|
||||
22
Task/Amb/Haskell/amb-3.hs
Normal file
22
Task/Amb/Haskell/amb-3.hs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
example :: [String]
|
||||
example =
|
||||
["the", "that", "a"] >>=
|
||||
\w1 ->
|
||||
when True ["frog", "elephant", "thing"] >>=
|
||||
\w2 ->
|
||||
when (joins w1 w2) ["walked", "treaded", "grows"] >>=
|
||||
\w3 ->
|
||||
when (joins w2 w3) ["slowly", "quickly"] >>=
|
||||
\w4 -> when (joins w3 w4) [w1, w2, w3, w4]
|
||||
|
||||
joins :: String -> String -> Bool
|
||||
joins left right = last left == head right
|
||||
|
||||
when :: Bool -> [a] -> [a]
|
||||
when p xs =
|
||||
if p
|
||||
then xs
|
||||
else []
|
||||
|
||||
main :: IO ()
|
||||
main = print $ unwords example
|
||||
16
Task/Amb/Haskell/amb-4.hs
Normal file
16
Task/Amb/Haskell/amb-4.hs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
joins :: String -> String -> Bool
|
||||
joins left right = last left == head right
|
||||
|
||||
example :: [String]
|
||||
example =
|
||||
[ unwords [w1, w2, w3, w4]
|
||||
| w1 <- ["the", "that", "a"]
|
||||
, w2 <- ["frog", "elephant", "thing"]
|
||||
, w3 <- ["walked", "treaded", "grows"]
|
||||
, w4 <- ["slowly", "quickly"]
|
||||
, joins w1 w2
|
||||
, joins w2 w3
|
||||
, joins w3 w4 ]
|
||||
|
||||
main :: IO ()
|
||||
main = print example
|
||||
Loading…
Add table
Add a link
Reference in a new issue