user3080627
user3080627

Reputation: 5

haskell - check two points in R^2

i have given two points. Now of i need to check if those points are identical, so i do:

type datatypePoint = (Float,Float)

anyLine :: datatypePoint -> datatypePoint -> datatypeLine
anyLine a b = [[fst a, fst b] , [snd a, snd b]]
    | (fst a == fst b) && (snd a == snd b) = error "Identical"
    | otherwise = error "Not identical"

But i get error:

unexpected | 

anybody could tell me why? What am i doing wrong?

Upvotes: 0

Views: 140

Answers (3)

jamshidh
jamshidh

Reputation: 12060

Other folks have already answered the question, but I wanted to point out that this would be even simpler if you used "newtype" and "deriving"

newtype Point a = Point (a, a) deriving (Eq)

anyLine a b 
    | a == b = ....
    | otherwise = ....

It also doesn't hurt to keep the type a generic, so now this will work for "Point"s of Floats, Ints, etc.

Upvotes: 0

daniel gratzer
daniel gratzer

Reputation: 53871

You have a few errors here, first off, all types start with upper case letters in Haskell

type Point = (Float,Float)

anyLine :: Point -> Point -> Point

Next, pattern matching happens before the = sign.

anyLine (a1, a2) (b1, b2)
  | a1 == b1 && a2 == b2 = error "Identical"
  | otherwise            = error "Not identical"

And with guards we omit the equality sign.

This could also just be

anyLine a b
  | a == b = ...
  | otherwise = ...

I think it's well worth the time to read a good Haskell tutorial to learn some of the basic concepts you're missing, I personally favor Learn You A Haskell.

Upvotes: 3

Simeon Visser
Simeon Visser

Reputation: 122336

You can specify a result or define cases; you can't do both at the same time.

anyLine :: datatypePoint -> datatypePoint -> datatypeLine
anyLine a b
    | (fst a == fst b) && (snd a == snd b) = error "Identical"
    | otherwise = error "Not identical"

Upvotes: 0

Related Questions