Writing a function makePol

Write a function makePol that takes two real numbers r and v and returns a complex number in polar form. The Function is supposed to only handle values between -2pi and 2pi. For other values on v there should be an error. For negative v it should use positive angle, like v+2pi.

English is not my first language so sorry if I translated it a bit weird.

Welcome @minimoll. It is ok to ask questions, not fine to paste a college assignment alone.

What have you tried? Where are you stuck?

2 Likes

Oh, sorry. Figured youā€™d need all info in order to help me.

Iā€™ve tried;

makePol :: Float ā†’ Float ā†’ Cplex
makePol r v
| v > 2pi = error ā€œV is not in intervalā€
| v < (-2)pi = error ā€œv is not in intervalā€
| v < 0 = (ā€œPā€,r
(cos (v+2
pi)),r*(sin (v+2pi)))
| otherwise = (ā€œPā€,r
(cos v),r*(sin v))

But Iā€™m unsure wether or not it is correct to use guards in my case or if I used them correctly.

Your code wonā€™t compileā€¦ Cplex data declaration is missing. Does the compiler error tell you anything useful?

Oh sorry, only posted the things I thought were useful for this assignment. Hereā€™s my full ā€œmoduleā€

type Cplex = (String,Float,Float)

makeRec :: Float ā†’ Float ā†’ Cplex
makeRec a b = (ā€œRā€,a,b)

makePol :: Float ā†’ Float ā†’ Cplex
makePol r v
| v > 2pi = error ā€œv is not in intervalā€
| v < (-2)pi = error ā€œv is not in intervalā€
| v < 0 = (ā€œPā€,r
(cos (v+2
pi)),r*(sin (v+2pi)))
| otherwise = (ā€œPā€,r
(cos v),r*(sin v))

type Cplex = (String,Float,Float)

makeRec :: Float -> Float -> Cplex
makeRec a b = ("R",a,b)

makePol :: Float -> Float -> Cplex
makePol r v
    | v > 2pi = error "v is not in interval"
    | v < (-2)pi = error "v is not in interval"
    | v < 0 = ("P",r(cos (v+2pi)),r*(sin (v+2pi)))
    | otherwise = ("P",r(cos v),r*(sin v))

Here it is properly idented and without Unicode. First error GHC throws at me is:

prova.hs:10:20: error:
    ā€¢ Couldn't match expected type ā€˜Float -> Floatā€™
                  with actual type ā€˜Floatā€™
    ā€¢ The function ā€˜rā€™ is applied to one value argument,
        but its type ā€˜Floatā€™ has none
      In the expression: r (cos (v + 2 pi))
      In the expression: ("P", r (cos (v + 2 pi)), r * (sin (v + 2 pi)))
   |
10 |     | v < 0 = ("P",r(cos (v+2pi)),r*(sin (v+2pi)))
   |                    ^^^^^^^^^^^^^^

And indeed r is a Float. So what are you trying to do with r(cos (v+2pi))? @minimoll you need to learn how to heed the compiler and simple exercises like these will become a breeze.

Yes. In most computer languages you need to explicitly say multiplication, using the * operator. See also 2pi.

After that it compiles and links.

Does it do the right thing now?

1 Like