user3461226
user3461226

Reputation:

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

Upvotes: 0

Views: 284

Answers (2)

Cirdec
Cirdec

Reputation: 24156

In Haskell, both functions and constructors are applied by juxtaposition. For example f x is the function f applied to the argument x. f x y is the function f applied to x, with the result applied to y. You can think of f x y as f applied to two arguments, x and y. You don't need parenthesis for function or constructor application, for example f (x y) means something different - in this case x is being applied to y, and f (x, y) means the function f applied to the tuple (x, y).

For your code, you need to use

  • R 2 4 instead of R(2,4)
  • R (n x) (d x) instead of R(d n)x

When we make these syntax changes, equivalentes would be written as

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

And your example would be written as

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

Upvotes: 3

bheklilr
bheklilr

Reputation: 54058

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.

Upvotes: 5

Related Questions