[Haskell-cafe] List operation question

Lennart Augustsson lennart at augustsson.net
Sun Feb 4 13:58:34 EST 2007


Not much better.  You could define shiftl such that is does a single  
traversal and
returns both the last element and all but the last.  That will save  
you one traversal.

On Feb 4, 2007, at 18:44 , Eric Olander wrote:

> Hi,
>    I'm still somewhat new to Haskell, so I'm wondering if there are  
> better ways I could implement the following functions, especially  
> shiftl:
>
> >> moves the first element to the end of the list
>     shiftr :: [a] -> [a]
>     shiftr [] = []
>     shiftr (x:y) = y ++ [x]
>
> >> moves the last element to the head of the list
>     shiftl :: [a] -> [a]
>     shiftl [] = []
>     shiftl x = [last x] ++ init x
>
> -Eric
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list