noo*_*bie 9 haskell types tuples
我是初学者,在尝试进入计算机科学大学之前,我正在尝试在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))
Run Code Online (Sandbox Code Playgroud)
错误是:
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)]
Run Code Online (Sandbox Code Playgroud)
你给编译器错误的类型信息:
orderTriple :: (Int -> Int -> Int) -> (Int -> Int -> Int)
Run Code Online (Sandbox Code Playgroud)
应该
orderTriple :: (Int, Int, Int) -> (Int, Int, Int)
Run Code Online (Sandbox Code Playgroud)
第一个打字声明orderTriple将一个函数(从两个Ints转换为一个)转换为另一个这样的函数,这完全不是你的代码所做的.
(另外:+1用于研究FP 以准备 CS程序).
| 归档时间: |
|
| 查看次数: |
3060 次 |
| 最近记录: |