Monday, 15 February 2010

pointfree - Point free programming style in haskell (ghci) -


I came to a snippet of code for computing combinations in Haskell, and I thought I would play in ghci to understand it how it works. In Ghci, when I just run a bit of code that I am interested in, fails as shown below.

  Prelude data. List & gt; Filter ((== 2) length). Later [1,2,3] & lt; Interactive>: 19: 1: Any instances ('[a0] - & gt; [[a0]]) is used by the use of' print ', an interactive GHCI command: print it, print data. List & gt; Filter ((== 2) length). Later [1,2,3] & lt; Interactive & gt; 20: 28: Expected type 'a - & gt; [[A1]] 'with the actual type' [[A]] 'in the relevant binding include: a - & gt; Possible reason: ['A1]' (binding on 20: 1) is followed by 'many arguments' in the second argument of' (.) ', Namely' [1, 2] , 3] 'in expression: filter ((== 2) length). Later [1, 2, 3]  

I knew that the structure should be caused by the operator (dot) in Ghci in order to use that "$" sign to run that code Had to be changed as shown below

  preedal data.list & gt; Filter ((== 2). Length) $ [1,2,3] [[1,2], [1,3], [2,3]]  

Someone has a little to explain about what is going on internally when you write code in this programming style? And when I use that expression directly, why not fail?

This is your operator's prioritization of code

  (filter ((== 2) length)) is being read as. (Later [1,2,3])  

and then the GHCI complains that is not later [1,2,3] function, therefore, You need brackets:

  (filter ((== 2) length). Later [1,2,3]  

or < Code> $ :

Filters ((== 2.) Length). Afterwards $ [1,2,3]

No comments:

Post a Comment