 # 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