Week 4-7 Nptel Haskell HRST

You might also like

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

9/19/23, 5:24 PM Introduction To Haskell Programming - Course

(https://swayam.gov.in) (https://swayam.gov.in/nc_details/NPTEL)

hs16052003@outlook.com 

NPTEL (https://swayam.gov.in/explorer?ncCode=NPTEL) » Introduction To Haskell Programming (course)

If already registered, Week 4: Programming Assignment


click to check your
Due on 2023-08-24, 23:59 IST
payment status
1. Define a function f1 :: [Int] -> [Int] which takes a list l of nonnegative
numbers as input, and replaces each n in l by 3*n if n is a power of 3,
and by 0 if it is not a power of 3.
Course outline
Examples:
f1 [] = []
How does an NPTEL
f1 [1] = [3]
online course work?
f1 [1, 2, 3] = [3, 0, 9]
()
f1 [0, 2, 4, 6] = [0, 0, 0, 0]
Week 1: Introduction
()
2. For a list l, define S(l) to be the set of all indices i of l (remember that
indices start from 0) such that l!!i > l!!(i+1). Define a function
Week 2: Lists,
f2 :: [Int] -> [Int] which takes a nonempty list l of
Strings, Tuples () integers as input and outputs a S(l) in order.

Week 3: Rewriting, Examples:


Polymorphism, f2 [] = []
Higher Order f2 [1] = []
Functions on Lists () f2 [1, 2, 3, 2, 1] = [2, 3]
f2 [1, 2, 3, 4, 5, 6] = []
Week 4: Efficiency,
Sorting, Infinite lists, 3. Define a function f3 :: [Int] -> [Int] that removes adjacent duplicates.
Conditional i.e. if the same element occurs n times contiguously, we retain only one copy.
polymorphism, Using
ghci () Examples:
f3 [1, 1, 1, 2, 2, 3, 3, 3, 3] = [1, 2, 3]
Measuring efficiency f3 [1, 2, 1, 2, 3, 1, 1, 2, 2] = [1, 2, 1, 2, 3, 1, 2]
(unit?
unit=38&lesson=39)
4. Define a function f4 :: [Int] -> [[Int]] that partitions the list into all
Sorting (unit? its upruns. An uprun is a maximal non-decreasing segment of the given list.
unit=38&lesson=40)

Using infinite lists (unit?


Examples:
unit=38&lesson=41)
f4 [] = []
Conditional f4 [5] = [[5]]
polymorphism (unit?
f4 [1, 2, 3, 4, 5] = [[1,2,3,4,5]]
unit=38&lesson=42)
f4 [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1] = [[1,2,3,4,5,6],[5],[4],[3],[2],[1]]
Defining functions in
ghci (unit?
unit=38&lesson=43) Private
Test
Week 4 Feedback Form:
cases Input Expected Output Actual Output S
Introduction To Haskell
used for
Programming (unit?
evaluation
unit=38&lesson=44)
Test Case
Week 4: Programming f1 [1, 2, 3] [3,0,9] [3,0,9]\n
1
Assignment
Test Case
(/noc23_cs94/progassi f1 [0, 2, 4, 6] [0,0,0,0] [0,0,0,0]\n
2
gnment?name=98)
Test Case
f1 [21, 27, 22, 9] [0,81,0,27] [0,81,0,27]\n
Week 5: User-defined 3
datatypes, abstract

https://onlinecourses.nptel.ac.in/noc23_cs94/progassignment?name=98 1/3
9/19/23, 5:24 PM Introduction To Haskell Programming - Course

datatypes, modules () Test Case


f1 [3, 3, 3, 3] [9,9,9,9] [9,9,9,9]\n
4
Week 6: recursive Test Case
data types, search f1 [3, 6, 9, 12] [9,0,27,0] [9,0,27,0]\n
5
trees ()
Test Case f1 [1, 2, 2, 3, 4, 5,
[3,0,0,9,0,0,0,0,27,27] [3,0,0,9,0,0,0,0,27,27]\n
Week 7: arrays, IO () 6 8, 8, 9, 9]

Test Case
Week 8 () f1 [1, 3, 9, 27, 81] [3,9,27,81,243] [3,9,27,81,243]\n
7
Test Case
DOWNLOAD VIDEOS f2 [] [] []\n
8
()
Test Case
f2 [1] [] []\n
Text Transcripts () 9
Test Case
f2 [1, 2, 3, 2, 1] [2,3] [2,3]\n
Books () 10
Test Case
f2 [1, 2, 3, 4, 5, 6] [] []\n
Problem Solving 11
Session - July 2023 () Test Case
f2 [6, 5, 4, 3, 2, 1] [0,1,2,3,4] [0,1,2,3,4]\n
12

Test Case f2 [19, 29, 28, 38,


[1] [1]\n
13 45]

Test Case f2 [1, 1, 1, 2, 2, 3,


[] []\n
14 3, 3, 4, 4, 5, 5, 5]

Test Case f2 [2, 1, 3, 1, 4, 1,


[0,2,4,6,8] [0,2,4,6,8]\n
15 5, 1, 6, 1]

Test Case f2 [5, 4, 1, 2, 3, 4,


[0,1,5,7,9] [0,1,5,7,9]\n
16 3, 4, 1, 2, 0]

Test Case f3 [1, 1, 1, 2, 2, 3,


[1,2,3] [1,2,3]\n
17 3, 3, 3]

Test Case f3 [1, 2, 1, 2, 3, 1,


[1,2,1,2,3,1,2] [1,2,1,2,3,1,2]\n
18 1, 2, 2]

Test Case f3 [1, 2, 2, 1, 3, 3,


[1,2,1,3,4,1,2] [1,2,1,3,4,1,2]\n
19 4, 1, 2, 2]

Test Case f3
[1,2,3,4,5,6,7,8,9,10] [1,2,3,4,5,6,7,8,9,10]\n
20 [1,2,3,4,5,6,7,8,9,10]

f3 [1, 1, 1, 2, 2, 3,
Test Case
3, 3, 3, 4, 4, 5, 5, [1,2,3,4,5] [1,2,3,4,5]\n
21
5, 5, 5]

Test Case f3 [5, 4, 1, 2, 3, 4,


[5,4,1,2,3,4,3,4,1,2,0] [5,4,1,2,3,4,3,4,1,2,0]\n
22 3, 4, 1, 2, 0]

Test Case
f3 [1, 2, 3, 2, 1] [1,2,3,2,1] [1,2,3,2,1]\n
23
Test Case
f4 [] [] []\n
24
Test Case
f4 [5] [[5]] [[5]]\n
25
Test Case
f4 [1,2,3,4,5] [[1,2,3,4,5]] [[1,2,3,4,5]]\n
26
Test Case
f4 [5,4,3,2,1] [[5],[4],[3],[2],[1]] [[5],[4],[3],[2],[1]]\n
27

Test Case f4 [5, 4, 1, 2, 3, 4, [[5],[4],[1,2,3,4],[3,4],[1,2],


[[5],[4],[1,2,3,4],[3,4],[1,2],[0]]\n
28 3, 4, 1, 2, 0] [0]]

f4 [1, 1, 1, 2, 2, 3,
Test Case
3, 3, 3, 4, 4, 5, 5, [[1,1,1,2,2,3,3,3,3,4,4,5,5,5,5,5]]\n
29 [[1,1,1,2,2,3,3,3,3,4,4,5,5,5,5,5]]
5, 5, 5]

https://onlinecourses.nptel.ac.in/noc23_cs94/progassignment?name=98 2/3
9/19/23, 5:24 PM Introduction To Haskell Programming - Course

Test Case f4 [1, 2, 3, 4, 5, 6, [[1,2,3,4,5,6],[5],[4],[3],[2],


[[1,2,3,4,5,6],[5],[4],[3],[2],[1]]\n
30 5, 4, 3, 2, 1] [1]]

The due date for submitting this assignment has passed.


30 out of 30 tests passed.
You scored 100.0/100.

Assignment submitted on 2023-08-24, 19:05 IST


Your last recorded submission was :
1 isPowerOf3 :: Int -> Bool
2 isPowerOf3 n = n > 0 && 3 ^ (round (logBase 3 (fromIntegral n))) == n
3 ​
4 f1 :: [Int] -> [Int]
5 f1 [] = []
6 f1 (x:xs)
7 | isPowerOf3 x = 3 * x : f1 xs
8 | otherwise = 0 : f1 xs
9 ​
10 f2 :: [Int] -> [Int]
11 f2 xs = [i | i <- [0..length xs - 2], xs !! i > xs !! (i + 1)]
12 ​
13 f3 :: [Int] -> [Int]
14 f3 [] = []
15 f3 [x] = [x]
16 f3 (x:y:xs)
17 | x == y = f3 (y:xs)
18 | otherwise = x : f3 (y:xs)
19 ​
20 uprun :: [Int] -> ([Int], [Int])
21 uprun [] = ([], [])
22 uprun [x] = ([x], [])
23 uprun (x:y:xs)
24 | x <= y = let (up, rest) = uprun (y:xs) in (x : up, rest)
25 | otherwise = ([x], y:xs)
26 ​
27 f4 :: [Int] -> [[Int]]
28 f4 [] = []
29 f4 xs = let (up, rest) = uprun xs in up : f4 rest
30 ​
31 main = do
32 line <- getLine;
33 let (func, rest) = break (== ' ') line in
34 case func of
35 "f1" -> let args = read rest :: [Int] in
36 putStrLn . show $ f1 args
37 "f2" -> let args = read rest :: [Int] in
38 putStrLn . show $ f2 args
39 "f3" -> let args = read rest :: [Int] in
40 putStrLn . show $ f3 args
41 "f4" -> let args = read rest :: [Int] in
42 putStrLn . show $ f4 args

https://onlinecourses.nptel.ac.in/noc23_cs94/progassignment?name=98 3/3
WEEK 5
types, search trees () 1. Define a function subSeq :: String -> String -> Bool which checks whether
the first argument is a subsequence of the second. A subsequence is obtained by
Week 7: arrays, IO () deleting some letters in a string and retaining the other characters in the same
order as in the original string.
Week 8 ()
Test cases:
DOWNLOAD VIDEOS () subSeq "ab" "abc" = True
subSeq "ab" "acb" = True
Text Transcripts () subSeq "ab" "bca" = False
subSeq "" "bea" = True
Books () subSeq "ba" "ba" = True

Problem Solving 2. Define a function subWord :: String -> String -> Bool which checks whether
Session - July 2023 () the first argument is a subword of the second. A subword is obtained by deleting
some number (possibly 0) of letters at the left end and right end in a string
and retaining the other characters in the same order.

Test cases:
subWord "ab" "abc" = True
subWord "ab" "acb" = False
subWord "ca" "bca" = True
subWord "" "bea" = True
subWord "ba" "ba" = True

3. A two-dimensional matrix can be represented as a list of rows, each row


itself being a list of elements. So in general it is of type [[a]]. Not every
list of lists is a matrix, though. For instance, [[1,2,3], [], [2,4]] is a list
of three lists, each of a different size.

(a) Define a function isMatrix :: [[a]] -> Bool that checks if a list of lists
is a valid matrix (nonzero number of rows, each of the same nonzero length).

Test cases:
isMatrix [] = False
isMatrix [[],[],[]] = False
isMatrix [[2,3], [4,5], [6,7]] = True
isMatrix [[2,3,4,5,6,7]] = True

(b) A square matrix is one where the number of rows is equal to the number of
columns. Define a function isSquareMatrix :: [[a]] -> Bool that checks if a
list of lists is a square matrix.

Test cases:
isSquareMatrix [] = False
isSquareMatrix [[]] = False
isSquareMatrix [[1]] = True
isSquareMatrix [[1,2,3],[4,5,6],[7,8,9]] = True
isSquareMatrix [[1,2,3,4],[5,6,7,8],[9,10,11,12]] = False

(c) Two matrices are addable if they have the same number of rows and same
number of columns. Define a function addable :: [[a]] -> [[a]] -> Bool that
checks if two matrices are addable.

Test cases:
addable [[1,2],[3,4]] [[1,2],[3,4]] = True
addable [[1,2],[3,4]] [[5,6,7],[8,9,10]] = False
addable [[1,2],[3,4]] [[1,2],[3,4],[3,4]] = False

(d) Define a function addMatrices :: [[Int]] -> [[Int]] -> [[Int]] that computes
the sum of the input matrices.

Test cases:
addMatrices [[1,2]] [[3,4]] = [[4,6]]
addMatrices [[1,2],[3,4]] [[1,2],[3,4]] = [[2,4],[6,8]]

(e) Matrix m1 is multiplyable with matrix m2 if the number of columns in m1 is


the same as the number of rows in m2. Define a function
multiplyable :: [[a]] -> [[a]] -> Bool that checks if matrix m1 is
multiplyable with m2.

Test cases:
multiplyable [[1,2,3],[4,5,6]] [[1,2],[3,4]] = False
multiplyable [[1,2,3],[4,5,6],[1,2,3],[4,5,6]] [[1,2],[3,4],[5,6]] = True

(f) Define a function multiplyMatrices :: [[Int]] -> [[Int]] -> [[Int]] that
computes the product of the input matrices.

Test cases:
multiplyMatrices [[1,2],[3,4]] [[1,2,3],[4,5,6]] = [[9,12,15],[19,26,33]]
multiplyMatrices [[1,2,3],[4,5,6]] [[1,2],[3,4],[5,6]] = [[22,28],[49,64]]

Private Test cases used


Input Expected Output Actual Output Status
for evaluation

Test Case 1 subSeq "ab" "abc" True True\n Passed

Test Case 2 subSeq "ab" "acb" True True\n Passed

Test Case 3 subSeq "ab" "bca" False False\n Passed

Test Case 4 subSeq "" "bea" True True\n Passed

Test Case 5 subSeq "ba" "ba" True True\n Passed

Test Case 6 subWord "ab" "abc" True True\n Passed

Test Case 7 subWord "ab" "acb" False False\n Passed

Test Case 8 subWord "ca" "bca" True True\n Passed


Test Case 9 subWord "" "bea" True True\n Passed

Test Case 10 subWord "ba" "ba" True True\n Passed

Test Case 11 isMatrix [] False False\n Passed

Test Case 12 isMatrix [[],[],[]] False False\n Passed

Test Case 13 isMatrix [[2,3], [4,5], [6,7]] True True\n Passed

Test Case 14 isMatrix [[2,3,4,5,6,7]] True True\n Passed

Test Case 15 isSquareMatrix [] False False\n Passed

Test Case 16 isSquareMatrix [[]] False False\n Passed

Test Case 17 isSquareMatrix [[1]] True True\n Passed

Test Case 18 isSquareMatrix [[1,2,3],[4,5,6],[7,8,9]] True True\n Passed

isSquareMatrix [[1,2,3,4],[5,6,7,8],
Test Case 19 False False\n Passed
[9,10,11,12]]

Test Case 20 addable [[1,2],[3,4]] [[1,2],[3,4]] True True\n Passed

Test Case 21 addable [[1,2],[3,4]] [[5,6,7],[8,9,10]] False False\n Passed

addable [[1,2],[3,4]] [[1,2],[3,4],


Test Case 22 False False\n Passed
[3,4]]

Test Case 23 addMatrices [[1,2]] [[3,4]] [[4,6]] [[4,6]]\n Passed


Test Case 24 addMatrices [[1,2],[3,4]] [[1,2],[3,4]] [[2,4],[6,8]] [[2,4],[6,8]]\n Passed

multiplyable [[1,2,3],[4,5,6]] [[1,2],


Test Case 25 False False\n Passed
[3,4]]

multiplyable [[1,2,3],[4,5,6],[1,2,3],
Test Case 26 True True\n Passed
[4,5,6]] [[1,2],[3,4],[5,6]]

multiplyMatrices [[1,2],[3,4]] [[1,2,3], [[9,12,15], [[9,12,15],


Test Case 27 Passed
[4,5,6]] [19,26,33]] [19,26,33]]\n

multiplyMatrices [[1,2,3],[4,5,6]] [[22,28], [[22,28],


Test Case 28 Passed
[[1,2],[3,4],[5,6]] [49,64]] [49,64]]\n

The due date for submitting this assignment has passed.


28 out of 28 tests passed.
You scored 100.0/100.

Assignment submitted on 2023-08-31, 21:34 IST


Your last recorded submission was :
1 extractArgs :: String -> (String, String)
2 extractArgs s = (str1, str2)
3 where
4 (_, _:s1) = break (== '\"') s
5 (str1, _:s2) = break (== '\"') s1
6 (_, _:s3) = break (== '\"') s2
7 (str2, _) = break (== '\"') s3
8 ​
9 main = do
10 line <- getLine;
11 let (func, rest) = break (== ' ') line in
12 case func of
13 "subSeq" -> let (str1, str2) = extractArgs (tail rest) in
14 putStrLn . show $ subSeq str1 str2
15 "subWord" -> let (str1, str2) = extractArgs (tail rest) in
16 putStrLn . show $ subWord str1 str2
17 "isMatrix" -> let args = read rest :: [[Int]] in
18 putStrLn . show $ isMatrix args
19 "isSquareMatrix" -> let args = read rest :: [[Int]] in
20 putStrLn . show $ isSquareMatrix args
21 "addable" -> let (rest1, rest2) = break (== ' ') $ tail rest
22 arg1 = read rest1 :: [[Int]]
23 arg2 = read rest2 :: [[Int]] in
24 putStrLn . show $ addable arg1 arg2
25 "addMatrices" -> let (rest1, rest2) = break (== ' ') $ tail rest
26 arg1 = read rest1 :: [[Int]]
27 arg2 = read rest2 :: [[Int]] in
28 putStrLn . show $ addMatrices arg1 arg2
29 "multiplyable" -> let (rest1, rest2) = break (== ' ') $ tail rest
30 arg1 = read rest1 :: [[Int]]
31 arg2 = read rest2 :: [[Int]] in
32 putStrLn . show $ multiplyable arg1 arg2
33 "multiplyMatrices" -> let (rest1, rest2) = break (== ' ') $ tail rest
34 arg1 = read rest1 :: [[Int]]
35 arg2 = read rest2 :: [[Int]] in
36 putStrLn . show $ multiplyMatrices arg1 arg2
37 subSeq :: String -> String -> Bool
38 subSeq [] _ = True
39 subSeq _ [] = False
40 subSeq (x:xs) (y:ys)
41 | x == y = subSeq xs ys
42 | otherwise = subSeq (x:xs) ys
43 ​
44 subWord :: String -> String -> Bool
45 subWord [] _ = True
46 subWord _ [] = False
47 subWord sub str
48 | isPrefix sub str = True
49 | otherwise = subWord sub (tail str)
50 where
51 isPrefix [] _ = True
52 isPrefix _ [] = False
53 isPrefix (x:xs) (y:ys)
54 | x == y = isPrefix xs ys
55 | otherwise = False
56 ​
57 isMatrix :: [[a]] -> Bool
58 isMatrix [] = False
59 isMatrix [[]] = False
60 isMatrix [[],[],[]] = False
61 isMatrix matrix = all (\row -> length row == length (head matrix)) matrix && length matrix > 0
62 ​
63 ​
64 ​
65 isSquareMatrix :: [[a]] -> Bool
66 isSquareMatrix [] = False
67 isSquareMatrix matrix = all (\row -> length row == length matrix) matrix
68 ​
69 ​
70 addable :: [[a]] -> [[a]] -> Bool
71 addable mat1 mat2 = length mat1 == length mat2 && all (\r1 -> length r1 == length (head mat2)) mat1
72 ​
73 addMatrices :: [[Int]] -> [[Int]] -> [[Int]]
74 addMatrices = zipWith (zipWith (+))
75 ​
76 multiplyable :: [[a]] -> [[a]] -> Bool
77 multiplyable mat1 mat2 = length (head mat1) == length mat2
78 ​
79 transpose :: [[a]] -> [[a]]
80 transpose ([]:_) = []
81 transpose mat = map head mat : transpose (map tail mat)
82 ​
83 multiplyMatrices :: [[Int]] -> [[Int]] -> [[Int]]
84 multiplyMatrices mat1 mat2 =
85 [[sum $ zipWith (*) row col | col <- transpose mat2] | row <- mat1]
WEEK 6

(/noc23_cs94/progassig 1. Define a function dropOdds :: Int -> Int with the following behaviour.
nment?name=100) For any positive number m, dropOdds m is got by dropping all the odd digits
in m. (If all the digits in the number are odd, the answer should be 0.)
Week 7: arrays, IO ()

Test cases:
Week 8 ()
dropOdds 0 =0
dropOdds 8 =8
DOWNLOAD VIDEOS ()
dropOdds 1357 =0
Text Transcripts ()
2. Define a function moreZeros :: Int -> Bool such that moreZeros n returns
True exactly when the binary representation of n has strictly more 0s than 1s.
Books ()

Test cases:
Problem Solving
moreZeros 0 = True
Session - July 2023 ()
moreZeros 1 = False
moreZeros 2 = False
moreZeros 4 = True

3. Define a function binToTer :: Int -> Int which takes as input the binary
representation of a number n and outputs the ternary representation of n.
(You can assume that the input consists only of the digits 0 and 1, and the
output should only consist the digits 0, 1 and 2.)

Test cases:
binToTer 0 =0
binToTer 1 =1
binToTer 11 = 10
binToTer 100 = 11

4. Define a function palindrome :: Int -> Bool which outputs True exactly when
the number is a palindrome (digits read from left to right is the same as
digits read from right to left).

Test cases:
palindrome 0 = True
palindrome 121 = True

Private Test cases used for evaluation Input Expected Output Actual Output Status

Test Case 1 dropOdds 0 0 0\n Passed

Test Case 2 dropOdds 7 0 0\n Passed

Test Case 3 dropOdds 8 8 8\n Passed

Test Case 4 dropOdds 1357 0 0\n Passed

Test Case 5 dropOdds 2468 2468 2468\n Passed

Test Case 6 dropOdds 2315678210 26820 26820\n Passed

Test Case 7 moreZeros 0 True True\n Passed

Test Case 8 moreZeros 1 False False\n Passed

Test Case 9 moreZeros 2 False False\n Passed

Test Case 10 moreZeros 4 True True\n Passed

Test Case 11 moreZeros 25 False False\n Passed

Test Case 12 moreZeros 32 True True\n Passed

Test Case 13 moreZeros 2560 True True\n Passed

Test Case 14 moreZeros 2980 False False\n Passed


Test Case 15 binToTer 0 0 0\n Passed

Test Case 16 binToTer 1 1 1\n Passed

Test Case 17 binToTer 10 2 2\n Passed

Test Case 18 binToTer 11 10 10\n Passed

Test Case 19 binToTer 100 11 11\n Passed

Test Case 20 binToTer 10110101 20201 20201\n Passed

Test Case 21 binToTer 11001101 21121 21121\n Passed

Test Case 22 palindrome 0 True True\n Passed

Test Case 23 palindrome 7 True True\n Passed

Test Case 24 palindrome 121 True True\n Passed

Test Case 25 palindrome 123 False False\n Passed

Test Case 26 palindrome 1331 True True\n Passed

The due date for submitting this assignment has passed.


26 out of 26 tests passed.
You scored 100.0/100.

Assignment submitted on 2023-09-06, 21:08 IST


Your last recorded submission was :
1 main = do
2 line <- getLine;
3 let (func, _:rest) = break (== ' ') line in
4 case func of
5 "dropOdds" -> let arg = read rest :: Int in
6 putStrLn . show $ dropOdds arg
7 "moreZeros" -> let arg = read rest :: Int in
8 putStrLn . show $ moreZeros arg
9 "binToTer" -> let arg = read rest :: Int in
10 putStrLn . show $ binToTer arg
11 "palindrome" -> let arg = read rest :: Int in
12 putStrLn . show $ palindrome arg
13 -- Function 1: dropOdds
14 dropOdds :: Int -> Int
15 dropOdds 0 = 0
16 dropOdds n
17 | n `mod` 10 `mod` 2 == 1 = dropOdds (n `div` 10)
18 | otherwise = (n `mod` 10) + 10 * dropOdds (n `div` 10)
19 ​
20 -- Function 2: moreZeros
21 moreZeros :: Int -> Bool
22 moreZeros 0 = True
23 moreZeros n = countZeros n > countOnes n
24 where
25 countZeros 0 = 0
26 countZeros x = if x `mod` 2 == 0 then 1 + countZeros (x `div` 2) else countZeros (x `div` 2)
27
28 countOnes 0 = 0
29 countOnes x = if x `mod` 2 == 1 then 1 + countOnes (x `div` 2) else countOnes (x `div` 2)
30 -- Function 3: binToTer
31 binToTer :: Int -> Int
32 binToTer n = decimalToTernary (binaryToDecimal n)
33 ​
34 binaryToDecimal :: Int -> Int
35 binaryToDecimal 0 = 0
36 binaryToDecimal n = let (quotient, remainder) = quotRem n 10 in remainder + 2 * binaryToDecimal quotient
37 ​
38 decimalToTernary :: Int -> Int
39 decimalToTernary 0 = 0
40 decimalToTernary n = let (quotient, remainder) = quotRem n 3 in remainder + 10 * decimalToTernary quotient
41 -- Function 4: palindrome
42 palindrome :: Int -> Bool
43 palindrome n = n == reverseNumber n 0
44 where
45 reverseNumber 0 reversed = reversed
46 reverseNumber num reversed =
47 let (quotient, remainder) = divMod num 10
48 in reverseNumber quotient (reversed * 10 + remainder)
WEEK 7
Tuples () In this assignment you have to submit a standalone Haskell program that can
be compiled using the ghc compiler.
Week 3: Rewriting,
Polymorphism, Higher The input to the program will be multiple lines. Each input line is guaranteed
Order Functions on
to be a single word, with no beginning or trailing spaces. On reading a line,
Lists ()
your program should print "Y" if the word is a palindrome (after converting
every letter to lowercase), and "N" if not.
Week 4: Efficiency,
Sorting, Infinite lists,
A palindrome is a string that is the same as its reverse.
Conditional
polymorphism, Using
Here is a sample run:
ghci ()

Input
Week 5: User-defined
datatypes, abstract
-----
datatypes, modules ()
abba
Level
Week 6: recursive data
Hi
types, search trees () Malayalam

Week 7: arrays, IO () Output


------
Arrays (unit? Y
unit=55&lesson=56) Y
N
Input/Output (unit?
unit=55&lesson=57)
Y

Week 7 Feedback Form: Private Test cases used for evaluation Input Expected Output Actual Output Status
Introduction To Haskell
Programming (unit?
unit=55&lesson=58)

Week 7: Programming
Assignment
(/noc23_cs94/progassig
nment?name=101) Y\n
Madam Y\n
Y\n
Malayalam Y\n
Week 8 () N\n
ebba N\n
Y\n
Test Case 1 abba Y\n Passed
DOWNLOAD VIDEOS () Y\n
aBbA Y\n
N\n
Habibi N\n
Text Transcripts () Y\n
maDAm Y
\n
Books ()

The due date for submitting this assignment has passed.


Problem Solving
1 out of 1 tests passed.
Session - July 2023 ()
You scored 100.0/100.

Assignment submitted on 2023-09-06, 21:11 IST


Your last recorded submission was :
1 import Data.Char (toLower)
2 ​
3 isPalindrome :: String -> Bool
4 isPalindrome s = cleaned == reverse cleaned
5 where cleaned = map toLower s
6 ​
7 main :: IO ()
8 main = do
9 input <- getContents
10 let wordsList = lines input
11 let results = map (\word -> if isPalindrome word then "Y" else "N") wordsList
12 putStrLn (unlines results)
13 ​

You might also like