我是Haskell的新手,我無法弄清楚如何聲明「數據」類型以及如何使用該類型初始化變量。我也想知道如何更改該變量的某些成員的值。對於的exaple:Haskell中的數據初始化和數據成員更改
data Memory a = A
{ cameFrom :: Maybe Direction
, lastVal :: val
, visited :: [Direction]
}
方向是包含N,S,E的數據類型,W val爲一個int類型
init :: Int -> a
init n = ((Nothing) n []) gives me the following error:
The function `Nothing' is applied to two arguments,
but its type `Maybe a0' has none
In the expression: ((Nothing) n [])
In an equation for `init': init n = ((Nothing) n [])
我怎樣才能解決這個問題?
UPDATE:那做到了,非常感謝你,但現在我還有一個問題
move :: val -> [Direction] -> Memory -> Direction
move s cs m | s < m.lastVal = m.cameFrom
| ...
這給了我以下錯誤:
Couldn't match expected type `Int' with actual type `a0 -> c0'
Expected type: val
Actual type: a0 -> c0
In the second argument of `(<)', namely `m . lastVal'
In the expression: s < m . lastVal
更新2:同樣,這幫助我很多,非常感謝你
另外,我還有一個問題(對不起,是這樣一個麻煩)
我如何ADRESS一種 例如只有一個元素,如果我有
Type Cell = (Int, Int)
Type Direction = (Cell, Int)
我該如何着手,如果我想要一個細胞變量比較方向可變的電池元件?
你的意思'lastVal ::了',與'數據存儲了'匹配? – dave4420 2012-04-10 16:40:32
這些都是很基本的問題;你有沒有閱讀過關於記錄和其他數據類型的部分? – 2012-04-10 18:24:01