我在 Haskell 中有一個串列,其中包含一些點的坐標,例如:
[[5.8,2.7,4.1,1.0],[6.2,2.2,4.5,1.5],[5.6,2.5,3.9,1.1]]
已經創建了一個“Point”資料型別
data Point = Point {
pointSelf :: Int,
coordinates:: [Float]
} deriving (Show)
我想讓那個串列[[Float]]變成[Point]的串列,屬性“pointSelf”是他的索引,我應該怎么做?
我在 Haskell 和函式式編程方面有點新……
uj5u.com熱心網友回復:
您可以使用zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]同時列舉兩個串列,并對串列的兩項應用一個函式。因此,您可以Point使用以下方法構造 s:
zipWith Point [0 ..] myCoords
使用myCoords坐標串列,因此對于樣本資料,myCoords是[[5.8,2.7,4.1,1.0],[6.2,2.2,4.5,1.5],[5.6,2.5,3.9,1.1]]。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/418702.html
標籤:
上一篇:Haskell:準引號轉義
下一篇:在swift中構造JSON
