Personal tools

Cum definim operatorii binari ?

From HaskellWiki

Revision as of 05:53, 31 May 2009 by Ha$kell (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Ca pe orice functie binara, dar numele ei il veti scrie punind numele operatorului in paranteza rotunda.

Ex:

suma :: Int -> Int -> Int suma a b = a + b

poate deveni

(+++) :: Int -> Int -> Int (+++) a b = a + b

iar cu o declaratie infix precizati asociativitatea si nivelul de prioritate al operatorului.


Varianta: puteti folosi o functie binara obisnuita ca un operator infixat punandu-i numele intre `apostroafe inverse`.

Ex:

3 `suma` 4

va calcula tot : suma 3 4 adica 3 + 4 cf. def. de sus.