Pattern matching with guards vs if/else construct in F#

前端 未结 4 791
予麋鹿
予麋鹿 2021-01-12 08:24

In ML-family languages, people tend to prefer pattern matching to if/else construct. In F#, using guards within pattern matching could easily replace if/e

相关标签:
4条回答
  • 2021-01-12 09:07

    I did some testing on a self writen prime number generator, and as far as i can say there is "if then else" is significantly slower than pattern matching, can't explain why though, but I as far as I have tested the imperativ part of F# have a slower run time than recursive functional style when it come to optimal algorithms.

    0 讨论(0)
  • 2021-01-12 09:10

    Agree with @Daniel that pattern matching is usually more flexible. Check this implementation:

    type Solution = | Identity | Roots of float list
    
    let quadraticEquation x =
    
        let rec removeZeros list =
            match list with
            | 0.0::rest -> removeZeros rest
            | _ -> list
        let x = removeZeros x
    
        match x with
        | [] -> Identity // zero constant
        | [_] -> Roots [] // non-zero constant
        | [a;b] -> Roots [ -b/a ] // linear equation
        | [a;b;c] ->
            let delta = b*b - 4.0*a*c
            match delta with
            | delta when delta < 0.0 -> 
                Roots [] // no real roots
            | _ ->
                let d = sqrt delta
                let x1 = (-b-d) / (2.0*a)
                let x2 = (-b+d) / (2.0*a)
                Roots [x1; x2]
        | _ -> failwithf "equation is bigger than quadratic: %A" x
    

    Also notice in https://fsharpforfunandprofit.com/learning-fsharp/ that it is discouraged to use if-else. It is considered a bid less functional.

    0 讨论(0)
  • 2021-01-12 09:15

    Both have their own place. People are more used to If/else construct for checking a value where as pattern matching is like a If/else on steroids. Pattern matching allows you to sort of compare against the decomposed structure of the data along with using gaurds for specifying some additional condition on the parts of the decomposed data or some other value (specially in case of recursive data structures or so called discriminated unions in F#).

    I personally prefer to use if/else for simple values comparisons (true/false, ints etc), but in case you have a recursive data structure or something which you need to compare against its decomposed value than there is nothing better than pattern matching.

    First make it work and make it elegant and simple and then if you seem some performance problem then check for performance issues (which mostly will be due to some other logic and not due to pattern matching)

    0 讨论(0)
  • 2021-01-12 09:23

    The right answer is probably it depends, but I surmise, in most cases, the compiled representation is the same. As an example

    let f b =
      match b with
      | true -> 1
      | false -> 0
    

    and

    let f b =
      if b then 1
      else 0
    

    both translate to

    public static int f(bool b)
    {
        if (!b)
        {
            return 0;
        }
        return 1;
    }
    

    Given that, it's mostly a matter of style. Personally I prefer pattern matching because the cases are always aligned, making it more readable. Also, they're (arguably) easier to expand later to handle more cases. I consider pattern matching an evolution of if/then/else.

    There is also no additional run-time cost for pattern matching, with or without guards.

    0 讨论(0)
提交回复
热议问题