[Haskell-beginners] Creating a Triple List from a List
Francesco Ariis
fa-ml at ariis.it
Sun Feb 28 02:29:27 UTC 2021
Il 27 febbraio 2021 alle 18:07 A. Mc. ha scritto:
> Hello,
>
> What is the best way to take:
> [1, 2, 3, 4 ]
>
> And convert it to:
>
> [ [ [ 1 ], [ 2 ] ], [ [3]. [4] ] ]
>
> so that each member pair is:
>
> [ [1], [2] ]
>
> roughly analogous to a 1x2 vector?
A 1×2 vector would be
> [[1 2], [3, 4]]
am I wrong? If so, a quick and dirty solution could be
d2 :: [a] -> [[a]]
d2 [] = []
d2 [a] = error "odd element"
d2 as = let (is, es) = splitAt 2 as
in is : d2 es
-- λ> d2 [1..6]
-- [[1,2],[3,4],[5,6]]
More information about the Beginners
mailing list