无法将预期类型 (Int -> Int -> Int) 与实际类型“(t0, t1, t2)”匹配

发布于 2024-12-01 20:12:37 字数 1130 浏览 1 评论 0原文

我是一名初学者,在进入计算机科学大学之前,我正在尝试做一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花间憩 2024-12-08 20:12:37

您向编译器提供了错误的类型信息:

orderTriple :: (Int -> Int -> Int) -> (Int -> Int -> Int)

应该是

orderTriple :: (Int, Int, Int) -> (Int, Int, Int)

第一个键入声明 orderTriple 将函数(从两个 Int 转换为一个)转换为另一个此类函数,这根本不是您的代码所做的。

(另外:+1 学习 FP 为 CS 课程做准备)。

You give the compiler wrong type information:

orderTriple :: (Int -> Int -> Int) -> (Int -> Int -> Int)

should be

orderTriple :: (Int, Int, Int) -> (Int, Int, Int)

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).

当爱已成负担 2024-12-08 20:12:37

箭头 -> 分隔函数的参数。 (实际上它有点复杂)但是要分隔元组的参数,请使用逗号:

orderTriple :: (Int,Int,Int) -> (Int,Int,Int)

对于其他类型,空格就足够了:

Either String Int

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:

orderTriple :: (Int,Int,Int) -> (Int,Int,Int)

In case of other types, a space is sufficient:

Either String Int
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文