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

wiz*_*zup 4 haskell record

假设我有记录定义

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
Run Code Online (Sandbox Code Playgroud)

[String]阅读中创建记录的短路是什么?getLine

zones <- replicateM zoneCount $ fmap (mkZone . words) getLine
Run Code Online (Sandbox Code Playgroud)

这是迄今为止我能做的最好的事情.

{-# LANGUAGE NamedFieldPuns #-}

mkZone :: [String] -> Zone
mkZone xs = Zone {zId, zOwnerId, zPodsP0, zPodsP1, zPodsP2, zPodsP3}
  where [zId, zOwnerId, zPodsP0, zPodsP1, zPodsP2, zPodsP3] = map read xs
Run Code Online (Sandbox Code Playgroud)

在编写编码器程序时,我会使用这种模式,如果有更好的方法可以做到这一点会很好.

And*_*ács 8

RecordWildCards 删除一半样板.

{-# LANGUAGE RecordWildCards #-}

mkZone :: [String] -> Zone
mkZone xs = Zone {..}
  where [zId, zOwnerId, zPodsP0, zPodsP1, zPodsP2, zPodsP3] = map read xs
Run Code Online (Sandbox Code Playgroud)