Hydra Implementation in 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
let firstArray = 0:1:[2, 3] | |
-- [0, 1, 2, 3] | |
let infiniteOnes = 1:infiniteOnes | |
-- [1, 1, 1, 1, 1, ........................] | |
-- never stops, hit ctrl-C to get your interpreter back |
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
sumOfElements [] = 0 | |
sumOfElements (x:xs) = x + sumOfElements xs |
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
[1] ++ [3] -- [1, 3] | |
null [] -- True | |
null [1] -- False | |
replicate 2 3 -- [3, 3] |
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
chop [] = [] | |
chop (1:xs) = xs | |
chop (n:xs) = (replicate (n - 1) (n - 1)) ++ xs | |
---------------------------------------------------- | |
chop [1] | |
-- [] | |
chop [4] | |
-- [3, 3, 3] | |
chop [3, 3, 3] | |
-- [2, 2, 3, 3] | |
chop [9,9,9,9,9,9,9,9,9] | |
-- [8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9] |
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
countOfChops heads = length (repeatedlyChopTillDead heads) | |
-------------------------------------------------- | |
countOfChops [1] -- 1 | |
countOfChops [3] -- 5 | |
countOfChops [9,9,9,9,9,9,9,9,9] -- 986409 (this takes a while) |
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
willHerculesDie n heads = any (> n) (map length (repeatedlyChopTillDead heads)) | |
---------------------------------------------------------------------------- | |
willHerculesDie 37 [9,9,9,9,9,9,9,9,9] -- False | |
willHerculesDie 36 [9,9,9,9,9,9,9,9,9] -- True |
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
-- To load this, save this as hydra.hs | |
-- then run ghci, and type :l hydra (colon ell hydra) | |
module Hydra where | |
firstArray = 0:1:[2, 3] | |
infiniteOnes = 1:infiniteOnes | |
chop [] = [] | |
chop (1:xs) = xs | |
chop (n:xs) = (replicate (n - 1) (n - 1)) ++ xs | |
-- repeatedlyChop heads = heads:repeatedlyChop (chop heads) | |
-- takeWhileAlive (x:xs) = if null x then [] else x:(takeWhileAlive xs) | |
-- repeatedlyChopTillDead heads = takeWhileAlive (repeatedlyChop heads) | |
repeatedlyChopTillDead heads = takeWhile (not.null) (iterate chop heads) | |
countOfChops heads = length (repeatedlyChopTillDead heads) | |
willHerculesDie n heads = any (> n) (map length (repeatedlyChopTillDead heads)) |
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
repeatedlyChopTillDead heads = takeWhileAlive (repeatedlyChopTillDead heads) | |
---------------------------------------------------------------------------- | |
repeatedlyChopTillDead [4] | |
-- [[4],[3,3,3],[2,2,3,3],[1,2,3,3],[2,3,3],[1,3,3],[3,3],[2,2,3],[1,2,3],[2,3],[1,3],[3],[2,2],[1,2],[2],[1]] |
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
repeatedlyChop heads = heads:repeatedlyChop (chop heads) | |
---------------------------------------------------------- | |
repeatedlyChop [3] | |
-- [[3],[2,2],[1,2],[2],[1], [], [], [] ...] | |
-- this is an infinite list |
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
repeatedlyChop heads = iterate chop heads |
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
repeatedlyChopTillDead heads = takeWhile (not.null) (iterate chop heads) |
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
takeWhileAlive (x:xs) = if null x then [] else x:(takeWhileAlive xs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment