Saturday 30 April 2016

Haskell: Maybe: programming with out null


Maybe data type is used to represent values that may or may not be present. A value of type 'Maybe x' either contains a value of type x (represented as Just x), or it is empty (represented as Nothing).

Maybe data type is defined like below.
data Maybe a = Just a | Nothing
     deriving (Eq, Ord)


For example, suppose you had given a list of employees and an id to search the list. Your method returns an employee if it exists, else nothing.

Following snippet define an Employee type.
data Employee = Employee{
    empId :: Integer,
    firstName :: String,
    lastName :: String,
    designation :: String
} deriving (Show)


Following is the signature of getEmployee function, it takes employeeId, list of Employees and return an Employee if it exist, else Nothing.
getEmployee :: Integer -> [Employee] -> Maybe Employee


Following is the definition of the function getEmployee.
getEmployee employeeId [] = Nothing
getEmployee employeeId (x:xs)
    |((empId x) == employeeId) = Just x
    |otherwise = getEmployee employeeId xs


You can create Employee instances like below.
emp1 = Employee{empId=1, firstName="Hari Krishna", lastName="Gurram", designation="Software Engineer"}
emp2 = Employee{empId=2, firstName="Sudheer", lastName="Sami", designation="Engineer"}
emp3 = Employee{empId=3, firstName="Harika", lastName="Kunche", designation="Senior Software Engineer"}
emp4 = Employee{empId=4, firstName="Sailaja", lastName="PTR", designation="HR"}
emp5 = Employee{empId=5, firstName="Jaideep", lastName="Gera", designation="Manager"}


Following statement creates list of Employees.

employees = emp1:emp2:emp3:emp4:emp4:[]

Now you can call the getEmployee function by passing employee id to search, list of employees.

For example,

getEmployee 3 employees : Return the Employee Just (Employee {empId = 3, firstName = "Harika", lastName = "Kunche", designation = "Senior Software Engineer"})

getEmployee 10 employees : Return Nothing.

Following is the complete working application.

EmployeeUtil.hs
import Data.Maybe

data Employee = Employee{
    empId :: Integer,
    firstName :: String,
    lastName :: String,
    designation :: String
} deriving (Show)

getEmployee :: Integer -> [Employee] -> Maybe Employee
getEmployee employeeId [] = Nothing
getEmployee employeeId (x:xs)
    |((empId x) == employeeId) = Just x
    |otherwise = getEmployee employeeId xs

*Main> :load EmployeeUtil.hs
[1 of 1] Compiling Main             ( EmployeeUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> let emp1 = Employee{empId=1, firstName="Hari Krishna", lastName="Gurram", designation="Software Engineer"}
*Main> let emp2 = Employee{empId=2, firstName="Sudheer", lastName="Sami", designation="Engineer"}
*Main> let emp3 = Employee{empId=3, firstName="Harika", lastName="Kunche", designation="Senior Software Engineer"}
*Main> let emp4 = Employee{empId=4, firstName="Sailaja", lastName="PTR", designation="HR"}
*Main> let emp5 = Employee{empId=5, firstName="Jaideep", lastName="Gera", designation="Manager"}
*Main> 
*Main> let employees = emp1:emp2:emp3:emp4:emp4:[]
*Main> 
*Main> getEmployee 3 employees
Just (Employee {empId = 3, firstName = "Harika", lastName = "Kunche", designation = "Senior Software Engineer"})
*Main> 
*Main> getEmployee 10 employees
Nothing
*Main> 
*Main> getEmployee (-1) employees
Nothing
*Main> 
*Main> getEmployee 4 employees
Just (Employee {empId = 4, firstName = "Sailaja", lastName = "PTR", designation = "HR"})
*Main> 


Previous                                                 Next                                                 Home

No comments:

Post a Comment