Lab 2
Lab 2
Question 1: Write a function named always0 ::Int→ Int. The return value should always
just be 0.
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
ghci Qn1.h
ghci> always0 7
always0 :: Int -> Int
0
always0 a=0
ghci> always0 0
Page 1 of 9
Lab Report
23CSE212 – Principles of Programming Languages
Question 2: Write a function subtract :: Int-> Int-> Int that takes two numbers (that is, Ints)
and subtracts them.
Will the above function work for Float type arguments? Call the function with float values?
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Page 2 of 9
Lab Report
23CSE212 – Principles of Programming Languages
Question 3: Define a function subtractNums which should perform subtraction on int and
well as float values.
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Question 4: Define a function isequalInt with its function type to check if two integers are
same. It shouldreturn True if they are same or else False. Give the appropriate function
type.
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
ghci> isEquallInt 10 10
isEquallInt :: Int -> Int -> Bool True
isEquallInt a b =a==b ghci> isEquallInt 9 8
False
Page 3 of 9
Lab Report
23CSE212 – Principles of Programming Languages
Question 5: Define a function isequalNum to check if any two numbers are the same. This
function should work on both int and float parameters.
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Question 6: Define a function isequal, so that it can accepts values of any type (Char,
String, list, integer, float,etc), which returns Bool value
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
ghci> isequal 3 4
isequal :: Eq a=> a-> a-> Bool False
isequal a b= a==b ghci> isequal 3 3
True
Page 4 of 9
Lab Report
23CSE212 – Principles of Programming Languages
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
ghci> compareNums 4 1
compareNums :: Ord a => a->a->
GT
Ordering
ghci> compareNums 10 10
compareNums x y = compare x y
EQ
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Page 5 of 9
Lab Report
23CSE212 – Principles of Programming Languages
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Page 6 of 9
Lab Report
23CSE212 – Principles of Programming Languages
Question 11: Define a new function threeString with the type signature observed in
Question 10.threeString takes three strings and returns a single string which when printed
shows the three strings on separate lines.
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Page 7 of 9
Lab Report
23CSE212 – Principles of Programming Languages
Ex: tellAge "Anu" 2000 should give the output "Anu is 25 years old in 2025".
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
tellAge :: String -> Int -> String ghci> tellAge "Anu" 2000
tellAge name birthYear = name ++ " "Anu is 25 years old in 2025."
is " ++ show (2025 - birthYear) ++ ghci> tellAge "Adi" 2001
" years old in 2025." "Adi is 24 years old in 2025."
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Page 8 of 9
Lab Report
23CSE212 – Principles of Programming Languages
Question 14: Modify the above function to print the result as Integer.
ghci> checksum 12 13
255
CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
Code Testcases (Input & Output)
Page 9 of 9