I have written some code for several shapes, and i get a compiling error:
parse error on input `:'
though I can't find where the error is...
data Shape = Ellipse Radius Radius
| Polygon [Vertex]
type Radius = Float
type Vertex = (Float, Float)
area :: Shape -> Float
area (Rectangle s1 s2) = s1 * s2
area (RtTriangle s1 s1) = s1 *s2 /2
area (Ellipse r1 r2) = pi * r1 * r2
= triArea v1 v2 v3 + area(Polygon(v1:v3:vs))
area(Polygon _ )
area (Polygon(v1:vs')) = polyArea vs
where polyArea :: [Vertex] -> Float
polyArea(v2:v3:vs') = triArea v1 v2 v3
polyArea _ = 0
triArea :: Vertex -> Vertex -> Vertex -> Float
triArea v1 v2 v3 = let a = distBeetween v1 v2
b = distBeetween v2 v3
c = distBeetween v3 v1
s = 0.5 * (a + b + c)
in sqrt (s * (s - a) * (s - b) * (s - c))
Am Sonntag, 19. Februar 2006 22:57 schrieb Chatzianastassiou Achilleas:
> Hi everybody,
> I have written some code for several shapes, and i get a compiling error:
> parse error on input `:'
> though I can't find where the error is...
ever heard of the layout-rule?
You must indent the cases for polyArea to exactly the same level as the
type-declaration, and all declarations in the let-binding to the same level.
Once the indentation is corrected, you get the real error messages -- those
you should understand easily.
"In My Egotistical Opinion, most people's C programs should be
indented six feet downward and covered with dirt."
-- Blair P. Houghton
In reply to this post by Chatzianastassiou Achilleas
Watch how deeply things are indented. The definitions for polyArea
should be indented to the same level as its type signature. Currently,
they're indented a little more, so the first line of polyArea's
definition is being treated as part of the type signature, which is
why you're getting the error. GHC/GHCi/hugs should be telling you also
what line the error happens on, which should be a good clue here :)