简体   繁体   English

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

[英]couldn't match expected type (Int -> Int -> Int) with actual type `(t0, t1, t2)'

I'm a beginner and I'm trying to do some tutorials on Haskell before entering uni for computer science. 我是初学者,在尝试进入计算机科学大学之前,我正在尝试在Haskell上做一些教程。

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

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. 第一个打字声称orderTriple将一个函数(从两个Ints转换为一个)转换为另一个这样的函数,这完全不是你的代码所做的。

(Also: +1 for studying FP in preparation for a CS program). (另外:+1用于研究FP 以准备 CS程序)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将预期类型“(t1,t2,t0)”与实际类型“ [a0]”匹配 - Couldn't match expected type `(t1, t2, t0)' with actual type `[a0]` 无法将预期类型&#39;(Int,Int)&#39;与实际类型&#39;[t0]&#39;相匹配 - Couldn't match expected type ‘(Int, Int)’ with actual type ‘[t0]’ 无法将预期类型 `(a1 -&gt; a1 -&gt; a1) -&gt; (t0 a0 -&gt; Int) -&gt; t Int -&gt; Int&#39; 与实际类型 `Int&#39; 匹配 - Couldn't match expected type `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int -> Int' with actual type `Int' 无法将预期类型`Maybe(String,Int,String)&#39;与实际类型`([Char],t0,[Char])&#39;匹配 - Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])' 无法将预期类型“[Char]”与实际类型“Int -&gt; [t -&gt; t1]”匹配 - Couldn't match expected type ‘[Char]’ with actual type ‘Int -> [t -> t1]’ 无法将期望类型“Int”与实际类型“IO [Int]”匹配 - Couldn't match expected type `Int' with actual type `IO [Int]' 无法将预期类型“Int”与实际类型[Int]匹配 - Couldn't match expected type `Int' with actual type [Int] 无法将预期类型“[Int]”与实际类型“Int”相匹配 haskell - Couldn't match expected type ‘[Int]’ with actual type ‘Int’ haskell 无法将预期类型“Int”与实际类型(Int、Int、Int)匹配 - Couldn't match expected type ‘Int’ with actual type (Int, Int, Int) Haskell:无法将期望的类型[(Int,Int)]与实际的类型IO [[Int,Int)]相匹配 - Haskell: Couldn't match expected type [(Int, Int)] with actual type IO [(Int,Int)]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM