Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use irrefutable tilde (`~`) patterns in lambda expressions?

Tags:

GHC gives me a parse error on input \~ if I try to put a tilde on the pattern of a lambda expression like I can do with named functions

let step = \~(x,s) -> run (f x) s  -- Parse Error  let step ~(x, s) = run (f x) s -- Works fine 
like image 916
hugomg Avatar asked Nov 01 '12 21:11

hugomg


People also ask

Which design pattern can you implement to encapsulate conditional logic in a lambda expression?

The Lambda-enabled Command Pattern.

Which sentence about lambda statements is not true?

Which is NOT true about lambda statements? A statement lambda cannot return a value.

What are the two conditions required for using a lambda function in a stream?

In order to match a lambda to a single method interface, also called a "functional interface", several conditions need to be met: The functional interface has to have exactly one unimplemented method, and that method (naturally) has to be abstract.

Which of the following statement is true about lambda expression?

What is the correct statement about lambda expression? Explanation: Return type in lambda expression can be ignored in some cases as the compiler will itself figure that out but not in all cases. Lambda expression is used to define small functions, not large functions.


1 Answers

You have to add a space between the lambda and the tilde

\ ~(x,s) -> run (f x) s 

The source of the confusion is because \ and ~ are both valid characters for user defined operators so \~ is parsed as one instead of being parsed as the start of a lambda expression:

-- Defining a custom \~ operator is allowed: let a \~ b = {- ... -}  
like image 191
hugomg Avatar answered Oct 02 '22 13:10

hugomg