Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

15
Task/Amb/Haskell/amb-1.hs Normal file
View file

@ -0,0 +1,15 @@
import Control.Monad
amb = id
joins left right = last left == head right
example = do
w1 <- amb ["the", "that", "a"]
w2 <- amb ["frog", "elephant", "thing"]
w3 <- amb ["walked", "treaded", "grows"]
w4 <- amb ["slowly", "quickly"]
guard (w1 `joins` w2)
guard (w2 `joins` w3)
guard (w3 `joins` w4)
pure $ unwords [w1, w2, w3, w4]

53
Task/Amb/Haskell/amb-2.hs Normal file
View 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
View 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

17
Task/Amb/Haskell/amb-4.hs Normal file
View file

@ -0,0 +1,17 @@
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"],
joins w1 w2,
w3 <- ["walked", "treaded", "grows"],
joins w2 w3,
w4 <- ["slowly", "quickly"],
joins w3 w4
]
main :: IO ()
main = print example