Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Haskell ((< x) y) and (((<) x) y)

Tags:

haskell

I can't wrap my head around why these would be different.:

Prelude> :t ((<) 3)
((<) 3) :: (Num a, Ord a) => a -> Bool
Prelude> ((<) 3) 2
False
Prelude> 

Prelude> :t (< 3)
(< 3) :: (Num a, Ord a) => a -> Bool
Prelude> (< 3) 2
True
Prelude> 

I suspect there's a straightforward answer, but I don't even know what words to use to describe the difference between the first and second cases. I'm happy to edit my question to use proper nomenclature once someone tells me what it is in this case.

like image 455
Greg C Avatar asked Jan 04 '23 14:01

Greg C


1 Answers

(<) 3 applies 3 as the first (left) argument of <. So (<) 3 2 is the same as 3 < 2.

(< 3) is a section that applies 3 as the right operand of <. This is special syntax that can be used with binary infix operators. So (< 3) 2 is the same as 2 < 3.

To apply 3 as the left operand with a section, you would write: (3 <). So (3 <) 2 is the same as 3 < 2.

Demonstrating with lambdas: (< 3) is the same as \x -> x < 3, and (3 <) is the same as \x -> 3 < x.

like image 77
interjay Avatar answered Jan 12 '23 08:01

interjay