# Simplifying a 'split' function

6 messages
Open this post in threaded view
|

## Simplifying a 'split' function

 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
Open this post in threaded view
|

## Simplifying a 'split' function

 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
Open this post in threaded view
|

## Re: Simplifying a 'split' function

 In reply to this post by Ben-24 Ben Sanders <[hidden email]> 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)? 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/
Open this post in threaded view
|

## Simplifying a 'split' function

 In reply to this post by Thomas Davie > How about map pure. ?I do like the robot monkey though :) > > Bob What is 'map pure'? And it took me a minute to figure out where you were getting 'robot monkey' from :) > split = fmap return >Greets, >Ertugrul. Awesome!  Thanks!!