Games
Arrays
Data Types
Algorithms
Logic
Very readable solution (though not tail-recursive).
Could be used as spec for quick check based tests in case a more performant solution is required.
module Platforms where platforms :: [Integer] -> Integer platforms (x:y:xs) = abs (x - y) + platforms (y:xs) platforms _ = 0
- module Platforms where
- platforms :: [Integer] -> Integer
platforms xs = sum $ zipWith energy xs (tail xs)whereenergy x y = abs (x - y)- platforms (x:y:xs) = abs (x - y) + platforms (y:xs)
- platforms _ = 0
Games
Arrays
Data Types
Algorithms
Logic
Made algorithm more readable
module Platforms where platforms :: [Integer] -> Integer platforms xs = sum $ zipWith energy xs (tail xs) where energy x y = abs (x - y)
- module Platforms where
- platforms :: [Integer] -> Integer
platforms [] = 0platforms (_:[]) = 0platforms (x:xs) = platforms' (abs . (x -), xs) whereplatforms' :: (Integer -> Integer, [Integer]) -> Integerplatforms' (acc, x:[]) = acc xplatforms' (acc, x:xs) = platforms' (((+) (acc x)) . abs . (x -), xs)- platforms xs = sum $ zipWith energy xs (tail xs)
- where
- energy x y = abs (x - y)