Search code examples
haskelltypeshaskell-platform

Haskell user defined data types


I want define kind of R data as rational numbers, where R is (denominator,numerator) and I defined as:

data R = R {n::Int,
            d::Int} deriving Show

Now I tried to do a function that given two arguments(a list of R and a R) and returns a list with the equivalents of R. I try this, but give me a error of types.

equivalentes' :: [R] -> R -> [R]
equivalentes' [] _ = []
equivalentes' (x:xs) r
    | (n x `mod` n r == 0) && (d x `mod` d r == 0) = (R(d n)x): equivalentes' xs r
    | otherwise = equivalentes' xs r

My idea is to return something like this:

> equivalentes'[R(2,4),R(3,5),R(4,8)] (R(1,2))
                   [R (2,4),R (4,8)]

Solution

  • The problem is with the expression

    R (d n) x : equivalentes' xs r
    

    And specifically with

    d n
    

    The n function has type R -> Int, as does the d function, but you've passed n to d as its argument. Maybe you meant something like

    R (d x) x
    

    But since x has type R, this also wouldn't work, so you could have meant

    R (d x) (n x)
    

    or something similar.


    On a different note, you can't do R (1, 2), because (1, 2) is a tuple of two Ints, not just two separate Ints. you could do instead R 1 2, or uncurry R (1, 2) if you really wanted to use tuples.