[Haskell-beginners] question >>= [style, efficiency]

Joel Neely joel.neely at gmail.com
Wed May 6 09:23:10 EDT 2009


I can think of a variety of ways to put a value at the end of a list
of the same type.

-- cutesy-clever
putAtEnd :: [a] -> a -> [a]
putAtEnd xs x = reverse (x : reverse xs)

-- by hand
atEnd :: [a] -> a -> [a]
atEnd [] z = [z]
atEnd (x:xs) z = x : atEnd xs z

-- obvious brute force
infixr 9 +:
(+:) :: [a] -> a -> [a]
(+:) xs x = xs ++ [x]

I'd appreciate feedback/advice on whether:
1) I've missed any clearly-better alternatives;
2) any of those is obviously better or worse than the others;
3) I should have known where to look for the answer myself (instead of
brute-force Googling).

I've semi-inadvertently mixed two issues above: infix-vs-prefix and
algorithm design. TIA for suggestions on either.

-jn-

-- 
Beauty of style and harmony and grace and good rhythm depend on
simplicity. - Plato


More information about the Beginners mailing list