Skip to content

Instantly share code, notes, and snippets.

@essic
Created April 9, 2017 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save essic/4d984d724fb0d9a60daac6228ad0c838 to your computer and use it in GitHub Desktop.
Save essic/4d984d724fb0d9a60daac6228ad0c838 to your computer and use it in GitHub Desktop.
Simple abstract data type exemple 1 created by essic - https://repl.it/GEaw/39
accumulate :: (a -> b) -> [a] -> [b]
accumulate _ [] = []
accumulate f (x:xs) = f x : accumulate f xs
data Planet = Mercury
| Venus
| Earth
| Mars
| Jupiter
| Saturn
| Uranus
| Neptune
ageOn :: Planet -> Float -> Float
-- ageOn = (*) . secondsPerYear
ageOn x y = secondsPerYear x * y
secondsPerYear :: Planet -> Float
secondsPerYear planet = earthYear * case planet of
Mercury -> 0.2408467
Venus -> 0.61519726
Earth -> 1
Mars -> 1.8808158
Jupiter -> 11.862615
Saturn -> 29.447498
Uranus -> 84.016846
Neptune -> 164.79132
where earthYear = 365.25 * 24 * 60 * 60
main = do
print result
where result = accumulate (ageOn Mars) [1,2,3,4,5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment