[Haskell-beginners] Function result being inversed

Daniel Fischer daniel.is.fischer at googlemail.com
Tue Jan 25 21:07:02 CET 2011


On Tuesday 25 January 2011 21:00:11, Xavier Shay wrote:
> Hello,
> I am confused by the following code.
> I would expect results of True, False.
>
> $ ghci
> *Main> let f x = x 4
> *Main> f(<) 3
> False
> *Main> f(<) 5
> True

It's because

f (<) k = (f (<)) k = ((<) 4) k = (<) 4 k = 4 < k

or, shorter,

(<) 4 = (4 <)

>
> This came about because I was trying to refactor a sort function I
> wrote:
>
>    mySort [] = []
>    mySort (h:t) =
>      (f (<= h)) ++ [h] ++ (f (> h))
>      where f x = mySort (filter x t)
>
> I came up with this, which appears to work, though the comparison
> operators are backwards.
>
>    mySort [] = []
>    mySort (h:t) =
>      f(>) ++ [h] ++ f(<=)
>      where f x = mySort (filter (x h) t)

A right operator section,

(<*> x)

translates to

flip (<*>) x

in prefix notation.

>
> Cheers,
> Xavier



More information about the Beginners mailing list