[Haskell-beginners] Solution to problem 17 (99 Haskell problems)
Christopher Reichert
creichert07 at gmail.com
Mon Dec 8 14:53:20 UTC 2014
On Mon, Dec 08 2014, jean lopes <hawu.bnu at gmail.com> wrote:
> Hello, I am wondering, is this solution acceptable to the question 17 ? (
> https://www.haskell.org/haskellwiki/99_questions/11_to_20)
>
> code:
> split :: [a] -> Int -> ([a], [a])
> split xs y = fst (mapAccumL (\a b -> (let f = fst a
> s = snd a
> l = length f
> in if l < y
> then (f ++ [b], s)
> else (f, s ++ [b]), b)) ([],[]) xs)
The example worked for me, so yes! It's a bit hard to read, though.
Stylistically, I would move the the let expressions into a where block
for readability. That would open up a few other pattern matching
opportunities. e.g.
split :: [a] -> Int -> ([a], [a])
split xs y = fst (mapAccumL go ([],[]) xs)
where
go (f,s) b = if length f < y
then ((f ++ [b], s), b)
else ((f, s ++ [b]), b)
Cheers,
-Christopher
>
> Also, any inputs are welcome (indentation, misuse of something)
>
> Best regards,
> Jean Lopes
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
--
Christopher Reichert
irc: creichert
gpg: C81D 18C8 862A 3618 1376 FFA5 6BFC A992 9955 929B
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141208/b488f839/attachment.sig>
More information about the Beginners
mailing list