Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sections - why do I need backticks here?

Tags:

haskell

I am trying to understand sections and think I have got it. Basically it is a way to apply partial application to binary operators. So I understand all the (2*), (+1), etc. examples just fine.

But in the O'Reilly Real World Haskell book, Sections 'section' :) it has this example:

(`elem` ['a'..'z']) 'f'
>True

I understand the need for the parentheses - ie the section syntax. But why do I need the backticks?

If I try, I get:

(elem ['a'..'z']) 'f'

<interactive>:220:19:
    Couldn't match expected type `[[Char]]' with actual type `Char'
    In the second argument of `elem', namely 'f'
    In the expression: (elem ['a' .. 'z']) 'f'
    In an equation for `it': it = (elem ['a' .. 'z']) 'f'
like image 363
Angus Comber Avatar asked Dec 19 '13 11:12

Angus Comber


Video Answer


1 Answers

In Haskell, the backtick turns a name to an infix operator:

a `elem` b = elem a b

So

(`elem` b) a = (\x -> x `elem` b) a
             = a `elem` b
             = elem a b

While

(elem b) a = elem b a
like image 151
kennytm Avatar answered Oct 20 '22 15:10

kennytm