Sunday, 5 June 2016

Haskell: Drop prefix from given String (or) list


DropPrefix.hs
dropPrefix :: Eq a => [a] -> [a] -> Maybe [a]
dropPrefix [] ys = Just ys
dropPrefix xs [] = Nothing
dropPrefix (x:xs) (y:ys)
    | (x == y) = dropPrefix xs ys
    | otherwise = Nothing

Prelude> :load DropPrefix.hs
[1 of 1] Compiling Main             ( DropPrefix.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> dropPrefix "Hello" "Hello World"
Just " World"
*Main> 
*Main> dropPrefix [2, 3, 5, 6] [2, 3]
Nothing
*Main> 
*Main> dropPrefix [2, 3, 5, 6] [2, 3, 5, 6, 8, 9]
Just [8,9]
*Main> 
*Main> 
*Main> dropPrefix [] []
Just []
*Main> 
*Main> dropPrefix [2, 3, 5, 6] [2, 3, 5, 7, 6, 8, 9]
Nothing
*Main> 
*Main> dropPrefix [] [2, 3, 5, 7, 6, 8, 9]
Just [2,3,5,7,6,8,9]
*Main>



Previous                                                 Next                                                 Home

No comments:

Post a Comment