Simplifying a 'split' function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Simplifying a 'split' function

Ben-24
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
Reply | Threaded
Open this post in threaded view
|

Simplifying a 'split' function

Thomas Davie

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

Re: Simplifying a 'split' function

Ertugrul Söylemez
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/


Reply | Threaded
Open this post in threaded view
|

Simplifying a 'split' function

Ben-24
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!!
Reply | Threaded
Open this post in threaded view
|

Simplifying a 'split' function

Brandon S Allbery KF8NH
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] [hidden email]
system administrator [openafs,heimdal,too many hats] [hidden email]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090430/c863206e/PGP.bin
Reply | Threaded
Open this post in threaded view
|

Re: Simplifying a 'split' function

Ertugrul Söylemez
"Brandon S. Allbery KF8NH" <[hidden email]> 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.

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/