fork download
  1. main = do
  2. print $ safeHead ([] :: [Int])
  3. print $ safeHead ([1,2] :: [Int])
  4.  
  5. safeHead :: [a] -> Maybe a
  6. safeHead = partial head (not . null)
  7.  
  8. partial :: (a -> b) -> (a -> Bool) -> a -> Maybe b
  9. partial f p x | p x = Just $ f x | otherwise = Nothing
  10.  
Success #stdin #stdout 0.02s 3580KB
stdin
Standard input is empty
stdout
Nothing
Just 1