functions in purescript - UCS - Unleash-Coding-Skills

functions in purescript - UCS

Functions in purescript:

Normally a function is one which takes some parameters and return a value or none.But in purescript there is no function that returns zero arguments.Means that your function must return one value definitely.

Function :: Parameter1type -> Parameter2type -> .. ->returntypeParametertype
Function x y ...  xn = do some operation
                                    return parameter of defined type in declaration

In this
Function :: Parameter1type -> Parameter2type -> .. ->returntypeParametertype

is declaration of the function and parameter1type .. are the types of parameters like Int,String ,Boolean or any defined type.

And ,
Function x y ...  xn => In this x ,y .. xn represent the receiving parameters we can use them in our code with that names.
Example :
Add :: Int -> Int -> Int
Add x y = x+y

In this x and y are input parameters and x+y is return it is also must be Int value.

You can also add match statements and return output based on input in  case of boolean type or any datatype.Consider an example.

Valid :: Boolean -> Boolean -> Boolean
Valid true false = true
Valid false true = false
Valid false false = true
Valid _  _ =true

The last Valid _ _ = true indicates if our input does not match with any of the above cases before it.It will automatically return it.It is just like default case for functions to return data.

If you not write code for all valid cases for our functions while returning it will throw some errors since compiler knows that you are missing some cases.
So better to use default case in function if you are using the function in that manner.

You can also write functions for any datatype based on your requirement.

Happy Coding....
Please add comments for queries

No comments:

Post a Comment