[Haskell-cafe] Idiomatically using lists
kevin birch
kbirch at pobox.com
Mon Jun 4 18:37:56 EDT 2007
On 火, 2007-6月-05, at 02:54, Greg Fitzgerald wrote:
> > rotating the fourth element 2 positions would result in: [1, 2,
> 4, 3, 5]
> Seems odd. Should that be [4,1,2,3,5]?
>
Yes, I meant to use the 5 element in my second example. Sorry for
the confusion.
> > Is there an idomatic way to handle both of these cases in a
> function?
> Generally people like to see your attempt at a solution before
> giving the idomatic one so that they are sure it's not a homework
> question. What do you have so far?
>
Yeah, I only wish I had gone to a school that would be forward
thinking enough to each FP. ;-)
Here is my version:
rotate :: Array Integer Card -> Integer -> Integer -> Array Integer Card
rotate a i n
| i <= u - n = a // [(i, a ! (i + 1)), (i + 1, a ! (i + 2)), (i
+ 2, a ! i)]
| otherwise = a // zip [l..u] (h ++ [a ! i] ++ filter (not . (==
(a ! i))) t)
where (l, u) = bounds a
(h, t) = splitAt (fromInteger ((i - u) + n)) $ elems a
This function is part of my implementation of the Solitaire
encryption algorithm, so that is why I have the reference to a Card
data type. This does what I want, and seems basically idiomatic, but
perhaps it could be better.
Thanks,
Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070605/7596aa05/attachment.htm
More information about the Haskell-Cafe
mailing list