为什么 ADT "data MyC c = EmptyC | ConsC c MyC" 类型不安全?
Why ADT "data MyC c = EmptyC | ConsC c MyC" not type safe?
--This is type safe
data IntList = Empty | Cons Int IntList
deriving Show
--This is type safe
data MyB b = EmptyB | ConsB b b
deriving Show
data MyC c = EmptyC | ConsC c MyC
deriving Show
为什么抽象数据类型 MyC 类型不安全?
gaurd 子句 ConsC c MyC
应该构建
缺点列表,因为它以与 IntList 相同的方式调用 MyC
从 Cons Int IntList
调用 IntList
ADT ?
这不是有效,它与安全无关,根本不正确。
data MyC c = EmptyC | ConsC c MyC
这里的问题是MyC
带了一个参数,但是在使用的时候却没有给它一个参数。应该是这样的:
data MyC c = EmptyC | ConsC c (MyC c)
-- ^^^^^^^ The parameter is necessary
注意MyC Int
与IntList
基本相同。
--This is type safe
data IntList = Empty | Cons Int IntList
deriving Show
--This is type safe
data MyB b = EmptyB | ConsB b b
deriving Show
data MyC c = EmptyC | ConsC c MyC
deriving Show
为什么抽象数据类型 MyC 类型不安全?
gaurd 子句 ConsC c MyC
应该构建
缺点列表,因为它以与 IntList 相同的方式调用 MyC
从 Cons Int IntList
调用 IntList
ADT ?
这不是有效,它与安全无关,根本不正确。
data MyC c = EmptyC | ConsC c MyC
这里的问题是MyC
带了一个参数,但是在使用的时候却没有给它一个参数。应该是这样的:
data MyC c = EmptyC | ConsC c (MyC c)
-- ^^^^^^^ The parameter is necessary
注意MyC Int
与IntList
基本相同。