I'm new to Haskell and would like a few pointers on creating functions. I am used to writing in C#, Java, and C++, so this stuff is really foreign to me.
Here is the question that I am working on:
Declare the type and write a function in Haskell that takes three numbers as inputs and returns the larger one. Write two versions of this function: first use if else and second use guards.
In working on the first one I've come up with something like this...:
largestInt :: Int -> Int -> Int --Declaring type
largestInt a b c = if (a > b && a > c) then a --I don't think this is right, but it's all I have
else if (b > a && b > c) then b
else c
I've been using this video for help, but am not able to do the same things as him for some reason. (Meaning that I can't create that Main.hs file)
I am using WinGHCi
I would appreciate it if someone could show me how to setup functions, maybe even this one ^ in Haskell so that I know what I'm doing in the future. Thank you very much.
Updates:
-added else
to code
-fixed last if
statement
Screenshot:
if X then Y else Z
, exactly. You're missing an else clause in the three ifs you have in that snippet. – alecbzInt
s and returns anInt
, you want a function that takes threeInt
s and returns anInt
. – alecbzif
expression is still missing anelse
. (Hint: do you really need the thirdif
at all?) – alecbzlargestInt :: Int -> Int -> Int -> Int
? – Eric after dark.hs
file, you have to load it in GHCi to be able to access the functions in the file. You load it by typing:load path/to/your/file.hs
and pressing return in GHCi. – kqr