使用Array.Count并匹配案例F#

时间:2017-12-19 10:27:39

标签: arrays count f#

我不确定问题是什么,我正在尝试通过ResizeArray并将项目与数据类型匹配,并根据此情况从空间中删除特定字段(iSpace)中的值(在返回最终值之前,库存有多少空间。

我的代码片段:

let spaceleft =
    let mutable count = 0 //used to store the index to get item from array
    let mutable thespace = 60 //the space left in the inventory
    printf "Count: %i \n" inventory.Count //creates an error
    while count < inventory.Count do 
        let item = inventory.[count]
        match item with
            |Weapon weapon ->

                thespace <- (thespace - weapon.iSpace)

            |Bomb bomb ->

                thespace <-(thespace - bomb.iSpace)

            |Potion pot ->

                thespace <- (thespace - pot.iSpace)

            |Armour arm ->

                thespace <- (thespace - arm.iSpace)


        count <- count+1
    thespace

我收到有关Int32的错误,这与

有关
printf "Count: %i \n" inventory.Count

线

另一个问题是空间似乎没有变化,并且总是返回60,虽然我已经检查过并且库存不是空的,它总是至少有两个物品,1个武器和1个装甲,所以空间应该至少减少但它永远不会。 其他可能有用的片段:

let inventory = ResizeArray[]

let initialise  = 
    let mutable listr = roominit
    let mutable curroom = 3
    let mutable dead = false
    inventory.Add(Weapon weap1)
    inventory.Add(Armour a1)
    let spacetogo = spaceleft //returns 60, although it should not

此外,除了iniitialise功能外,其他功能似乎无法正确添加商品,例如:

let ok, input = Int32.TryParse(Console.ReadLine())

match ok with
    |false ->
        printf "The weapon was left here \n"
        complete <- false
    |true ->
        if input = 1 && spaceleft>= a.iSpace then
            inventory.Add(Weapon a)
            printf "\n %s added to the inventory \n" a.name
            complete <- true
        else
            printf "\n The weapon was left here \n"
            complete <- false
complete

2 个答案:

答案 0 :(得分:3)

您将spaceLeft作为常量值。要使其成为一项功能,您需要添加单位()作为参数。这里的变化包括修改使其变得更简单(我已经包含了我的虚拟类型):

type X = { iSpace : int }
type Item = Weapon of X | Bomb of X | Potion of X | Armour of X
let inventory = ResizeArray [ Weapon {iSpace = 2}; Bomb {iSpace = 3} ]

let spaceleft () =
    let mutable thespace = 60 //the space left in the inventory
    printf "Count: %i \n" inventory.Count
    for item in inventory do
        let itemSpace =
            match item with
            | Weapon w -> w.iSpace
            | Bomb b -> b.iSpace
            | Potion p -> p.iSpace
            | Armour a -> a.iSpace
        thespace <- thespace - itemSpace
    thespace

spaceleft () // 55

以上代码非常必要。如果您想让它更具功能性(并且更简单),您可以使用Seq.sumBy

let spaceleft_functional () =
    printf "Count: %i \n" inventory.Count
    let spaceUsed =
        inventory
        |> Seq.sumBy (function
            | Weapon w -> w.iSpace
            | Bomb b -> b.iSpace
            | Potion p -> p.iSpace
            | Armour a -> a.iSpace)
    60 - spaceUsed

答案 1 :(得分:2)

只需添加到已接受的答案:只要您的内部类型是记录,您也可以匹配记录标签。结合外部DU上的内在类型扩展:

type X = { iSpace : int }
type Y = { iSpace : int }
type Item = Weapon of X | Bomb of Y | Potion of X | Armour of X
let inventory = ResizeArray [ Weapon {iSpace = 2}; Bomb {iSpace = 3} ]

let itemSpace = function
| Weapon { iSpace = s } | Bomb { iSpace = s }
| Potion { iSpace = s } | Armour { iSpace = s } -> s
type Item with static member (+) (a, b) = a + itemSpace b

60 - (Seq.fold (+) 0 inventory)
// val it : int = 55

否则,您可以使用成员约束调用表达式。

let inline space (x : ^t) = (^t : (member iSpace : int) (x))