Last active
December 17, 2020 17:10
-
-
Save sroccaserra/67dbc3a01023eaa111b8678ae5ca1f48 to your computer and use it in GitHub Desktop.
Suite de Conway en Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Conway where | |
-- https://fr.wikipedia.org/wiki/Suite_de_Conway | |
-- 1211 | |
-- Il y a trois éléments : | |
-- - la liste déjà traitée, | |
-- - le chiffre en cours de comptage, | |
-- - la liste restant à traiter | |
-- ([],[],[1,2,1,1]) | |
-- ([],[1,1][2,1,1]) | |
-- ([1,1],[1,2],[1,1]) | |
-- ([1,1,1,2],[1,1][1]) | |
-- ([1,1,1,2],[2,1][]) | |
-- ([1,1,1,2,2,1],[][]) | |
type Processed = [Int] | |
type ToProcess = [Int] | |
conway :: ToProcess -> Processed | |
conway ns = recur [] ns | |
recur :: Processed -> ToProcess -> Processed | |
recur ps [] = ps | |
recur ps ts = recur (ps++counted) rest | |
where (counted, rest) = countSameNumbers ts | |
countSameNumbers :: ToProcess -> (Processed,ToProcess) | |
countSameNumbers ns@(n:_) = ([length sameNs, n], rest) | |
where sameNs = takeWhile (== n) ns | |
rest = dropWhile (== n) ns | |
countSameNumbers _ = error "wrong state" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment