Matching casting types

I am new to F # and follow the tutorials to try to make part of the code, but it is not.

I create types of solitary and smoking sports through inheritance.

Then I use pattern matching to find out the type, and if it's a chicken coop, get the number of players as well. Then rank accordingly.

However, I get errors. I followed Microsoft’s lead on this, and I really don’t understand the error. I do not have a functional programming background.

type Sport (name: string) =
    member x.Name = name

type Individual(name: string) =
    inherit Sport(name)

type Team(name: string, numberOfPlayers : int) =
    inherit Sport(name)
    member x.numberOfPlayers = numberOfPlayers


let MK = new Individual("Combate Mortal")
let SF = new Individual("Lutadores de Rua")
let Tk = new Individual("Tekken Chupa")

let MvC = new Team("Marvel Contra Capcom", 3)
let Dbz = new Team("Bolas do Dragao", 3)

let interpretSport (sport:string) (players:int)  =
    match sport with
    | "Combate Mortal" -> printfn "Rank1"  
    | "Lutadores de Rua" -> printfn "Rank2"
    | "Tekken Chupa" -> printfn "Rank3"
    | "Bolas do Dragao" -> printfn "Rank4. No of players: %d " players
    | "Marvel Contra Capcom" -> printfn "Rank5. No of players: %d" players
    | _ -> printfn "not a sport in our list..." 



let matchSport (sport:Sport)  = 
    match sport with
    | :? Individual -> interpretSport(sport.Name)
    | :? Team as teamSport -> interpretSport(teamSport.Name,teamSport.numberOfPlayers)
    | _ -> printfn "not a sport" 

matchSport(MK)
matchSport(SF)
matchSport(Tk)
matchSport(MvC)
matchSport(Dbz)

1st error when calling a function with more than 1 argument:

Second printing error:

+4
source share
4 answers

, , , F #, , . :

let interpreteSport (sport:string) (player:int) =

F # , #, , , , , . , , :

:? Individual -> interpretSport(sport.Name)

, , .

! , , ? , , , interpreteSport, , F #. , , , " ", , . , :

let parzFun = interpretSport sport.Name 

, , :

let result = parzFun 1

, , 'int -> unit': F # :

a -> b -> c -> d -> retval, a, b, c, d .. , retVal - .

interpreteSport : string -> int -> unit, unit - , " ", # void, , - , , void - , void #.

, , , (), , , int -> unit, , .

, , , 2 int -> unit, , .

- , , , , :

:? Team as teamSport -> interpretSport(teamSport.Name,teamSport.numberOfPlayers)

, , : 2 , , , , . , , : - , : ('a *' b) - , F # : , 'a (generic, case) ' b (generic, integer). , :

:? Team as teamSport -> interpretSport teamSport.Name teamSport.numberOfPlayers

, , , , , int -> unit (, ), unit, , unit (interpreteSport printfn). , , , :

let matchSport (sport:Sport)  = 
    match sport with
    | :? Individual -> interpretSport sport.Name 1
    | :? Team as teamSport -> interpretSport teamSport.Name teamSport.numberOfPlayers
    | _ -> printfn "not a sport" 
+6

interpretSport , . :

| :? Individual -> interpretSport sport.Name 1

, , . :

| :? Team as teamSport -> interpretSport teamSport.Name teamSport.numberOfPlayers
+5

F #, . F # .

, , :

type Players =
    | Individual
    | Team of numberOfPlayers:int

type Sport = { Name : string; Players : Players }

let MK = { Name = "Combate Mortal"; Players = Individual }
let SF = { Name = "Lutadores de Rua"; Players = Individual }
let Tk = { Name = "Tekken Chupa"; Players = Individual }

let MvC = { Name = "Marvel Contra Capcom"; Players = Team 3 }
let Dbz = { Name = "Bolas do Dragao"; Players = Team 3 }

let interpretSport (sport:Sport) =
    let players =
        match sport.Players with
        | Individual -> ""
        | Team numberOfPlayers -> sprintf ". No of players: %d" numberOfPlayers
    let rank =
        match sport.Name with
        | "Combate Mortal" -> Some 1
        | "Lutadores de Rua" -> Some 2
        | "Tekken Chupa" -> Some 3
        | "Bolas do Dragao" -> Some 4
        | "Marvel Contra Capcom" -> Some 5
        | _ -> None
    match rank with
    | Some r -> printfn "Rank%d%s" r players
    | None -> printfn "not a sport in our list..."
+5

. matchSport, interpSport . :

Team as teamSport -> interpretSport teamSport.Name teamSport.numberOfPlayers

, interpretSport , , int -> unit, unit, , , . 1 :

Individual -> interpretSport sport.Name 1

But you probably want to use the sport that you linked before (perhaps in the list that you give as a parameter) to perform the check. In general, it’s a bad idea (in functional programming and elsewhere) to hard code many lines, you probably want to make some sort of List or Map of the Sports to the Ranks association, then collapse with a list of sports and compare when they meet with staff or team, and then print everything that the Card gives you in this case. It will be shorter and more extensible.

+3
source

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


All Articles