gpt4 book ai didi

haskell - 创建记录表单列表的最短方法是什么?

转载 作者:行者123 更新时间:2023-12-04 22:56:15 24 4
gpt4 key购买 nike

假设我有记录定义

data Zone = Zone
{ zId :: Int -- this zone's ID
, zOwnerId :: Int -- the player who owns this zone (-1 otherwise)
, zPodsP0 :: Int -- player 0's PODs on this zone
, zPodsP1 :: Int -- player 1's PODs on this zone
, zPodsP2 :: Int -- player 2's PODs on this zone (always 0 for a two player game)
, zPodsP3 :: Int -- player 3's PODs on this zone (always 0 for a two or three player game)
} deriving Show

[String] 创建记录的捷径是什么?读自 getLine
zones <- replicateM zoneCount $ fmap (mkZone . words) getLine

这是迄今为止我能做的最好的。
{-# LANGUAGE NamedFieldPuns #-}

mkZone :: [String] -> Zone
mkZone xs = Zone {zId, zOwnerId, zPodsP0, zPodsP1, zPodsP2, zPodsP3}
where [zId, zOwnerId, zPodsP0, zPodsP1, zPodsP2, zPodsP3] = map read xs

我在玩编码游戏时经常使用这种模式 bot programmings , 如果有更好的方法来做到这一点,那就太好了。

最佳答案

RecordWildCards 删除一半的样板。

{-# LANGUAGE RecordWildCards #-}

mkZone :: [String] -> Zone
mkZone xs = Zone {..}
where [zId, zOwnerId, zPodsP0, zPodsP1, zPodsP2, zPodsP3] = map read xs

关于haskell - 创建记录表单列表的最短方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com