有沒有通過它的某個屬性查找給定「對象」的方法? 我試着模式匹配一樣,我會在邏輯編程都做,但我無法弄清楚:Haskell:按屬性查找列表中的對象
data Object = Object {
_prop1 :: type,
_prop2 :: color,
_prop3 :: pos
} deriving Eq
type Square = Maybe Object
type Board = [[Square]]
objectlist::Board
objectlist = [[ Just (Object type color pos), Just (Object type color pos)]
...
[ Just (Object type color pos), Just (Object type color pos)]
index_of :: (Int, Int)->Int
index_of (x,y) = fromJust $ elemIndex piece objectlist
where
piece = Piece _ _ (x,y)
另外,我覺得我的方法找到該指數是不好的。我用了一個簡單的列表,但無法找到如何使用2個暗淡列表來完成它。
正是我在找的東西!搭乘5年的編程習慣需要很長時間!謝謝! –