sun
sun

Reputation: 315

Data type errors on haskell regarding arguments

So I'm new to haskell and I'm not quite sure how to do certain things Not many online tutorials even address the problem I'm having, so I figured I'd ask here finally

This is my code: (It's in an lhs file)

> data PointVal = PointVal Float Float Float Float
>                 deriving (Show)


> parabolaCordDistance :: Float -> Float -> Float -> Float -> Float
> parabolaCordDistance x1 x2 y1 y2
>       abc = PointVal x1 x2 y1 y2     
>
>

So now the error I'm getting regarding this is :

Extension.lhs:17:3:
    Couldn't match expected type `t0 -> PointVal'
                with actual type `Float'
    The equation(s) for `parabolaCordDistance' have five arguments,
    but its type `Float -> Float -> Float -> Float -> Float'
    has only four
Failed, modules loaded: none.

I'm basically trying to create a data type called PointVal where the 4 floats act as x and y co-ordinates. Then I'm trying to input these using ParabolaCordDistance And using abc as a variable in the data type "PointVal"

If anyone could help remove this error; or point out where I've gone wrong and how to fix it, that'd be great!

Upvotes: 0

Views: 62

Answers (4)

Bogdan Ruzhitskiy
Bogdan Ruzhitskiy

Reputation: 1237

You should better use more explicit way to create 2d point

data Point = Point (Float, Float) (Float, Float)
             deriving (Show)

result in ghci

*Main> Point (1,1) (2,2)
Point (1.0,1.0) (2.0,2.0)

Upvotes: 0

Erik Kaplun
Erik Kaplun

Reputation: 38257

EDIT: I think I've misinterpreted your specific issue; Daniel has nailed it though! I'm keeping my answer though as it might still be useful and is definitely moderately relevant.


A type signature of the form a -> b -> c -> d -> e indicates a function with FOUR arguments (a, b, c, d) not five — the last item is the type of the return value (e).

So when you are defining the body of such function, you should only define it with the right number of arguments, and the return value is not an argument.

In your case, the signature is Float -> Float -> Float -> Float -> Float which "translates" to "a function that takes 4 floats and returns a float".


So first remove the 5th abc argument from the implementation definition

parabolaCordDistance x1 x2 y1 y2 = PointVal x1 x2 y1 y2 

and secondly I think PointVal is what you are returning not a Float so you need to adapt your type signature to

parabolaCordDistance :: Float -> Float -> Float -> Float -> PointVal

you can also just remove the type annotation and ask on a GHCi prompt what Haskell thinks the type of the function is (after removing the extraneous/bogus abc arg, which you didn't really mean):

*> :t parabolaCordDistance
parabolaCordDistance :: Float -> Float -> Float -> Float -> PointVal

But note that the definition of parabolaCordDistance, considering its current implementation, can be simplified to

parabolaCordDistance = PointVal

but I'm assuming you possibly wanted to add more processing to the function at a later phase.

Upvotes: 2

Daniel Wagner
Daniel Wagner

Reputation: 153287

Perhaps you are looking for this syntax:

> parabolaCordDistance x1 x2 y1 y2 = 3 where
>       abc = PointVal x1 x2 y1 y2

This defines a variable abc, local to parabolaCordDistance, with value PointVal x1 x2 y1 y2. You will of course want to change the return value 3 to some more meaningful computation; that's just for the sake of making a complete example.

Upvotes: 3

Sam van Herwaarden
Sam van Herwaarden

Reputation: 2361

I'm not sure exactly what you're trying to do, especially with abc. Your code is interpreted as

parabolaCordDistance x1 x2 y1 y2 abc = PointVal x1 x2 y1 y2

so the function parabolaCordDistance is getting five arguments. This is not in agreement with the type, which says it takes four Float arguments. That's the error you're getting.

Upvotes: 0

Related Questions