Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Haskell lab exercise

1.
halves :: [Int] -> [Int]
halves [] = [] -- Base case: an empty list results in an empty list
halves (x:xs) = (x `div` 2) : halves xs

main :: IO ()
main = do
let originalList = [1, 2, 3, 4, 5]
let result = halves originalList
print result

2.
stack :: [a] -> [a]
stack [] = [] -- If the list is empty, return an empty list
stack (x:xs) = xs ++ [x] -- Move the first element to the back

main :: IO ()
main = do
let originalList = [1, 2, 3, 4, 5]
let result = stack originalList
print result

3.
fibonacci :: Int -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)

main :: IO ()
main = do
let n = 10 -- Change n to compute a different Fibonacci number
let result = fibonacci n
putStrLn $ "The " ++ show n ++ "th Fibonacci number is: " ++ show result

4.
factors :: Int -> [Int]
factors n = [x | x <- [2..n-1], n `mod` x == 0]

main :: IO ()
main = do
let n = 12 -- Change n to find factors of a different integer
let result = factors n
putStrLn $ "Factors of " ++ show n ++ ": " ++ show result

5.
pivot :: Ord a => a -> [a] -> ([a], [a])
pivot _ [] = ([], []) -- Base case: empty list
pivot pivotValue (x:xs)
| x <= pivotValue = (x : left, right)
| otherwise = (left, x : right)
where
(left, right) = pivot pivotValue xs

main :: IO ()
main = do
let pivotValue = 3
let inputList = [5, 6, 4, 2, 1, 3]
let (left, right) = pivot pivotValue inputList
putStrLn $ "Elements less than or equal to " ++ show pivotValue ++ ": " ++ show left
putStrLn $ "Elements greater than " ++ show pivotValue ++ ": " ++ show right

6.
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)

treeHeight :: Tree a -> Int


treeHeight EmptyTree = 0
treeHeight (Node _ leftSubtree rightSubtree) =
1 + max (treeHeight leftSubtree) (treeHeight rightSubtree)

main :: IO ()
main = do
let tree = Node 1
(Node 2
(Node 4 EmptyTree EmptyTree)
EmptyTree)
(Node 3 EmptyTree EmptyTree)
let height = treeHeight tree
putStrLn $ "Height of the tree: " ++ show height

8.
merge :: Ord a => [a] -> [a] -> [a]
merge [] ys = ys -- If the first list is empty, the result is the second list.
merge xs [] = xs -- If the second list is empty, the result is the first list.
merge (x:xs) (y:ys)
| x <= y = x : merge xs (y:ys) -- If x is smaller or equal, include x in the result.
| otherwise = y : merge (x:xs) ys -- If y is smaller, include y in the result.

main :: IO ()
main = do
let list1 = [1, 3, 5, 7]
let list2 = [2, 4, 6, 8]
let mergedList = merge list1 list2
putStrLn $ "Merged List: " ++ show mergedList

9.
mergeSort :: Ord a => [a] -> [a]
mergeSort [] = [] -- Base case: empty list is already sorted
mergeSort [x] = [x] -- Base case: singleton list is already sorted
mergeSort xs = mergeSortedLists (mergeSort leftHalf) (mergeSort rightHalf)
where
(leftHalf, rightHalf) = splitAt (length xs `div` 2) xs

mergeSortedLists :: Ord a => [a] -> [a] -> [a]


mergeSortedLists [] ys = ys -- If the first list is empty, the result is the second list.
mergeSortedLists xs [] = xs -- If the second list is empty, the result is the first list.
mergeSortedLists (x:xs) (y:ys)
| x <= y = x : mergeSortedLists xs (y:ys) -- If x is smaller or equal, include x in the result.
| otherwise = y : mergeSortedLists (x:xs) ys -- If y is smaller, include y in the result.

main :: IO ()
main = do
let myList = [8, 4, 2, 7, 30, 5, 299, 3, 6]
let sortedList = mergeSort myList
putStrLn $ "Sorted List: " ++ show sortedList

12.

main :: IO ()
main = do
let numbers = [1..100]
mapM_ (printNumber) numbers

printNumber :: Int -> IO ()


printNumber n
| n `mod` 3 == 0 && n `mod` 5 == 0 = putStrLn "NITAndhra"
| n `mod` 3 == 0 = putStrLn "NIT"
| n `mod` 5 == 0 = putStrLn "Andhra"
| otherwise = print n
13.
rotateList :: Int -> [a] -> [a]
rotateList _ [] = [] -- If the list is empty, no rotation needed
rotateList k xs = take len $ drop (k `mod` len) $ cycle xs
where
len = length xs

main :: IO ()
main = do
let inputList = [1, 2, 3, 4, 5, 6, 7]
let k = 2
let rotatedList = rotateList k inputList
putStrLn $ "Input List: " ++ show inputList
putStrLn $ "Rotated List: " ++ show rotatedList
17.
-- Calculate the binomial coefficient (n choose k)
binomial :: Int -> Int -> Int
binomial n k
| k == 0 || k == n = 1
| otherwise = binomial (n - 1) (k - 1) + binomial (n - 1) k

-- Generate Pascal's Triangle up to a given number of rows


pascalsTriangle :: Int -> [[Int]]
pascalsTriangle n = [[binomial i j | j <- [0..i]] | i <- [0..n-1]]

-- Print Pascal's Triangle


printPascalsTriangle :: [[Int]] -> IO ()
printPascalsTriangle triangle = mapM_ (putStrLn . unwords . map show) triangle

main :: IO ()
main = do
let numRows = 5
let triangle = pascalsTriangle numRows
printPascalsTriangle triangle

18.

maxSubArraySum :: [Int] -> Int


maxSubArraySum [] = 0 -- Return 0 for an empty list
maxSubArraySum xs = maxSubArraySum' xs 0 0
where
maxSubArraySum' [] _ maxSum = maxSum
maxSubArraySum' (x:xs) currentSum maxSum =
let newSum = max x (currentSum + x)
newMax = max maxSum newSum
in maxSubArraySum' xs newSum newMax

main :: IO ()
main = do
let inputList = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
let maxSum = maxSubArraySum inputList
putStrLn $ "Maximum sum of a subarray: " ++ show maxSum

You might also like