Reputation: 335
I'm in the process of learning haskell, so I tried to make a the following function to see if I could do it.
This is what I wrote
projectileY :: (Num a, Fractional a) => a -> a -> a -> a -> a
projectileY gravity time vInit height = let equation gr ti veli hei = -0.5*g*(t^2)+vi*t+h
in if (equation g t vi h < 0) then 0 else (equation g t vi h)
where
g = gravity
t = time
vi = vInit
h = height
main :: IO()
main = do
print (projectileY 9.8 0.1 2.0 100)
This was the error
parse error (possibly incorrect indentation or mismatched brackets)
So I tried messing with the indentation for a while, with no success. Am I allowed to use "in" and "where" like this?
Upvotes: 0
Views: 115
Reputation: 2373
In addition to what others wrote about indents, it hardly makes sense to define a local function and then to twice apply it with the same set of arguments. On the whole, you make it all too complicated for no gain.
Why not write simply?:
projectileY g t v0 h0 = max 0.0 (-0.5 * g * (t ^ 2) + v0 * t + h0)
As another style note, in physical formulae, I find it much more readable to use conventional short identifiers for variables and constants, such as g
, h
and t
.
Upvotes: 0
Reputation: 48746
If you change the type signature and do some cleaning up (proper indentation, not mixing tabs and spaces), it should work. This is an working code:
projectileY :: (Fractional a, Ord a) => a -> a -> a -> a -> a
projectileY gravity time vInit height = let equation gr ti veli hei = -0.5*g*(t^2)+vi*t+h
in if (equation g t vi h < 0)
then 0
else (equation g t vi h)
where
g = gravity
t = time
vi = vInit
h = height
main :: IO()
main = print (projectileY 9.8 0.1 2.0 100)
Am I allowed to use "in" and "where" like this?
Yes. But note that in
should be used alongside with let
. The let in syntax should form an expression.
Upvotes: 5