在haskell中的奇数/偶数函数中的相互递归 [英] Mutual recursion in odd/even functions in haskell
问题描述
even :: Int - > Bool
even 0 = True
even(n + 1)= odd n
odd :: Int - > Bool
odd 0 = False
odd(n + 1)= even n
我想尝试一下。我把代码放在Hof.hs文件中,运行 ghci (版本7.8.3),输入
:l Hof.hs
并获得以下错误消息:
Hof.hs:3:7:模式中的分析错误:n + 1
失败,模块加载:无。
为什么我会收到此消息?是代码在语法上是过时的还是什么?
请注意,这个函数在负输入中失败了,所以你可能想用 In chapter 6 of "Programming in Haskell" by Graham Hutton there is a section called "6.5 Mutual recursion", that contains the following example: I wanted to try it out. I put the code in Hof.hs file, ran ghci(version 7.8.3), typed and got the following error message Why am I getting this message? Is the code syntactically out of date or something? n + k patterns have been removed from Haskell and are no longer available. Write it like this instead: Note that this function fails horribly for negative inputs, so you probably want to augment it with 这篇关于在haskell中的奇数/偶数函数中的相互递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
even :: Int - > Bool
even 0 = True
even n = odd(n-1)
odd :: Int - > Bool
odd 0 = False
odd n = even(n - 1)
abs
来扩充它。even :: Int -> Bool
even 0 = True
even (n + 1) = odd n
odd :: Int -> Bool
odd 0 = False
odd (n + 1) = even n
:l Hof.hs
Hof.hs:3:7: Parse error in pattern: n + 1
Failed, modules loaded: none.
even :: Int -> Bool
even 0 = True
even n = odd (n - 1)
odd :: Int -> Bool
odd 0 = False
odd n = even (n - 1)
abs
.