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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Array.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 
" 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 
" 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 
"
        complete <- false
    |true ->
        if input = 1 && spaceleft>= a.iSpace then
            inventory.Add(Weapon a)
            printf "
 %s added to the inventory 
" a.name
            complete <- true
        else
            printf "
 The weapon was left here 
"
            complete <- false
complete
答案

你有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 
" 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 
" 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
另一答案

只需添加到已接受的答案:只要您的内部类型是记录,您也可以匹配记录标签。结合外部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))

以上是关于使用Array.Count并匹配案例F#的主要内容,如果未能解决你的问题,请参考以下文章

在Python中,array.count()的数量级是否比list.count()慢几个?

重新创建应用时,片段与底部导航视图图标不匹配

缺少 SQL SERVER 2014 代码片段

firebase 的短信代码不匹配

如何通过字符串c#中的调用变量名设置值

VSCode自定义代码片段——声明函数