Case Statement in Purescruipt - UCS - Unleash-Coding-Skills

Friday, 16 March 2018

Case Statement in Purescruipt - UCS







CaseStatement in Purescript:


The purescript will also have case statement like in C or C++.The only difference is that a little bit syntax change.The case statements are very much useful in purescript like traversing along the array by recursion.

function :: Int -> Int
function x = case x of
                     a -> some operation return type
                     b -> some operation return type
                     _ -> some operation return type

example:

getSum :: Int -> Int -> Int
getSum a b c = case a of
                          1 -> b + c
                          2 -> b * c
                          3 -> b / c
                          4 -> 0
                           _ -> 1

Like in C language _ -> is represented like default statement if no match to our case is done then it is be executed. In this, example a,b are integers .In the same way we can match those  cases according to our requirement.Here 'x' is input argument.

The important thing in purescript it also follows indentation so be careful while writing code in purescript.

Errors:

If you dont mention default case "_" in the cases list it will throw an error.
If multiple cases has the same value for cases matching  it will again show an
error

Happy Coding.
Few more tutorials will be added on purescript soon...

You comment your views below..........



No comments:

Post a comment