[Haskell-beginners] Are these solution the Haskell way ?

Roelof Wobben r.wobben at home.nl
Wed May 13 06:10:17 UTC 2015


Hello,

For practising pattern matching and recursion I did recreate some 
commands of Data,list.

My re-implementation of ++ :

plusplus :: [a] -> [a] -> [a]
plusplus [] [] = [] ;
plusplus [] (xs) = xs
plusplus (xs) [] = xs
plusplus (xs) yx = plusplus' (reverse xs) yx

plusplus' :: [a] -> [a] -> [a]
plusplus' [] (xs) = xs
plusplus' (x:xs) yx = plusplus' xs (x:yx)

main = print $ plusplus ["a","b"] ["c","d"]

my re-implementation of init :

import Data.Maybe

-- | The main entry point.
init' :: [a] -> Maybe [a]
init' [] = Nothing
init' [x] = Just []
init' (x:xs) = Just (x:fromMaybe xs (init' xs))

main = print . init' $ [1,3]


my re-implementation of last :

-- | The main entry point.
last' :: [a] -> Maybe a
last' [] = Nothing
last' [x] = Just x
last' (_:xs) = last' xs

main = print . last' $ []

Now I wonder if these solutions are the haskell way ? if not so, how can 
I improve them ,

Roelof


---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
http://www.avast.com



More information about the Beginners mailing list