
In looking through a merge sort implementation example, I came across this code: split :: [a] -> [[a]] split [] = [] split (x:xs) = [x] : split xs I would have written the same code as split :: [a] -> [[a]] split = map (: []) Is there any particular difference here (other than explicit recursion)? And is there any other nicer way to write it? Thanks, Ben Sanders

On 29 Apr 2009, at 21:00, Ben Sanders wrote:
In looking through a merge sort implementation example, I came across this code:
split :: [a] -> [[a]] split [] = [] split (x:xs) = [x] : split xs
I would have written the same code as
split :: [a] -> [[a]] split = map (: [])
Is there any particular difference here (other than explicit recursion)? And is there any other nicer way to write it?
How about map pure. I do like the robot monkey though :) Bob

On Apr 29, 2009, at 16:12 , Ben Sanders wrote:
How about map pure. I do like the robot monkey though :)
What is 'map pure'?
It uses the applicative functor (Control.Applicative) instance for lists, just as "fmap return" uses the monad instance. Both work out to the same thing. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

"Brandon S. Allbery KF8NH"
How about map pure. I do like the robot monkey though :)
What is 'map pure'?
It uses the applicative functor (Control.Applicative) instance for lists, just as "fmap return" uses the monad instance. Both work out to the same thing.
In fact, I'd choose a combination of both, split = fmap pure which seems to be most elegant to me. It's more general than both of them. I just took 'return' instead of 'pure', because you don't need any extra modules for that. Greets, Ertugrul. -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/

Ben Sanders
In looking through a merge sort implementation example, I came across this code:
split :: [a] -> [[a]] split [] = [] split (x:xs) = [x] : split xs
I would have written the same code as
split :: [a] -> [[a]] split = map (: [])
Is there any particular difference here (other than explicit recursion)?
No.
And is there any other nicer way to write it?
Yes: split = fmap return Greets, Ertugrul. -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/
participants (4)
-
Ben Sanders
-
Brandon S. Allbery KF8NH
-
Ertugrul Soeylemez
-
Thomas Davie