以下是结构归纳的定义吗?
foldr f a (xs::ys) = foldr f (foldr f a ys) xs
有人能给我一个Haskell结构归纳的例子吗?
答案 0 :(得分:24)
您没有指定它,但我认为::
表示列表连接和
使用++
,因为那是Haskell中使用的运算符。
为了证明这一点,我们将在xs
上进行归纳。首先,我们表明了
声明适用于基本情况(即xs = []
)
foldr f a (xs ++ ys)
{- By definition of xs -}
= foldr f a ([] ++ ys)
{- By definition of ++ -}
= foldr f a ys
和
foldr f (foldr f a ys) xs
{- By definition of xs -}
= foldr f (foldr f a ys) []
{- By definition of foldr -}
= foldr f a ys
现在,我们假设归纳假设foldr f a (xs ++ ys) = foldr
f (foldr f a ys) xs
适用于xs
,并表明它将保留列表
x:xs
也是如此。
foldr f a (x:xs ++ ys)
{- By definition of ++ -}
= foldr f a (x:(xs ++ ys))
{- By definition of foldr -}
= x `f` foldr f a (xs ++ ys)
^------------------ call this k1
= x `f` k1
和
foldr f (foldr f a ys) (x:xs)
{- By definition of foldr -}
= x `f` foldr f (foldr f a ys) xs
^----------------------- call this k2
= x `f` k2
现在,根据我们的归纳假设,我们知道k1
和k2
相等,
因此
x `f` k1 = x `f` k2
从而证明了我们的假设。