[Haskell-beginners] Type declarations

Patrik Iselind patrik.mrx at gmail.com
Sun Nov 26 13:50:30 UTC 2017


Hi,

What's the difference between `delta :: (Point t) => t -> t -> Double` 
and `delta :: Point p -> Point q -> Double`. The later one is accepted 
by GHCI when i do :load.

As i see it the first one would make better sense to me. I want two 
Point as in-parameters and delta will produce a Double, but GHCI refuse 
this saying

```
nine.hs:10:11:
     Expected a constraint, but ‘Point t’ has kind ‘*’
     In the type signature for ‘delta’:
       delta :: (Point t) => t -> t -> Double
Failed, modules loaded: none.
```

Is this saying that Point t match 'everything' (*)?

In the second version, which is accepted by GHCI, i don't see the point 
of p and q. Can i use these somehow?

All of delta using the accepted type declaration looks like this for 
reference:

```
data Direction d = LEFT
                 | RIGHT
                 | STRAIGHT
                   deriving (Show)

data Point a = Coordinate Double Double
              deriving (Show)

-- Calculate the slope between two points (dy/dx)
delta :: Point p -> Point q -> Double
delta (Coordinate a b) (Coordinate c d)
         | (a == c) = 0
         | otherwise = (d-b)/(c-a)

angle (Coordinate g h) (Coordinate i d) (Coordinate e f)
         | (delta a b) > (delta b c) = RIGHT
         | (delta a b) < (delta b c) = LEFT
         | otherwise = STRAIGHT
         where a = Coordinate g h
               b = Coordinate i d
               c = Coordinate e f
```

I'm also wondering if there is a simpler way than recreating the 
Coordinate as a, b, and c in angle. It seems to work ok to me, i just 
feel that a, b, and c in angle should be possible to express in a better 
way.

-- 
Patrik Iselind



More information about the Beginners mailing list