0% found this document useful (0 votes)
3 views9 pages

Lab 2

This lab report for the course 23CSE212 details various Haskell programming exercises focusing on functions, type classes, and list processing. It includes code implementations and test cases for functions such as always0, subtract, isequal, and checksum, among others. Each function is designed to demonstrate specific programming concepts and their outputs are provided for validation.

Uploaded by

Ad K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Lab 2

This lab report for the course 23CSE212 details various Haskell programming exercises focusing on functions, type classes, and list processing. It includes code implementations and test cases for functions such as always0, subtract, isequal, and checksum, among others. Each function is designed to demonstrate specific programming concepts and their outputs are provided for validation.

Uploaded by

Ad K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Report

23CSE212 – Principles of Programming Languages

Excellent Good Poor


Criteria
Timely
Submission
Correctness
of lab
assignment
Total
Faculty Signature

Lab Session No: 1


Date: 14-02-2025

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.

Code Testcases (Input & Output)

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)

ghci> subtract 5-2


subtract :: Int -> Int ->Int 3
subtract a b =a-b ghci> subtract 41.2-31.2
error

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)

ghci> subtractNums 10.2 3.1


subtractNums :: Float -> Float ->
7.1
Float
ghci> subtractNums 10.0 3.0
subtractNums a b = a-b
7.0

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)

ghci> isEquallNum 9.2 6.1


isEquallNum :: Float -> Float -
False
>Bool
ghci> isEquallNum 9.2 9.2
isEquallNum a b = a==b
True

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

Question 7: Define a function compareNums which takes 2 numbers and returns


a. GT if the first parameter is greater than the second,
b. LT if the first parameter is lesser than the second
c. or EQ
where GT, LT and EQ are Ordering 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> compareNums 4 1
compareNums :: Ord a => a->a->
GT
Ordering
ghci> compareNums 10 10
compareNums x y = compare x y
EQ

Question 8: Define a function


threeDifferent : : Integer->Integer->Integer->Bool
so that the result of threeDifferent m n p is True only if all three of the numbers m, n and p
are different.

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)

threeDifferent :: Integer -> ghci> threeDifferent 45 435 3


Integer -> Integer ->Bool True
threeDifferent m n p = (m /= n) && ghci> threeDifferent 1 2 2
(n /= p) && (m /= p) False

Page 5 of 9
Lab Report
23CSE212 – Principles of Programming Languages

Question 9: Define the function


strToNum :: String->Int
which converts any integer like "8" to its value, 8.

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> strToNum "2"


strToNum :: String -> Int 2
strToNum [c] = digitToInt c ghci> strToNum "3"
3

Question 10: Given the following function definition


twoStrings a b = putStrLn (a++ "\n" ++ b )

Find the type signature of the function twoStrings.

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

ghci> twoStrings "aditya" "Kiran"


Aditya
twoStrings a b = putStrLn (a ++ "\ Kiran
n" ++ b) ghci> twoStrings "adi" "kira"
adi
kira

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)

ghci> threeString "Hi" "heyy"


"Hiii"
Hi
threeString :: [Char] -> [Char] ->
heyy
[Char] -> IO ()
Hiii
threeString a b c = putStrLn (a ++
ghci> threeString "I" "am" "adi"
"\n" ++ b++ "\n" ++c)
I
am
adi

Page 7 of 9
Lab Report
23CSE212 – Principles of Programming Languages

Question 12: Define a function


tellAge :: String->Int->String
which takes the name and year of birth and prints the age of the person in 2025. Use show
function to convert any type to String.

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."

Question 13: Define a function


checksum m n :: Int->Int->String
which takes two numbers and creates a new number by appending the digit at the unit place
of mand n with the sum of (m+n)
Ex: checksum 12 13
"255"
(2+3) -> 5 is appended to sum of 12+13-> 25 append 5 > 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 8 of 9
Lab Report
23CSE212 – Principles of Programming Languages

checksum :: Int -> Int -> String


checksum m n = show (sumMN * 10 +
ghci> checksum 1 1
unitDigit)
"22"
where
ghci> checksum 2 2
sumMN = m + n
"44"
unitDigit = (m `mod` 10 + n
`mod` 10) `mod` 10

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)

checksumInt :: Int -> Int -> Int


checksumInt m n = sumMN * 10 +
ghci> checksumInt 12 13
unitDigit
255
where
ghci> checksumInt 12 12
sumMN = m + n
244
unitDigit = (m `mod` 10 + n
`mod` 10) `mod` 10

Page 9 of 9

You might also like