Search code examples
functional-programmingsmlsmlnj

Pattern matching for lambdas?


Is it possible to have pattern matching of arguments and casing for an anonymous function? If so, what is the syntax?

Ipsum lorem


Solution

  • It's exactly the same as for named functions:

    - (fn 0 => 1 | x => 34) 1;
    val it = 34 : int
    
    - (fn (_::y::_) => y) [1,2,3];
    val it = 2 : int
    

    (A warning was omitted in the second example.)