Christopher King
Christopher King

Reputation: 10941

How to give infixities to operators in lambda?

For example, this does not type check

\cons nil -> 5 `cons` 3 `cons` nil

nor does this

\(#) -> 5 # 3 # nil

Although both of these do

\cons nil -> 5 `cons` nil
\(#) nil -> 5 # nil

Is there a way to assign infixites to operators in lambdas. I tried

infixr 5 #
foo = \(#) nil -> 5 # 3 # nil

which gives an error for no definition of # and

foo = \(infixr 5 #) nil -> 5 # 3 # nil

which is just a syntax error.

What can I do?

Upvotes: 11

Views: 463

Answers (1)

Reid Barton
Reid Barton

Reputation: 14999

Fixity declarations can be local but must accompany definitions, so you'd have to write something like

foo cons nil = 'a' # 'b' # nil
  where (#) = cons
        infixr 5 #

or

foo = \cons nil -> let (#) = cons; infixr 5 # in 'a' # 'b' # nil

etc.

Upvotes: 16

Related Questions