Advertisement
Guest User

Untitled

a guest
Oct 11th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. consume :: (Ord a, Num a) => [a] -> a -> a -> a -> a                                                      
  2. consume [] _ _ sum = sum                                                                                  
  3. consume xs lh rh sum =                                                                                    
  4.     if lh <= rh then                                                                                      
  5.     consume (tail xs) (max lh next_l) rh (if next_l >= lh then sum else sum + lh - next_l) else          
  6.     consume (init xs) lh (max rh next_r) (if next_r >= rh then sum else sum + rh - next_r)                
  7.     where next_l = head xs                                                                                
  8.           next_r = last xs                                                                                
  9.                                                                                                          
  10. calculatePuddleCapacity :: (Num p, Ord p) => [p] -> p                                                    
  11. calculatePuddleCapacity xs = if length xs <= 2 then 0 else consume (init $ tail xs) (head xs) (last xs) 0
  12.                                                                                                          
  13. main :: IO ()                                                                                            
  14. main = do                                                                                                
  15.     putStrLn . show $ calculatePuddleCapacity [2, 1, 2]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement