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)]
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With