0

I tried to write a function where given two points (in degree), the Great-circle distance will be calculated. For this, degree has to be changed in radian.

beside the code is looking real ugly, ghci is giving me a parse error on input 'rad2grad' what i am doing wrong?

gke  (x1, y1)(x2, y2) = c*rad2grad
                                  where 
                                              c = 111.2225685
                                       rad2grad = (360*arcos ((sin(grad2rad x1)*sin(grad2rad x2) + cos(grad2rad x1)*cos(grad2rad x2)*cos(grad2rad y1 - grad2rad y2)))/(2*pi)
                                    grad2rad x1 = (2*pi/360)*x1
                                    grad2rad x2 = (2*pi/360)*x2
                                    grad2rad y1 = (2*pi/360)*y1
                                    grad2rad y2 = (2*pi/360)*y2

3 Answers 3

2

Haskell is whitespace sensitive, so alignment matters. Once aligned, GHCi said,

parse error (possibly incorrect indentation or mismatched brackets)

Which should indicate to you that you might have mismatched brackets (assuming your indentation is correct). You are indeed missing a final closing paren before dividing by (2*pi).

The following compiles:

gke  (x1, y1)(x2, y2) = c*rad2grad
  where 
    c = 111.2225685
    rad2grad = (360*arcos ((sin(grad2rad x1)*sin(grad2rad x2) + cos(grad2rad x1)*cos(grad2rad x2)*cos(grad2rad y1 - grad2rad y2))))/(2*pi)
    grad2rad x1 = (2*pi/360)*x1
    grad2rad x2 = (2*pi/360)*x2
    grad2rad y1 = (2*pi/360)*y1
    grad2rad y2 = (2*pi/360)*y2
Sign up to request clarification or add additional context in comments.

2 Comments

i am open for suggestions how to make it less ugly . But please, nothing to fancy for i am just a beginner and wouldn't understand much.
I think your problem (the missing paren) started because of that huge definition for rad2grad. Besides getting an editor that matches parentheses for you, you can break it up into smaller chunks to make it more manageable.
1

The left-hand sides of equations in a where block must align with each other. So a minimal fix for your indentation problem looks like this:

gke  (x1, y1)(x2, y2) = c*rad2grad
                              where 
                                c           = 111.2225685
                                rad2grad    = (360*arcos ((sin(grad2rad x1)*sin(grad2rad x2) + cos(grad2rad x1)*cos(grad2rad x2)*cos(grad2rad y1 - grad2rad y2)))/(2*pi)
                                grad2rad x1 = (2*pi/360)*x1
                                grad2rad x2 = (2*pi/360)*x2
                                grad2rad y1 = (2*pi/360)*y1
                                grad2rad y2 = (2*pi/360)*y2

Also, the parentheses on your rad2grad line aren't all matched up. Perhaps the very first open parenthesis can be deleted.

This will take care of the parse errors; there are other problems, too, but I think you will be able to find and fix those on your own.

Comments

1

You are giving redundant definitions for grad2rad - they will just be ignored. You can write the function like this:

gke  (x1, y1)(x2, y2) = c*rad2grad
                          where 
                            c           = 111.2225685
                            rad2grad    = (360*acos ((sin(grad2rad x1)*sin(grad2rad x2) + cos(grad2rad x1)*cos(grad2rad x2)*cos(grad2rad y1 - grad2rad y2)))/(2*pi))
                            grad2rad a = (2*pi/360)*a

3 Comments

How does the function "know" that it can put x1...y2 for a? sorry for the dumb question :). now, after typing in 4 numbers, i get the mesage "non type-variable argument in the constraint. What does that mean?
Please, ignor the last part of my question. found my fault.
@letter You can pass any expression of the appropriate type to grad2rad, including x1, y1, and similar, but also including more complicated stuff like x1+y1/2 and verletIntegrate f x1 x2 or whatever.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.