Sunday 3 August 2008

Join-ing the Blogosphere

I'd like to share a little Haskell snippet (I wanted to say "idiom" but actually I'm not sure if I've seen it used anywhere else...) that I quite like:


join (,) 9


What does this do? On its own, not much - in fact you just get:


(9,9)


...which you'd have been much better off typing in yourself. Where it can come-in handy though is when writing code in a pointfree style if you have a need to duplicate an anonymous value into both parts of a pair:


functionWhichTakesAPair . join (,) . functionWhichProducesASingleValue


Anyway, how / why does this construct work....?

The (,) is just the standard data constructor for a 2-tuple, a pair. (This constructor has type a -> b -> (a,b) so you can always write (,) 9 9 as an alternative notation for (9,9) if you wish).

The join is the standard function :: Monad m => m (m a) -> m a we know from monadic programming (Control.Monad). But this just raises the question of how it can make sense to apply it to (,) which as we've seen is a function of type a -> b -> (a,b).

The answer is of course that this function may be considered to be of type m (m a). This is because Control.Monad.Instances declares any partially applied function (eg a -> ...) to be a monad (the type a can be seen as representing an encapsulated environment which is threaded through by the monadic bind). This then means that a function of two arguments (such as (,)) can be seen as having type m (m a) which is just what we need to be able to apply join. The net result is that the single argument of the resulting function (join (,)) is passed in twice as desired.

This of course generalises to functions other than (,) - whenever we use join f where f is a function of two arguments, we end up with a function of one argument which is passed into f twice.

9 comments:

beroal said...

Interesting. Does it mean that a function monad is like combinatory logic?

return=const=K
join=duplicate=W

Sharon Wood said...

Norton toll-free number
Avast antivirus toll-free number
McAfee toll-free phone number

Smith Leo said...

I am Grateful™ that you shared this informational post. ✆ Your website has everything that I have been looking for so long. Your knowledge about this topic is quite impressive. I am amazed by the content you shared on this website.☞ I loved this page so much that I have bookmarked it for future reference. Thank you for this valuable piece of information. Sadly I could not find any additional information on this page. The information available here is similar to other websites. I like that your understanding about this subject is very deep. Visit௹☞ Telstra WebmailPlumbers Near Mewww.Norton.com/setupwww.office.com/setupwww.office.com/setupNurton.com/nu16

Sam Dew said...

office.com/setup - To Install Office Setup office.com/setup Enter Office Setup Product Key and activate, setup office product, visit Microsoft Office at office.com/setup and Get Started.

office.com/setup- To get started with your Microsoft Office Installation you need valid product key code & visit www.Office.com/Setup and we can also help you with your entire process to setup office at office.com/setup.

office.com/setup– To Install Office setup Enter office setup product key and activate, setup office, visit Microsoft office at office.com/setup and Get Installed.

Mark Watson said...

For Accessing Xfinity Router, or admin tool in the Xfinity My Account app, you are supposed to follow a few important instructions. To access, First of all, you are supposed to open the app and then sign. Now, you must go to the internet, and then change wifi settings and other settings. Now, if you have not set a username and password, yet you can simply enter admin and password.

Eva winget said...

Amazin post please keep posting such kind of posts
canonprintersupport

After Prom Parties NYC - Prom Time said...

The After Prom Party is the ultimate way to extend the excitement of prom night, allowing students to continue celebrating with friends in a fun and safe environment. It's a chance to dance, enjoy food and drinks, and create lasting memories before parting ways. The after party adds an extra touch of excitement and glamour to the overall prom experience, leaving attendees with unforgettable moments to treasure.

12Panelnow said...

I recently came across these incredible oxy stripst and k2 strips, and I must say, they have revolutionized the way I drug test. OxyStrips provide an accurate and reliable way to detect the presence of oxycodone, ensuring safety and peace of mind.

Harb Plumber said...

For top-notch drain cleaning services in Mississauga, look no further than our professional plumbers. We specialize in tackling stubborn clogs and restoring optimal flow to your drains. Using advanced tools and techniques, our experienced team can effectively clear blockages caused by debris, grease, hair, or other common culprits. We understand the inconvenience and potential damage that clogged drains can cause, which is why we offer prompt and reliable services to ensure your plumbing system runs smoothly. Don't let clogged drains disrupt your daily routine – contact us today for expert Plumber Drain Cleaning Mississauga