简体   繁体   English

如何在F#中将数组中的项目相乘,例如4批[1 .. 10]

[英]How to multiply items in an array for example 4 lots of [ 1 .. 10 ] in F#

I am trying to put a deck of cards (Blackjack) into an array or list. 我试图将一副纸牌(二十一点)放入阵列或列表中。 I have seen this done before but can't remember the syntax and can't find it anywhere. 我以前看过这件事,但是记不清语法,在任何地方都找不到。

I need something like this let list = [ 1 .. 10; 10; 10; 10 ] * 4 我需要这样的let list = [ 1 .. 10; 10; 10; 10 ] * 4 let list = [ 1 .. 10; 10; 10; 10 ] * 4 let list = [ 1 .. 10; 10; 10; 10 ] * 4 , but of course this doesn't work. let list = [ 1 .. 10; 10; 10; 10 ] * 4 ,但这当然不起作用。 If anybody could help I'd greatly appreciate it. 如果有人可以帮忙,我将不胜感激。

I'm not sure of the exact syntax you're after... 我不确定您要使用的确切语法...

Ideally you want a list for suits and a list for cards and then you create a Cartesian Product , and it is probably easiest to store each card in the deck in a tuple of number, suit 理想情况下,您需要一个西服列表和一张卡列表,然后创建一个笛卡尔乘积 ,这可能是最简单的将每张卡存储在牌组中的number, suit元组中

let cards = [1..10]
let suits = [1..4]

let deck = seq { 
    for card in cards do //cards could just be [1..10]
        for suit in suits do //suits could be too
            yield card, suit }

//Show the answer
printfn "%A" (deck |> Seq.toList)

Usually a suit would be a Discriminated Union and you could map the numbers: 通常,西装是歧视联盟,您可以绘制数字:

type Suit = 
| Hearts
| Spades
| Diamonds
| Clubs

let toSuit suit =
    match suit with
    | 1 -> Hearts
    | 2 -> Spades
    | 3 -> Diamonds
    | 4 -> Clubs

and change yield card, suit } to yield card, toSuit suit } 并将yield card, suit }更改为yield card, toSuit suit }

You could do the same with the cards to by having a DU from Ace -> King. 您可以通过从Ace-> King获得DU来处理卡片。 I'll leave that as an exercise for you ;) 我将其留给您练习;)

Finally, if you are after a generic function to this, you could write something like this - I don't recall this been in FSharp.Core, but I may be wrong. 最后,如果您追求的是通用函数,则可以编写如下内容-我不记得这是在FSharp.Core中使用的,但是我可能是错的。

let multiplyList xs by = seq {
    for x in xs do
        for y in [1..by] do 
            yield x, y }

let deck = multiplyList cards 4 |> Seq.toList

It would be nice if you stated what language you are using, because the answer may be different. 如果您说出使用的是哪种语言,那会很好,因为答案可能有所不同。 I think you are looking for an array of array. 我认为您正在寻找数组数组。 You'll like need a bunch of arrays to keep track of the card rank, suit, rank value, and whether it has been played, not played, or whatever you'd like. 您将需要一堆数组来跟踪纸牌等级,西服,等级值,以及是否已玩过,未玩过或想要的内容。 So you need something like: 因此,您需要类似:

ranks = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']
rankValues = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
suits = [ 'spades', 'hearts', 'clubs', 'diamonds' ]

deck = [[ 0,0,0,0,0,0,0,0,0,0,0,0,0],
        [ 0,0,0,0,0,0,0,0,0,0,0,0,0],
        [ 0,0,0,0,0,0,0,0,0,0,0,0,0],
        [ 0,0,0,0,0,0,0,0,0,0,0,0,0]]
card = [suit, rank]

deck [suit][rank] = true
deck [suit][rank] = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM