# Pascals triangle in Haskell … or why point-free sometimes is pointless

Today I stumpled upon a very nice and concise (but quite hard to understand) point-free implementation of Pascals-Triangle in Haskell:

import Control.Applicative1)<*>jQuery("#footnote_plugin_tooltip_1").tooltip({tip:"#footnote_plugin_tooltip_text_1",tipClass:"footnote_tooltip",effect:"fade",fadeOutSpeed:100,predelay:400,position:"top right",relative:true,offset:[2,2]});

pascal :: [[Int]]
pascal = iterate newrow [1]
where newrow = (zipWith (+) <*> tail) . (0:) . (++[0])


let’s first confirm that this really works (…magic):

λ> take 5 \$ pascal
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]


Seems to work.

## But how …

Of course I know how to get a pascals triangle and I don’t have a problem with iterate.

But the usage of applicative in newrow (together with some doubt about precedence of function application and <*>)
made this very short snippet quite hard to understand for me.

So let’s flesh out newrow out a bit:

newrow = (zipWith (+) <*> tail) . (0:) . (++[0])
==
newrow = 2)zipWith (+jQuery("#footnote_plugin_tooltip_2").tooltip({tip:"#footnote_plugin_tooltip_text_2",tipClass:"footnote_tooltip",effect:"fade",fadeOutSpeed:100,predelay:400,position:"top right",relative:true,offset:[2,2]}); <*> tail) . (0:) . (++ [0])


The last two functions are easy: they just append and prepend a 0 each, so if we look at the types we get:

λ> :t (0:) . (++[0])
(0:) . (++[0]) :: Num a => [a] -> [a]


and we are left with

(zipWith (+)) <*> tail


Now the hard part to understand is what instance of Applicative we are
really using here – and it’s not [] it’s Num a => 3)->) [a])! So the applicative instance are functions taking a list of numbers somewhere. And now of course the types match niecely: λ> :t ...continuejQuery("#footnote_plugin_tooltip_3").tooltip({tip:"#footnote_plugin_tooltip_text_3",tipClass:"footnote_tooltip",effect:"fade",fadeOutSpeed:100,predelay:400,position:"top right",relative:true,offset:[2,2]}); -> ([a] -> [a]) -> ([a] -> [a]) 

So what this really does is taking an row of numbers row, prepends and appends a 0 to it: 0:row++[0]. And then it takes the tail of this and zips the two versions with (+): zipWith (+) (row++[0]) (0:row++[0]).

 That’s it. why not just … pascal :: [[Int]] pascal = iterate newrow [1] where newrow row = zipWith (+) (0:row) (row++[0]) This is in my opinion easier to read, needs fewer knowledge (don’t need to know about applicatives) and is even shorter! The only difference is that this does not force point-free style down your throat. Ok – I cheated a bit (because where is the point in making the list bigger on both sides if zip and tail only uses parts of it anyway). But I think this is another good case of why sometimes point-free is indeed pointless and should be avoided to make your code more read- and understandable. Remember: the next person that’s to stupid to understand your clever code is most likely you in 2 months 😀 Share this:Click to share on Twitter (Opens in new window)Click to share on Facebook (Opens in new window)Click to share on Google+ (Opens in new window)Like this:Like Loading... Related References   [ + ] 1. ↑ <*> 2. ↑ zipWith (+ 3. ↑ ->) [a])! So the applicative instance are functions taking a list of numbers somewhere. And now of course the types match niecely: λ> :t zipWith (+) zipWith (+) :: Num a => [a] -> ([a] -> [a]) λ> :t tail tail :: [a] -> [a] λ> :t (<*>) (<*>) :: Num a => ([a] -> ([a] -> [a] function footnote_expand_reference_container(){jQuery("#footnote_references_container").show();jQuery("#footnote_reference_container_collapse_button").text("-");}function footnote_collapse_reference_container(){jQuery("#footnote_references_container").hide();jQuery("#footnote_reference_container_collapse_button").text("+");}function footnote_expand_collapse_reference_container(){if(jQuery("#footnote_references_container").is(":hidden")){footnote_expand_reference_container();}else{footnote_collapse_reference_container();}}function footnote_moveToAnchor(p_str_TargetID){footnote_expand_reference_container();var l_obj_Target=jQuery("#"+p_str_TargetID);if(l_obj_Target.length){jQuery('html, body').animate({scrollTop:l_obj_Target.offset().top-window.innerHeight/2},1000);}} 
 
 Post navigation ← Previous Post Puzzle: prisoners and switchesNext Post → Solution to the prisoner puzzle var disqus_url='https://gettingsharper.de/2014/11/04/pascals-triangle-in-haskell-or-why-point-free-sometimes-is-pointless/';var disqus_identifier='706 http://gettingsharper.de/?p=706';var disqus_container_id='disqus_thread';var disqus_shortname='gettingsharper';var disqus_title="Pascals triangle in Haskell &#8230; or why point-free sometimes is pointless";var disqus_config_custom=window.disqus_config;var disqus_config=function(){this.language='';this.callbacks.onReady.push(function(){var script=document.createElement('script');script.async=true;script.src='?cf_action=sync_comments&post_id=706';var firstScript=document.getElementsByTagName('script')[0];firstScript.parentNode.insertBefore(script,firstScript);});if(disqus_config_custom){disqus_config_custom.call(this);}};(function(){var dsq=document.createElement('script');dsq.type='text/javascript';dsq.async=true;dsq.src='//'+disqus_shortname+'.disqus.com/embed.js';(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(dsq);})();