Sep 15, 2010

More about Haskell

Functions with if-then-else
myfunction f=
if x<0 then -1 else if x=0 0 else 1 Case statment In haskell we can write case statment also. casefunction c= case c of 0 -> 0
10 -> 1
100 -> 2
_ -> -1

We can write this as;
casefunction c =
case c of {0->0;10->1;100->2;_->-1}

This also gives the same answer.
But the order of these statments are very important. Because if you insert the default statement (_-> ) in the beggining you will get that as the result for every thing.


hello = do
putStrLn "Hello World"

Load this one and type hello you will see;
Hello world


input = do
putStrLn "What is your name ?"
name <- getLine putStrLn (" Welcome "++ name ++" ...!!! ") This will give user to input some thing. Then it will print it. You can use if else statments with inputs and outputs as; num x = if x==10 then do putStrLn "Num is equal to 10" else if x>10 then
do putStrLn "Num is greater than 10"
do putStrLn "Num is less than 10"

IF you load this thing and type as;
num 12
It will give the out put as
Num is greater than 10

Read Inputs

main =
do putStrLn "What is your name"
name <- getLine
if (read name) =="guest" then
do putStrLn ("Welcome "++ name ++" ..!! ")
do putStrLn "Opss ...!!!"

In this it will ask to user to input some thing.
Then it will read the input and chech the condition.

No comments:

Post a Comment

Your comments are always welcome ...