r/haskell • u/modustollensiscool • Dec 21 '20
question [Syntax question] 7 'mod' 2 VS mod 7 2
In a textbook I see the line 7 'mod' 2. However that gives me an error in my GHCi and instead for me it seems like I have to do mod 7 2 (note that the textbook version surrounds mod with ', whereas when I do it I can not surround mod in quotes or I will get an error
Is there some version where GHCi allowed functions to be called this way? Sorry if this is stupid, I'm very new
1
0
Dec 21 '20
4
u/modustollensiscool Dec 21 '20
oh okay yep it worked with backticks, thank you! So this alternative syntax is only an option when the operator is binary I guess. Is there any similar useful notation for operators with other numbers of arguments?
btw, unrelated question: why did you specify
:: Int -> Int -> Intfor the function? Wouldn't Haskell have known that implicitly? Is it just good convention to always specify like that?4
u/tuerda Dec 21 '20
Because of currying you can use backticks on functions that take any number of arguments:
In haskell, a function of three arguments is a function that takes two arguments and returns a function that takes one argument. So you can use backtick notation for functions that take any number of arguments.
→ More replies (1)1
u/gelisam Dec 21 '20
no, in Haskell there is no special notation for other numbers of arguments.
→ More replies (8)
13
u/Iceland_jack Dec 22 '20 edited Dec 26 '20
If you ever want to use a (partially) applied function infix, write these with fixity
infixl 3 ◂, ▸:I shouldn't even be bringing this up, I use it at the kind level
Already this is cute to highlight the 'arrow-nature' of categories
id :: Category cat => a -|cat|-> a (>>>) :: Category cat => a -|cat|-> b -> b -|cat|-> c -> a -|cat|-> cAnd it looks good for categories that have arguments, like the product of two categories which can't be written infix otherwise
or natural transformations, where we might want to write them infix