[Haskell-cafe] List operation question
ihope
ihope127 at gmail.com
Mon Feb 5 17:21:41 EST 2007
On 2/4/07, Eric Olander <olandere at gmail.com> 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 last element to the head of the list
> shiftl :: [a] -> [a]
> shiftl [] = []
> shiftl x = [last x] ++ init x
Well, you could try this, though I'm actually sure it's any faster:
> shiftl (x1:x2:xs) = last:x1:init
> where last:init = shiftl (x2:xs)
> shiftl [x] = [x]
> shiftl [] = error "shiftl: empty list"
Or, if you don't want to give an error on [], omit the last line and
replace both of the [x] with xs.
More information about the Haskell-Cafe
mailing list