无法将预期类型 (Int -> Int -> Int) 与实际类型“(t0, t1, t2)”匹配
我是一名初学者,在进入计算机科学大学之前,我正在尝试做一些 Haskell 教程。
我被这个程序困住了。它需要三个数字并将它们按升序排列。谁能帮助我并告诉我出了什么问题,因为这让我发疯了?感谢您抽出时间。
import Prelude hiding (min,max)
orderTriple :: (Int -> Int -> Int) -> (Int -> Int -> Int)
max :: Int -> Int -> Int -> Int
min :: Int -> Int -> Int -> Int
middle :: Int -> Int -> Int -> Int
max x y z
|(x>=y) && (x>=z) = x
|(y>=x) && (y>=z) = y
|otherwise = z
min d e f
|(d<=e) && (d<=f) = d
|(e<=d) && (e<=f) = e
|otherwise = f
middle g h i
| (g <= (max g h i)) && (g >= (min g h i)) = g
| (h <= (max g h i)) && (h >= (min g h i)) = h
| otherwise = i
orderTriple (a,b,c) = ((min a b c),(middle a b c),(max a b c))
错误是:
orderList.hs:23:13:
Couldn't match expected type `[Int -> Int -> Int]'
with actual type `(t0, t1, t2)'
In the pattern: (a, b, c)
In an equation for `orderTriple':
orderTriple (a, b, c) = [(min a b c), (middle a b c), (max a b c)]
I'm a beginner and I'm trying to do some tutorials on Haskell before entering uni for computer science.
I got stuck in this program. It takes three numbers and puts them in ascending order. Can anyone help me and tell me whats wrong because it's driving me crazy? Thanks for your time.
import Prelude hiding (min,max)
orderTriple :: (Int -> Int -> Int) -> (Int -> Int -> Int)
max :: Int -> Int -> Int -> Int
min :: Int -> Int -> Int -> Int
middle :: Int -> Int -> Int -> Int
max x y z
|(x>=y) && (x>=z) = x
|(y>=x) && (y>=z) = y
|otherwise = z
min d e f
|(d<=e) && (d<=f) = d
|(e<=d) && (e<=f) = e
|otherwise = f
middle g h i
| (g <= (max g h i)) && (g >= (min g h i)) = g
| (h <= (max g h i)) && (h >= (min g h i)) = h
| otherwise = i
orderTriple (a,b,c) = ((min a b c),(middle a b c),(max a b c))
The error is:
orderList.hs:23:13:
Couldn't match expected type `[Int -> Int -> Int]'
with actual type `(t0, t1, t2)'
In the pattern: (a, b, c)
In an equation for `orderTriple':
orderTriple (a, b, c) = [(min a b c), (middle a b c), (max a b c)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您向编译器提供了错误的类型信息:
应该是
第一个键入声明
orderTriple
将函数(从两个 Int 转换为一个)转换为另一个此类函数,这根本不是您的代码所做的。(另外:+1 学习 FP 为 CS 课程做准备)。
You give the compiler wrong type information:
should be
The first typing claims that
orderTriple
converts a function (from two Ints to one) into another such function, which is not at all what your code does.(Also: +1 for studying FP in preparation for a CS program).
箭头
->
分隔函数的参数。 (实际上它有点复杂)但是要分隔元组的参数,请使用逗号:对于其他类型,空格就足够了:
An arrow
->
separates a function's arguments. (Actually it is a little bit more sophisticated) But to separate the arguments of a tuple, use a comma:In case of other types, a space is sufficient: