Intersecting number wheels: Difference between revisions
Content added Content deleted
(Added C#) |
(→{{header|Haskell}}: Added a first Haskell draft) |
||
Line 439: | Line 439: | ||
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4 ... |
1 3 5 1 4 3 1 4 5 1 3 4 1 3 5 1 4 3 1 4 ... |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
|||
Defining a unit movement of the interlocking wheels as a recursive descent, |
|||
terminating at the first digit found, and map-accumulating with that recursion over a list of given length. |
|||
<lang haskell>import qualified Data.Map.Strict as M |
|||
import Data.Maybe (fromMaybe) |
|||
import Data.List (mapAccumL) |
|||
import Data.Char (isDigit) |
|||
import Data.Bool (bool) |
|||
clockWorkTick :: M.Map Char String -> (M.Map Char String, Char) |
|||
clockWorkTick wheelMap = |
|||
let leftRotate = take . length <*> (tail . cycle) |
|||
click wheels name = |
|||
let wheel = fromMaybe ['?'] (M.lookup name wheels) |
|||
v = head wheel |
|||
in bool |
|||
click |
|||
(,) |
|||
(isDigit v) |
|||
(M.insert name (leftRotate wheel) wheels) |
|||
v |
|||
in click wheelMap 'A' |
|||
main :: IO () |
|||
main = |
|||
mapM_ print $ |
|||
(flip (mapAccumL (const . clockWorkTick)) [1 .. 20] . M.fromList) <$> |
|||
[ [('A', "123")] |
|||
, [('A', "1B2"), ('B', "34")] |
|||
, [('A', "1DD"), ('D', "678")] |
|||
, [('A', "1BC"), ('B', "34"), ('C', "5B")] |
|||
]</lang> |
|||
{{Out}} |
|||
<pre>(fromList [('A',"312")],"12312312312312312312") |
|||
(fromList [('A',"21B"),('B',"43")],"13214213214213214213") |
|||
(fromList [('A',"D1D"),('D',"786")],"16718617816718617816") |
|||
(fromList [('A',"C1B"),('B',"34"),('C',"5B")],"13514314513413514314")</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |