Pattern matching multiple variables

Javier Lorenzini

I'm new to F# and haven't seen anywhere how to do the following correctly

let fun1 a b =
match a b with
| a + b > 0 -> fun2
| a + b < 0 -> fun3

I have to do this with pattern matching—for homework—but I don't know how to do it correctly. I thought I could be use tuples as follows:

let fun1 (a,b) =
match (a,b) with
| (a,b) when a + b > 0 -> fun2
| (a,b) when a + b < 0 -> fun3

But that doesn't work too. How do I proceed?

Søren Debois

You have the right idea, you just forgot that indentation matters in F#. Adding spaces makes you solution work:

let fun1 (a,b) =  
    match (a,b) with
    | (a,b) when a + b > 0 -> fun2
    | (a,b) when a + b < 0 -> fun3

This actually patterns matches twice, so we might want to rewrite it:

let fun1 t = function 
    | (a,b) when a + b > 0 -> fun2
    | (a,b) when a + b < 0 -> fun3

The compiler/interpreter will let this go through, but with a warning:

warning FS0025: Incomplete pattern matches on this expression.

I'll leave getting rid of that as an exercise :-)

PS. Good of you to be upfront about this being homework.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Values of variables for pattern matching

From Dev

Pattern matching with variables

From Dev

Pattern matching with variables

From Dev

Regex matching multiple pattern

From Dev

Multiple pattern matching

From Dev

Pattern matching across multiple lines

From Dev

Pattern matching covering multiple cases

From Dev

Multiple string/ pattern matching in R

From Dev

Pattern matching across multiple lines

From Dev

Pattern matching covering multiple cases

From Dev

Regexp - Pattern matching for multiple characters

From Dev

Regex Pattern (...)+ not matching multiple times

From Dev

Is is possible to refer to Type Variables in pattern matching?

From Dev

Standard ML (smlnj): "constant" variables for pattern matching

From Dev

Bash brace expansion with variables for pattern matching

From Dev

Need help in pattern matching in Java for multiple braces

From Dev

Matching when an arbitrary pattern appears multiple times

From Dev

make: pattern rule matching multiple extensions

From Dev

Scala pattern matching against something of multiple types

From Dev

Matching pattern multiple times in same string with regex

From Dev

Scala pattern matching aggregate multiple matches

From Dev

Pattern matching multiple constructors in a single clause in Coq

From Dev

Redis: how to delete multiple keys matching pattern?

From Dev

Python multiple pattern matching from a log file

From Dev

Multiple repeated groups pattern matching in Java

From Dev

regular expression replace multiple matching pattern with link

From Dev

Scala string pattern matching by multiple possible groups

From Dev

Scala pattern matching multiple combinator parsers results

From Dev

Execute command on multiple files matching a pattern in parallel

Related Related

  1. 1

    Values of variables for pattern matching

  2. 2

    Pattern matching with variables

  3. 3

    Pattern matching with variables

  4. 4

    Regex matching multiple pattern

  5. 5

    Multiple pattern matching

  6. 6

    Pattern matching across multiple lines

  7. 7

    Pattern matching covering multiple cases

  8. 8

    Multiple string/ pattern matching in R

  9. 9

    Pattern matching across multiple lines

  10. 10

    Pattern matching covering multiple cases

  11. 11

    Regexp - Pattern matching for multiple characters

  12. 12

    Regex Pattern (...)+ not matching multiple times

  13. 13

    Is is possible to refer to Type Variables in pattern matching?

  14. 14

    Standard ML (smlnj): "constant" variables for pattern matching

  15. 15

    Bash brace expansion with variables for pattern matching

  16. 16

    Need help in pattern matching in Java for multiple braces

  17. 17

    Matching when an arbitrary pattern appears multiple times

  18. 18

    make: pattern rule matching multiple extensions

  19. 19

    Scala pattern matching against something of multiple types

  20. 20

    Matching pattern multiple times in same string with regex

  21. 21

    Scala pattern matching aggregate multiple matches

  22. 22

    Pattern matching multiple constructors in a single clause in Coq

  23. 23

    Redis: how to delete multiple keys matching pattern?

  24. 24

    Python multiple pattern matching from a log file

  25. 25

    Multiple repeated groups pattern matching in Java

  26. 26

    regular expression replace multiple matching pattern with link

  27. 27

    Scala string pattern matching by multiple possible groups

  28. 28

    Scala pattern matching multiple combinator parsers results

  29. 29

    Execute command on multiple files matching a pattern in parallel

HotTag

Archive