Reputation: 65
I'm trying to create a function that accepts a list of cards and returns a list without a specific card in it. How do I pass a list of defined types to a function?
I passed a list to the function and it stated my types are not matching.
type Suit = Clubs | Diamonds | Hearts | Spades
type Rank = Jack | Queen | King | Ace | Num of int
type Card = Rank * Suit
type Color = Red | Black
type Move = Discard of Card | Draw
let cards = [(Jack,Clubs); (Num(8),Spades)]
let c:Card = Jack, Clubs
let remove_card (cs, c:Card, e:string) =
cs
remove_card cards c "Err"
Expected list to return properly when calling function remove_card. But, error is thrown:
This expression was expected to have type '('a -> 'b -> 'c) * Card * string' but here has type '(Rank * Suit) list'
Upvotes: 0
Views: 63
Reputation: 3784
Parameter syntax in F# is quite different from other common languages.
Your function - though looks like having 3 parameters - actually has only one parameter which is a tuple of 3 components.
So you have to call it like remove_card (cards, c, "Err")
However, in idiomatic F#, without special requirement, we often don’t use tuple for parameters. Instead, we use separated parameters to leverage partial application when needed:
let remove_card cs (c:Card) (e:string) = ...
let remove_card_withFixedCards = remove_card cards
Upvotes: 2
Reputation: 1473
It's because you've defined your remove_card
function as a function that takes a single tupled parameter and returns a value, but you're calling it like a function that takes three parameters. To call remove_card
the way you have it defined now you'd need to call it like remove_card (cards, card, "Err")
, creating the three-argument tuple and calling the function with it. I think what you probably meant to do was:
let remove_card cardList cardToRemove err =
cardList
remove_card cards c "Err"
i.e. creating a function with three arguments instead of a single tupled argument.
Upvotes: 3