简体   繁体   English

F#版本的haskell模式匹配

[英]F# version of haskell pattern match

How do I do this haskell in F# cleanly? 我如何干净利落地在F#中使用它?

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 用法如预期: 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: 如果你愿意使用元组作为参数(即放弃currying和部分应用),你甚至可以用简写来写它:

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) 现在用法是: add (1, 2, 3)

Recall in Haskell that the general form of functions as a list of declarations with patterns: 回想一下Haskell 中函数一般形式作为带有模式的声明列表:

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

is just sugar for the case analysis: 只是用于case分析的糖:

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: 像Haskell,Standard ML和Mathematica这样的语言允许你写出不同的匹配案例,就像它们是不同的函数一样:

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: 而像OCaml和F#这样的语言要求您拥有单个函数定义并在其正文中使用match或等效:

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#. 正如衙门写的那样,在F#中使用let fab = match a, b with ...

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: 在经典的红黑树实现中,我发现标准ML和Haskell中的函数名称和右侧的重复非常难看:

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#: 与等效的OCaml或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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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