Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# "this rule will never be matched" expression in a NOT recursive function

I have such a function:

let RotateFace (face: int, turns: int) =
    match face with
        | upperface -> 
            TiltCube(2)
            TwistCube(turns)
            TiltCube(2)
        | leftface -> 
            TiltCube(3)
            TwistCube(turns)
            TiltCube(1)
        | frontface -> 
            TurnCube(1)
            TiltCube(3)
            TwistCube(turns)
            TiltCube(1)
            TurnCube(3)
        | rightface -> 
            TiltCube(1)
            TwistCube(turns)
            TiltCube(3)
        | backface -> 
            TiltCube(3)
            TwistCube(turns)
            TiltCube(1)
        | downface -> 
            TurnCube(3)
            TiltCube(3)
            TwistCube(turns)
            TiltCube(1)
            TurnCube(1)
        | _ -> ()

I have a "this rule will never be matched" problem on my cases "'upperface', 'leftface', 'frontface', 'rightface', 'backface', 'downface', and '_'.".

I've looked this link;

f# match expression - "rule will never be matched"

but to be honest I didn't understand what should I do with my case.

like image 595
yusuf Avatar asked Dec 29 '14 20:12

yusuf


1 Answers

You can't match against a variable, I mean you can but if you do so what would happen is that the variable will be bound to that value and it seems to me that's not what you are trying to do, otherwise the first case in your code will "eat" all other cases and that's why the following rules will never be matched.

You can either match with a condition:

let RotateFace (face: int, turns: int) =
    match face with
        | x when x = upperface -> 
            TiltCube(2)
            TwistCube(turns)
            TiltCube(2)

or declare upperface and the other variables as constants by using the literal attribute:

[<Literal>]
let Upperface  = 4

let RotateFace (face: int, turns: int) =
    match face with
        | Upperface -> 
            TiltCube(2)
            TwistCube(turns)
            TiltCube(2)
like image 184
Gus Avatar answered Nov 07 '22 10:11

Gus