Why does F # need a literal character symbol needed between the pipe and when?

I am new to F # and messing around with it. What I need:

let rec fact n =
  match n with 
    | dummy when n < 2 -> 1
    | _ -> n * fact (n - 1)

let x = 6
printfn "%d! = %d" x (fact x)

Why does F # need this dummy placeholder between the pipe and when? A mannequin is undefined all the time, and the compiler somehow needs and ignores it at the same time as the necessary ghost character.

Thanks in advance.

+3
source share
3 answers

The character must not be fictitious and may appear in the when clause. For example, your function may be rewritten:

let rec fact = function
| n when n < 2 -> 1 
| n -> n * fact (n - 1) 

Here, since we use anonymous pattern matching with function, rather than match ... with, the identifier really matters.

,

match p with
| i,j when i < j -> true
| _ -> false

, when, , when - .

, , , _, :

let rec fact n = 
  match n with  
  | _ when n < 2 -> 1 
  | _ -> n * fact (n - 1) 
+10

, dummy ( _), . match , - , .

, if ... then match:

let rec fact n = 
  if n < 2 then 1 
  else n * fact (n - 1) 

, ( , - match, , ).

+9

dummysimply means a value (or a more accurate image) that may correspond to a value between matchand with. It is not visible outside your function, only locally for the first case.

As kvb said, you should not call it dummy, it can be any valid F # identifier.

It is very powerful because it can be used to decompose types, for example, tuples or a list:

match l with
| head::tail -> tail // returns tail
| _ -> [] // returns an empty list
+1
source

Source: https://habr.com/ru/post/1762158/


All Articles