0% found this document useful (0 votes)
19 views6 pages

Lab 1

The lab report for 23CSE212 outlines various Haskell programming tasks, including functions for string concatenation, mathematical operations, and transformations. Each question includes the function definition, type signatures, and example test cases demonstrating the expected output. The report emphasizes the development of Haskell programs based on type classes, function definitions, higher-order functions, and list processing.

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)
19 views6 pages

Lab 1

The lab report for 23CSE212 outlines various Haskell programming tasks, including functions for string concatenation, mathematical operations, and transformations. Each question includes the function definition, type signatures, and example test cases demonstrating the expected output. The report emphasizes the development of Haskell programs based on type classes, function definitions, higher-order functions, and list processing.

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/ 6

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: 12-02-2025

Question 1: Write a Haskell program to print your name, roll number, and department. [use
.hs file to write the script, to concat string use “++”]

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
func :: String -> String -> String
*Main> func "D Aditya Kiran, " "15,
-> String
" "CSE" "D Aditya Kiran, 15, CSE"
func name rollno dept = name ++
rollno ++ dept
*Main> func "Kira, " "11, " "CSE"
"Kira, 11, CSE"

Question 2: Define a function squareNum that takes an integer as input and returns its
square. Use it to test for various integer inputs.

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.

ghci Qn1.h

squareNum :: Integer -> Integer *Main> squareNum 6


squareNum n = n*n 36
*Main> squareNum 15
225

Signed By Lab Instructor: Page 1 of 6


Lab Report
23CSE212 – Principles of Programming Languages

Question 3: Define a function cube that takes an integer as input and returns its cube.
Provide the type signature.

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
ghci Qn1.h

cube :: Integer -> Integer *Main> cube 5


cube n = n*n*n 125
*Main> squareNum 9
729

Question 4: Create a function maxOfTwo that takes two integers and returns the larger of
the two [make us eof predefined Max function] .

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
ghci Qn1.h
maxOfTwo :: Integer -> Integer ->
Integer *Main> maxOfTwo 5 8
maxOfTwo x y = max x y 8
*Main> maxOfTwo 13 10
13

Question 5: Write a function swap that takes two integer arguments and returns them in
swapped order as a tuple.

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
ghci Qn1.h
swap :: Integer -> Integer ->
(Integer, Integer) *Main> swap 5 10
swap x y = (y, x) (10,5)
*Main> swap 15 10
(10,15)

Signed By Lab Instructor: Page 2 of 6


Lab Report
23CSE212 – Principles of Programming Languages

Question 6: Write a Haskell program that asks for the user's name and then prints "Hello,
[Name]!"

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
ghci> namePrint "Dattanand"
namePrint :: String -> String "Hello, Dattanand"
namePrint x = "Hello, "++x ghci> namePrint "Ganesh"
"Hello, Ganesh"

Question 7: Define a function absValue that takes an integer and returns its absolute value
using pattern matching. [ make use of abs function of Haskell]

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
ghci> absValue 10
absValue :: Integer -> Integer
absValue 0 = 0
10
absValue n = abs n ghci> absValue (-10)
10

Question 8: Write a program to compute the area of a rectangle by defining a function


areaRectangle that takes length and breadth as arguments.

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
areaRectangle :: Double -> Double -
ghci> areaRectangle 10 15
> Double
150.0
areaRectangle l b = l * b
ghci> areaRectangle 8 12
96.0

Signed By Lab Instructor: Page 3 of 6


Lab Report
23CSE212 – Principles of Programming Languages

Question 9: Define a function charToAscii that takes a character as input and returns its
ASCII value. [ord function converts a character to ascii]

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
ghci> chartoAscii 'B'
import Data.Char(ord)
66
chartoAscii :: Char -> Int
ghci> chartoAscii 'b'
chartoAscii c = ord (c)
98

Question 10: Demonstrate function composition in Haskell by defining and combining


two simple functions (e.g., one that doubles a number and another that adds 5 to
it).

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
doubleNumber :: Double -> Double
ghci> addFiveToResult 10
addFiveToResult :: Double -> Double
25.0
doubleNumber x = 2*x
ghci> addFiveToResult 2
addFiveToResult y = doubleNumber
9.0
y+5

Question 11: Define a function calculate that performs the following operations on an
integer:
 Multiply the input by 3 (use a function for the operation)
 Add 5 to the result
 Subtract 2
 Take the square of the result (use a function for the operation)

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
calculate :: Integer -> Integer
square :: Integer -> Integer
ghci> calculate 5
multiply :: Integer -> Integer
324
multiply x = x * 3
ghci> calculate 6
square n = n * n
441
calculate x = square((multiply x) +
5 - 2)

Signed By Lab Instructor: Page 4 of 6


Lab Report
23CSE212 – Principles of Programming Languages

Question 12: Define a function transform that performs the following operations on an
integer:
 Subtract 10 from the input
 Multiply the result by 4
 Divide the result by 2
 Add 1 to the result

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
transform :: Integer -> Integer
multiply4 :: Integer -> Integer ghci> transform 65
divide :: Integer -> Integer 111
multiply4 x = (x-10) * 4 ghci> transform 52
divide x = (multiply4 x) `div` 2 85
transform x = (divide x) + 1

Question 13: Define a function complexTransform that performs the following operations
on an integer:
 First, subtract 5 from the input.
 Then, square the result.
 Next, multiply the result by 3.
 Finally, apply another function halfThenAddTen to the result, which
 performs the following:
o Divide the number by 2
o Add 10 to the result

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.
complexTransform :: Integer ->
Integer
complexTransform x = halfThenAddTen
ghci> complexTransform 5
(operation x)
10
operation :: Integer -> Integer
ghci> complexTransform 1
operation n = 3 * ((n - 5) ^ 2)
34
halfThenAddTen :: Integer ->
Integer
halfThenAddTen y = (y `div` 2) + 10

Signed By Lab Instructor: Page 5 of 6


Lab Report
23CSE212 – Principles of Programming Languages

Question 14: Define a function linearTransform that performs the following operations on
an integer:
 First, apply a linear transformation: f(x)= 3x+ 4
 Then, apply a scaling transformation: g(y) = 2y − 5
 Finally, apply a normalization-like transformation: ℎ(z) = z/2+6
The overall function: ℎ(g(f(x))) is?

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.

linearTransform::Integer->Integer
ghci> linearTransform 5
linearTransform x = ((2*(3*x+4) -
22
5) `div` 2) + 6
ghci> linearTransform 6
25

Question 15: Define a function affineTransform that applies the following transformations
to an integer:
 Apply a shift transformation (translation): f(x)=x+7
 Apply a scaling transformation (multiplication): g(y)=4y−3
 Apply a projection-like transformation (division + shift): ℎ(z) = z/3+2
The overall function: ℎ(g(f(x)))

CO2: Develop Haskell programs to solve basic programming problems based on type
classes, function definitions, higher-order functions, and list processing.

ghci> affineTransform 5
affineTransform::Integer->Integer
17
affineTransform x = ((4*(x+7) - 3)
ghci> affineTransform 30
`div` 3) + 2
50

Signed By Lab Instructor: Page 6 of 6

You might also like