嗨,我有一个数据类型是
data MyIntStr = MyIntStr { intList :: IntList, strList :: StrInt}
type IntList = [[Int]]
type StrList = [[String]]
我想在intList
和strList
中添加内容MyData
。所以我要传递一个默认数据,它是空的,然后尝试将Ints添加到Intlist
:
putInts :: [Int] -> MyIntStr -> MyIntStr
putInts (h:t) d
|length t /= 0 = putInts t (intList:h)
|otherwise intList:h
这给错误的任何想法如何做到这一点?
仅仅为了以一些建设性的帮助(或者我希望如此)结束,这里是一个应该执行您期望的版本:
type IntList = [Int]
type StrList = [String]
data MyIntStr = MyIntStr { intList :: IntList, strList :: StrList }
deriving Show
empty :: MyIntStr
empty = MyIntStr [] []
putInts :: [Int] -> MyIntStr -> MyIntStr
putInts is (MyIntStr is' ss) = MyIntStr (is'++is) ss
这是一个例子:
λ> putInts [1,2,3] empty
MyIntStr {intList = [1,2,3], strList = []}
我做了什么:
deriving Show
以便我可以看到我的示例;)StrInt
成StrList
因为你明明是认真的empty
所以我可以测试putInts
使用模式匹配重写并++
连接您的[Int]
列表本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句