Maybe a -> Maybe a 有多少个函数?

问题描述 投票:0回答:1

Thinking Functionly with Haskell(第 45 页)中,Richard Bird 说有 6 个类型为

Maybe a -> Maybe a
的函数,尽管他只提到了 5 个。

应用于

Nothing
的函数只能返回
Nothing
undefined
。 应用于
Just x
的函数只能返回
Nothing
Just x
undefined
。关键是我们对此一无所知 基础类型,因此无法发明新值。这使得六个 所有可能的功能。

我数到七:

first, second, third, fourth, fifth, sixth, seventh :: Maybe a -> Maybe a

first (Just x) = Just x
second (Just x) = Nothing
third (Just x) = undefined
fourth Nothing = Nothing
fifth Nothing = undefined
sixth undefined = Nothing
seventh undefined = undefined

一个相关的难题是,当我尝试将其中三个定义放在一个函数下时,我收到编译错误“模式匹配是多余的”。删除三行中的任何一行都会使其消失

foo :: Maybe a -> Maybe a
foo (Just x) = Nothing
foo Nothing = undefined
foo undefined = Nothing
haskell
1个回答
0
投票

你的函数不是total函数,每次都取一个case。这意味着未涵盖的情况会出现错误。

所以本质上如果你写:

first (Just x) = Just x

是:

first (Just x) = Just x
first _ = error "case not covered"

因此,您需要涵盖两种情况的组合:

Just x
Nothing
,从而乘以每种情况的可能性。

另一个问题是

undefined
中的
sixth undefined = Nothing
检查该项是否未定义:它只是一个名为
undefined
的变量。所以相当于
sixth x = Nothing

因此,这基本上为

Just x
留下了三个选项:
Just x
Nothing
undefined
/
error
,以及
Nothing
的两个选项:
Nothing
和未定义,所以:

first (Just x) = Just x
first Nothing = Nothing

second (Just x) = Just x
second Nothing = undefined

third (Just x) = Nothing
third Nothing = Nothing

fourth (Just x) = Nothing
fourth Nothing = undefined

fifth (Just x) = undefined
fifth Nothing = Nothing

sixth (Just x) = undefined
sixth Nothing = undefined
© www.soinside.com 2019 - 2024. All rights reserved.