I'm new to Haskell and I'm having kind of a hard time joining two sections of code for this program. What it does (or should do) is to let me know if a triangle is isosceles by the theorem of cosenes. Here it is what I thought would work:
--Determine if a triangle is isosceles by the cosene theroem
module Main where
sides :: (Float, Float, Float) -> (Float, Float, Float)
sides (a, b, c) = (x, y, z)
where
x = acos((b^2 + c^2 - a^2) / (2 * b * c))
y = acos((a^2 + c^2 - b^2) / (2 * a * c))
z = acos((a^2 + b^2 - c^2) / (2 * a * b))
theorem :: (Float, Float, Float) -> String
theorem (x, y, z)
|(x==y && x/=z) = "Es isosceles"
|(x==z && x/=y) = "Es isosceles"
|(y==z && y/=x) = "No es isosceles"
main :: IO()
main = do
print "Please type the size of the triangle faces: "
(a, b, c) <- getLine
(x, y, z) <- sides (a, b, c)
string <- theorem(x, y, z)
print string
I'm not sure that after my advice updated code will run, but I see some problems in this code:
print
isn't exactly what you want, because strings will be printing with quotes ("Please type the size of the triangle faces: "
insteadPlease type the size of the triangle faces:
). If you want print a string, replaceprint
toputStrLn
getLine
has typeIO String
, so pattern matching with(a, b, c)
will be failed. But you can writeread <$> getLine
- it will apply functionread
which convert a string to a value of any readable type (in this case it is(Float, Float, Float)
) to the input string. But the input string must be in format(_, _, _)
(for example(1, 1, 1)
)when you write
a <- b
in do-block, it means thatb
has typem t
wherem
isMonad
, anda
has typet
.sides
has type(Float, Float, Float) -> (Float, Float, Float)
, so you can't write this insteadb
in the expression above. But you can writelet a = b
where botha
andb
has the same typet
. So you can writelet (x, y, z) = sides (a, b, c)
instead(x, y, z) <- sides (a, b, c)
. Similarly withstring <- theorem(x, y, z)
theorem
don't defined for all values(x, y, z)
because you consider cases with the pair of equals value and one not equal