From db07bba64ca135a2472378cc8e365a2fd3828b09 Mon Sep 17 00:00:00 2001
From: Ramaguru Radhakrishnan
<7790256+ramagururadhakrishnan@users.noreply.github.com>
Date: Mon, 13 May 2024 11:17:23 +0530
Subject: [PATCH] Mid-Term Solutions
---
Assets/Solutions/Mid-Term/Mid-Term.md | 161 ++++++++++++++++++++++++++
1 file changed, 161 insertions(+)
create mode 100644 Assets/Solutions/Mid-Term/Mid-Term.md
diff --git a/Assets/Solutions/Mid-Term/Mid-Term.md b/Assets/Solutions/Mid-Term/Mid-Term.md
new file mode 100644
index 0000000..6413d41
--- /dev/null
+++ b/Assets/Solutions/Mid-Term/Mid-Term.md
@@ -0,0 +1,161 @@
+# 20CYS312 - Principles of Programming Languages
+![](https://img.shields.io/badge/Batch-21CYS-lightgreen) ![](https://img.shields.io/badge/UG-blue) ![](https://img.shields.io/badge/Subject-PPL-blue)
+![](https://img.shields.io/badge/Lecture-2-orange) ![](https://img.shields.io/badge/Practical-3-orange) ![](https://img.shields.io/badge/Credits-3-orange)
+
+## Mid-Term Question Paper with Solutions
+
+#### 1. Write the \textit{type signature} of the below functions.
+(a) **zip :: [a] → [b] → [(a, b)]**
+(b) **filter :: (a → Bool) → [a] → [a]**
+
+#### 2. Identify the type of the following values.
+ (a) "C" - **String or [Char]**
+ (b) (85, 'Y', [True]) - **(Int, Char, [Bool])**
+ (c) ["S":[]] - **[[String]] or [[[Char]]]**
+
+#### 3. Write a function '_pairswap_' that swaps the elements of a list on a pair-wise basis.
+Assumption: the list has an even number of elements.
+Usage Example:
+ 1. \> _pairswap_ [1..6]
+ [2,1,4,3,6,5]
+ 2. \> _pairswap_ "ammu"
+ "maum"
+ 3. \> _pairswap_ []
+ []
+
+**Solution:**
+```
+pairswap:: [a] -> [a]
+pairswap [ ] = [ ]
+pairswap (x:y:xs) = y:x:pairswap(xs)
+```
+
+#### 5. Write the expression equivalent to the following list comprehension using map and/or filter.
+(a) [ reverse s | s ← strs, even (length s) ]
+(b) [ length word | word ← words, length word > 5 ]
+(c) [ (x, y) | x ← [1..5], y ← [1..5], x + y == 6 ]
+(d) [ x^2 | x ← [1..10], even x ]
+
+**Solution:**
+(a)
+(b)
+(c)
+(d)
+
+#### 6. Write a Haskell code to generate the below HTML file with custom _body_ and _footer_ as mentioned.
+ - body = "Mid-Term Examination"
+ - footer = "19-March-2024"
+```
+
+
+
+
+ Haskell Generated HTML
+
+
+
+
+ Content generated by Haskell:
+
+ /* body content as mentioned above */
+
+
+
+
+
+
+```
+
+**Solution:**
+```
+
+```
+
+#### 7. Implement a Queue with the isEmpty, enqueue, dequeue and front function using Haskell data type with Maybe.
+
+**Solution:**
+```
+data Queue a = Queue { elements :: [a] }
+
+-- Check if the queue is empty
+isEmpty :: Queue a -> Bool
+isEmpty (Queue []) = True
+isEmpty _ = False
+
+-- Enqueue an element into the queue
+enqueue :: a -> Queue a -> Queue a
+enqueue x (Queue xs) = Queue (xs ++ [x])
+
+-- Dequeue an element from the queue
+dequeue :: Queue a -> Maybe (a, Queue a)
+dequeue (Queue []) = Nothing
+dequeue (Queue (x:xs)) = Just (x, Queue xs)
+
+-- Dequeue an element from the queue
+-- dequeue :: Queue a -> Maybe (Queue a)
+-- dequeue (Queue []) = Nothing
+-- dequeue (Queue (_:xs)) = Just (Queue xs)
+
+
+-- Get the front element of the queue
+front :: Queue a -> Maybe a
+front (Queue []) = Nothing
+front (Queue (x:_)) = Just x
+
+main :: IO ()
+main = do
+ let emptyQueue = Queue ([] :: [Int])
+ let queue1 = enqueue 1 emptyQueue
+ let queue2 = enqueue 2 queue1
+
+ putStrLn "Testing Queue Implementation:"
+ putStrLn "Initial queue:"
+ putStrLn $ "Is empty? " ++ show (isEmpty emptyQueue)
+ putStrLn $ "Front element: " ++ show (front emptyQueue)
+ putStrLn ""
+
+ putStrLn "After enqueueing 1:"
+ putStrLn $ "Is empty? " ++ show (isEmpty queue1)
+ putStrLn $ "Front element: " ++ show (front queue1)
+ putStrLn ""
+
+ putStrLn "After enqueueing 2:"
+ putStrLn $ "Is empty? " ++ show (isEmpty queue2)
+ putStrLn $ "Front element: " ++ show (front queue2)
+ putStrLn ""
+
+ putStrLn "Dequeueing:"
+ case dequeue queue2 of
+ Just (element, newQueue) -> do
+ putStrLn $ "Dequeued element: " ++ show element
+ putStrLn $ "New front element: " ++ show (front newQueue)
+ putStrLn $ "Is empty? " ++ show (isEmpty newQueue)
+ Nothing -> putStrLn "Queue is empty"
+```
+
+#### 8. Which of the following is NOT a key characteristic of Haskell?
+**Solution:** Mutable variables
+
+#### 9. Given the following data type declaration, What does the deriving (Show) part indicate?
+``` data Color = Red | Green | Blue deriving (Show). ```
+**Solution:** It automatically generates a Show instance for the Color type
+
+#### 10. Complete the following Haskell function findMax.
+```
+-- Consider the following incomplete Haskell function definition for
+finding the maximum element in a list
+findMax :: [Int] -> Int
+findMax [] = ???
+findMax (x:xs) = ???
+-- Your task is to complete the definition of the ’findMax’ function to correctly find and
+ return the maximum element in the given list ’xs’.
+-- Assumption: the list ’xs’ is not empty
+```
+**Solution:**
+```
+findMax :: [Int] \rightarrow Int
+findMax [x] = x
+findMax (x:xs) = max$ x (findMax xs)
+```