Lab 1
Lab 1
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
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
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)
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
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
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
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)
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
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