简体   繁体   中英

F# version of haskell pattern match

How do I do this haskell in F# cleanly?

add 1 2 x = 3 + x
add 1 x y = 1 + x + y
add z x y = z + x + y

You can't overload the function itself, but you can use pattern matching directly:

let add z x y =               // curried multiple parameters
    match z, x, y with        // convert to three-tuple to match on
    | 1, 2, x -> 3 + x
    | 1, x, y -> 1 + x + y
    | z, x, y -> z + x + y

Usage is as expected: add 1 2 3

If you're willing to use tuples as arguments (ie forgo currying and partial application), you can even write it more shorthand:

let add =                     // expect three-tuple as first (and only) parameter
    function                  // use that one value directly to match on
    | 1, 2, x -> 3 + x
    | 1, x, y -> 1 + x + y
    | z, x, y -> z + x + y

Usage now is: add (1, 2, 3)

Recall in Haskell that the general form of functions as a list of declarations with patterns:

f pat1 ... = e1
f pat2 ... = e2
f pat3 ... = e3

is just sugar for the case analysis:

f x1 .. xn = case (x1, .. xn) of
                (pat1, ..., patn) -> e1
                (pat2, ..., patn) -> e2
                (pat3, ..., patn) -> e3

so the same translation can be made to other languages with pattern matching but without declaration-level patterns.

This is purely syntactic. Languages like Haskell, Standard ML and Mathematica allow you to write out different match cases as if they were different functions:

factorial 0 = 1
factorial 1 = 1
factorial n = n * factorial(n-1)

whereas languages like OCaml and F# require you to have a single function definition and use match or equivalent in its body:

let factorial = function
  | 0 -> 1
  | 1 -> 1
  | n -> n * factorial(n-1)

Note that you don't have to copy the function name over and over again using this syntax and you can factor match cases more easily:

let factorial = function
  | 0 | 1 -> 1
  | n -> n * factorial(n-1)

As yamen wrote, do currying with let fab = match a, b with ... in F#.

In the classic red-black tree implementation, I find the duplication of the function names and right-hand sides in Standard ML and Haskell quite ugly:

balance :: RB a -> a -> RB a -> RB a
balance (T R a x b) y (T R c z d) = T R (T B a x b) y (T B c z d)
balance (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d)
balance (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d)
balance a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d)
balance a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d)
balance a x b = T B a x b

compared to the equivalent OCaml or F#:

let balance = function
  | B, z, (T(R, y, T(R, x, a, b), c) | T(R, x, a, T(R, y, b, c))), d
  | B, x, a, (T(R, z, T(R, y, b, c), d) | T(R, y, b, T(R, z, c, d))) ->
      T(R, y, T(B, x, a, b), T(B, z, c, d))
  | a, b, c, d -> T(a, b, c, d)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM