【发布时间】:2021-02-23 21:23:09
【问题描述】:
我正在尝试解决以下 Haskell 练习:
定义函数exists::(N-> Bool)-> N->Bool,它接收一个谓词p和一个自然n,如果在O和n之间有任何数字p为真,则返回True。
例子:
exists pair three = True
exists isGreaterThanZero O = False
此代码在我的存在函数之前:
{-#LANGUAGE GADTs #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
module Naturales where
import Prelude(Show)
data Bool where { False :: Bool;
True :: Bool
} deriving Show
{- Data Type of Natural Numbers -}
data N where { O :: N ;
S :: N -> N
} deriving Show
zero:: N
zero= O
one:: N
one = S O
two :: N
two = S one
three :: N
three = S two
four :: N
four = S three
...
这就是我对调用的请求函数进行编程的方式 exists 但是当我尝试编译 .hs 代码时 说
exists:: (N->Bool)->N->Bool
exists = \p -> \x -> case x of {
O -> p O;
(S y) -> if p (S y) then True else existe p y; {- Line 288 -}
}
• Couldn't match expected type ‘GHC.Types.Bool’
with actual type ‘Bool’
NB: ‘Bool’ is defined at EstudiandoRecursion.hs:(9,1)-(11,47)
‘GHC.Types.Bool’
is defined in ‘GHC.Types’ in package ‘ghc-prim-0.5.3’
• In the expression: p (S y)
In the expression: if p (S y) then True else existe p y
In a case alternative:
(S y) -> if p (S y) then True else existe p y
|
288 | (S y) -> if p (S y) then True else existe p y; |
我认为我的函数的逻辑存在它是正确的,但也许我在编写代码时出现了语法错误。
【问题讨论】:
-
您似乎在代码中定义了
Bool类型,因此不再引用GHC.Types.Bool的Bool。 -
@WillemVanOnsem,我已经更新了我的问题的描述,添加了我在 exists 函数之前的头代码
-
好吧,
if ...需要一个内置类型的Bool,因此您的类型中的True/False将不足够。
标签: debugging haskell lambda case lambda-calculus