About
Find the biggest of two numbers. mx :: (Int, Int)->Int mx (x,y) = max x y main = do putStrLn "Enter number 1" x <- getLine
- let a = read x :: Int
- putStrLn ("Enter number 2") y <- getLine let b = read y :: Int let c=mx(a,b)
- putStrLn $ "Max:" ++ show c
- Check if a number is even or odd. eve :: Int -> Int eve x = if mod x 2 == 0 then 0 else 7 main=do x <- getLine let b = read x :: Int let a =eve(b) if a == 0 then print("Hi even") else print("Odd") Check in list: main :: IO () main = do let myList = [1, 2, 3, 4, 5] let x = 3 if elem x myList then putStrLn "x is in the list" else putStrLn "x is not in the list"\ Uppercase import Data.Char (toLower, toUpper) changeCase :: Char -> Char changeCase c = if isUpper c then toLower c else toUpper c main :: IO () main = do let input = 'a' let result = changeCase input putStrLn $ "Input: " ++ [input] ++ ", Result: " ++ [result]
let myList = [1, 2, 3, 4, 5] let a =myList ++ [98] print a let myList = ['A'..'Z'] print myList let myList = [1 .. 200] print myList Vowels: hk :: Char -> IO() vow=['a','e','i','o','u'] num=['1','2','3','4'] hk a = if elem a vow then putStrLn $ "Vowel" ++ show a else if elem a num then putStrLn "Number" else putStrLn "Other" main=do y <- getChar hk(y) Switch: intToString :: Int -> String intToString n = case n of 0 -> "zero" 1 -> "one" 2 -> "two" 3 -> "three" _ -> "greater than three" main :: IO () main = do y <- getLine let a = read y :: Int putStrLn $ intToString a -- "two" putStrLn $ intToString 5 -- "greater than three" Double a list using map: doubleList :: [Int] -> [Int] doubleList x = map (2) x main :: IO () main = do let xs = [1, 2, 3, 4, 5] let doubled = doubleList xs print doubled -- [2, 4, 6, 8, 10] Print odd: printEven :: [Int] -> IO () printEven xs = mapM_ print (filter odd xs) main :: IO () main = do let xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] printEven xs Len list using recursion: myList = [1, 2, 3, 4, 5] listLength = length myList listLength :: [a] -> Int listLength [] = 0 listLength (_:xs) = 1 + listLength xs List fns: list = [1,2,3,4,5] main = do print(list !! 0) print(head list) print(last list) print(tail list) print(init list) print(length list) print (null hk) let hk = [True,False,True] print (and hk) print(or hk) Create list using recursion: asc :: Int -> Int -> [Int] asc n m |m < n =[] |m == n=[m] |m > n = n : asc (n+1) m main = do print(asc 1 3) Add tuples; addTuples :: [(Int, Int)] -> [Int] main :: IO () addTuples xs = [ x+y | (x,y) <- xs ] main=do let a = addTuples [(1,2), (2,3), (100,100)] print(a) List: let y =[x2 | x <- [1..10], x2 >= 12] print let y =[x2 | x <- [1..10], x2 >= 12] print Concat list: main = do let hk=["harish","kumar","4845"] let a =concat hk print a Year to days str2Int::String->Int str2Int x = read x :: Int year::(Int)->Int year(x) = (div x 365) weeks::(Int,Int)->Int weeks (x,y) = (div (x-(y365)) 52) main = do putStrLn ("Enter number of days:") input1 <- getLine let num1 = str2Int input1 let a=year(num1) let b=weeks(num1,a) let c=(num1-(a365))-(b52) putStr $ "Years: " ++ show a putStr $ " Weeks " ++ show b putStr $ " Days: " ++ show c putStrLn "Days" Concat List user defined fn: concatLists :: [a] -> [a] -> [a] concatLists [] ys = ys concatLists (x:xs) ys = x : concatLists xs ys main :: IO () main = do let xs = [1, 2, 3] let ys = [4, 5, 6] putStrLn $ "Concatenation of lists " ++ show xs ++ " and " ++ show ys ++ " is " ++ show (concatLists xs ys) or myconcat xss = [x | xs <- xss, x <- xs] myconcat [['a','b','c'],['d','e','f']] Print odd elements in list: main :: IO () myfilter p xs = [x | x <- xs, p x] main = do let a= myfilter odd [1,2,3,4,5] print a Sort using built in fn: import Data.List (sort) main :: IO () main = do let xs = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] putStrLn $ "Sorted list is " ++ show (sort xs) Area of rectangle: rectangleArea :: (Double, Double) -> Double rectangleArea (width, height) = width height main :: IO () main = do let width = 5.0 height = 3.0 area = rectangleArea (width, height) putStrLn $ "The area of a rectangle with width " ++ show width ++ " and height " ++ show height ++ " is " ++ show area Product: productList :: [Int] -> Int productList [] = 1 -- base case: empty list has product 1 productList (x:xs) = x productList xs -- recursive case: multiply head by product of tail main :: IO () main = do let xs = [1, 2, 3, 4, 5] putStrLn $ "The product of the list " ++ show xs ++ " is " ++ show (productList xs) Duplicate in list: import Data.List (nub) findDuplicates :: Eq a => [a] -> [a] findDuplicates xs = xs \ nub xs -- subtract the unique elements from the original list to get the duplicates main :: IO () main = do let xs = [1, 2, 2, 3, 4, 4, 4, 5] duplicates = findDuplicates xs putStrLn $ "The list " ++ show xs ++ " has the following duplicate elements: " ++ show duplicates Traverse: Recursion traverseList :: [a] -> IO () traverseList [] = return () -- base case: empty list traverseList (x:xs) = do putStrLn $ "Current element: " ++ show x -- do something with current element traverseList xs -- traverse the rest of the list or traverseList :: [a] -> IO () traverseList xs = mapM_ (\x -> putStrLn $ "Current element: " ++ show x) xs Reverse: reverseList :: [a] -> [a] reverseList xs = reverse xs findOddOccurrence :: Eq a => [a] -> a findOddOccurrence xs = head $ filter (\x -> odd $ length $ filter (==x) xs) $ map head $ group $ sort xs Let's break down this implementation step by step: • sort xs sorts the input list in ascending order. • group $ sort xs groups consecutive equal elements in the sorted list into sublists. • map head $ group $ sort xs extracts the first element from each sublist, giving us a list of unique elements from the input list. • filter (\x -> odd $ length $ filter (==x) xs) $ map head $ group $ sort xs filters the list of unique elements to include only those that occur an odd number of times in the input list. This is done by filtering each element of the list and checking if the length of the filtered list is odd. • head $ filter (\x -> odd $ length $ filter (==x) xs) $ map head $ group $ sort xs returns the first element of the filtered list, which is the number occurring an odd number of times in the input list. Here's an example usage: haskellCopy code main :: IO () main = do let xs = [1, 2, 3, 2, 1, 3, 4] putStrLn $ "Number occurring an odd number of times in list " ++ show xs ++ " is " ++ show (findOddOccurrence xs) Divisors: divisors :: Int -> [Int] divisors n = [x | x <- [1..n], mod n x == 0] main :: IO () main = do let n = 24 divs = divisors n putStrLn $ "The divisors of " ++ show n ++ " are " ++ show div
rotabc :: String -> String rotabc = map rotate where rotate 'a' = 'b' rotate 'b' = 'c' rotate 'c' = 'a' rotate c = c
import Data.Char (isDigit)
separate :: String -> (String, String) separate s = separate' s ("", "")
separate' :: String -> (String, String) -> (String, String) separate' [] acc = acc separate' (x:xs) (digits, nonDigits) | isDigit x = separate' xs (digits ++ [x], nonDigits) | otherwise = separate' xs (digits, nonDigits ++ [x])
filterEven :: [Int] -> [Int] filterEven = filter even
applyNTimes :: Int -> (a -> a) -> a -> a applyNTimes 0 _ x = x applyNTimes n f x = f (applyNTimes (n-1) f x)
compose :: (b -> c) -> (a -> b) -> (a -> c) compose f g = \x -> f (g x)
redact :: String -> String redact s = map (\c -> if c == ' ' then ' ' else '*') s
transform :: [Float] -> [Float] transform values = map (sqrt . abs) values
compose f g x = f (g x) result = compose (+1) (*2) 3
doubleAll = map (2) tripleAll = map (3) result = doubleAll (tripleAll [1,2,3])
applyTwice f x = f (f x) result = applyTwice (+1) 2
foldr1Safe f [] = Nothing foldr1Safe f xs = Just (foldr1 f xs)
sumSafe = foldr1Safe (+) result = sumSafe [1,2,3,4]
filterByLength n = filter (\x -> length x == n) result = filterByLength 4 ["cat", "dog", "bird", "turtle"]
concatLists [] ys = ys concatLists (x:xs) ys = x : concatLists xs ys main = do let lst1 = [4, 8, 1] let lst2 = [5, 9, 2] putStrLn $ show (concatLists lst1 lst2)
toCatYears n | n == 0 = 0 | n < 0 = -1 catYears | n == 1 = 15 | n == 2 = 24 | otherwise = 24 + 4 (n - 2) where catYears = toCatYears (abs n)
main = do putStrLn "Enter the number of calendar years:" input <- getLine let years = read input :: Float let catYears = toCatYears years putStrLn $ "The number of cat years is: " ++ show catYears
join :: [String] -> String join [] = "" join [x] = x join (x:xs) = x ++ " " ++ join xs
main :: IO () main = do let strings = ["This", "is", "Jai", "from", "CSE-A"] putStrLn $ join strings
reach :: [Int] -> Int reach xs = maximum xs - minimum xs
main :: IO () main = do putStrLn "Enter a list of numbers separated by spaces:" input <- getLine let numbers = map read $ words input :: [Int] let result = reach numbers putStrLn $ "The distance between the least and greatest values is " ++ show result
middle3 :: [a] -> [a]
middle3 [] = []
middle3 [x] = [x]
middle3 [x, y] = [x, y]
middle3 xs = take 3 (drop middleIndex xs)
where middleIndex = (length xs - 1) div
2
main = do
let lst1 = [5,4,2,3,5,7,1,2,6]
let lst2 = [1, 2, 3, 4, 5, 6, 7]
putStrLn $ show (middle3 lst1)
putStrLn $ show (middle3 lst2)
import Data.List (nub, (\))
findDuplicates :: Eq a => [a] -> [a] findDuplicates xs = xs \ nub xs
main :: IO () main = do putStrLn "Enter a list of elements separated by spaces:" input <- getLine let list = words input duplicates = findDuplicates list if null duplicates then putStrLn "There are no duplicates in the list." else putStrLn ("Duplicates in the list: " ++ unwords duplicates)
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n mod
x == 0]
main = do input <- readLn :: IO Int let a = factors input print (length a)
myLength :: [a] -> Int myLength [] = 0 myLength (x:xs) = 1 + myLength xs main = do let lst = [3, 7, 2, 9] putStrLn $ show (myLength lst)
import Data.List (group)
findOddOccurrence :: Eq a => [a] -> a findOddOccurrence xs = head [x | x <- xs, odd (length (filter (==x) xs))]
main :: IO () main = do putStrLn "Enter a list of elements separated by spaces:" input <- getLine let list = words input result = findOddOccurrence list putStrLn ("The number occurring an odd number of times is: " ++ result)
sumEven :: [Int] -> Int sumEven xs = sum [x | x <- xs, even x]
main :: IO () main = do putStrLn "Enter a list of integers:" input <- getLine let xs = map read (words input) let result = sumEven xs putStrLn ("The sum of even numbers in the list is " ++ show result)
printOddNumbers :: [Int] -> IO () printOddNumbers [] = return () printOddNumbers (x:xs) = do if odd x then putStrLn (show x) else return () printOddNumbers xs
main :: IO () main = do putStrLn "Enter a list of integers separated by spaces:" input <- getLine let list = map read (words input) putStrLn "Odd numbers in the list:" printOddNumbers list
productList :: [Int] -> Int productList [] = 1 productList (x:xs) = x * productList xs
main :: IO () main = do putStrLn "Enter a list of integers separated by spaces:" input <- getLine let list = map read (words input) putStrLn ("Product of the list: " ++ show (productList list))
runningTotal :: [Int] -> [Int] runningTotal [] = [] runningTotal xs = scanl1 (+) xs
main :: IO () main = do let nums = [1, 2, 3, 4, 5] putStrLn $ "The running total of " ++ show nums ++ " is " ++ show (runningTotal nums)
searchList xs x
| x elem
xs = "The value " ++ show x ++ " is in the list."
| otherwise = "The value " ++ show x ++ " is not in the list."
main = do putStrLn "Enter a list of elements separated by spaces:" input <- getLine let list = words input putStrLn "Enter a value to search for:" value <- getLine putStrLn (searchList list value)
findSmallest :: [Int] -> Int findSmallest [] = error "empty list" findSmallest [x] = x findSmallest (x:xs) = min x (findSmallest xs)
main :: IO () main = do putStrLn "Enter a list of integers:" input <- getLine let xs = map read (words input) let result = findSmallest xs putStrLn ("The smallest element in the list is " ++ show result)
startEnd :: [a] -> (a, a) startEnd [] = error "List is empty." startEnd [x] = (x, x) startEnd (x:xs) = (x, last xs) main = do let lst = [1, 2, 3, 4, 5] let (start, end) = startEnd lst putStrLn $ "Start: " ++ show start ++ ", End: " ++ show end
sumsquare1 n= (sum([1..n]))^2 -- squaresum1 n=(sum[xx | x <- [1..n]]) -- squaresum1 n=(sum[1,4..nn])
main=do putStrLn("Enter a number for limit:") input1<-getLine let a=read input1 :: Int sumofsquare1=sumsquare1 a squareofsum1=squaresum1 a difference1=sumofsquare1-squareofsum1 putStrLn("Differenece is: "++show(difference1)) -}
import Data.List (find)
isPythagorean (a, b, c) = a^2 + b^2 == c^2
sumEquals n (a, b, c) = a + b + c == n
findProduct n = case find isPythagorean [(a,b,c) | c <- [1..n], b <- [1..c], a <- [1..b], a + b + c == n] of Nothing -> Nothing Just (a, b, c) -> Just (a b c)
main = case findProduct 1000 of Nothing -> putStrLn "No triplet found" Just product -> putStrLn ("Product: " ++ show product)
concatLists [] ys = ys concatLists (x:xs) ys = x : concatLists xs ys main = do let lst1 = [4, 8, 1] let lst2 = [5, 9, 2] putStrLn $ show (concatLists lst1 lst2)
toCatYears n | n == 0 = 0 | n < 0 = -1 catYears | n == 1 = 15 | n == 2 = 24 | otherwise = 24 + 4 (n - 2) where catYears = toCatYears (abs n)
main = do putStrLn "Enter the number of calendar years:" input <- getLine let years = read input :: Float let catYears = toCatYears years putStrLn $ "The number of cat years is: " ++ show catYears
join :: [String] -> String join [] = "" join [x] = x join (x:xs) = x ++ " " ++ join xs
main :: IO () main = do let strings = ["This", "is", "Jai", "from", "CSE-A"] putStrLn $ join strings
reach :: [Int] -> Int reach xs = maximum xs - minimum xs
main :: IO () main = do putStrLn "Enter a list of numbers separated by spaces:" input <- getLine let numbers = map read $ words input :: [Int] let result = reach numbers putStrLn $ "The distance between the least and greatest values is " ++ show result
middle3 :: [a] -> [a]
middle3 [] = []
middle3 [x] = [x]
middle3 [x, y] = [x, y]
middle3 xs = take 3 (drop middleIndex xs)
where middleIndex = (length xs - 1) div
2
main = do
let lst1 = [5,4,2,3,5,7,1,2,6]
let lst2 = [1, 2, 3, 4, 5, 6, 7]
putStrLn $ show (middle3 lst1)
putStrLn $ show (middle3 lst2)
import Data.List (nub, (\))
findDuplicates :: Eq a => [a] -> [a] findDuplicates xs = xs \ nub xs
main :: IO () main = do putStrLn "Enter a list of elements separated by spaces:" input <- getLine let list = words input duplicates = findDuplicates list if null duplicates then putStrLn "There are no duplicates in the list." else putStrLn ("Duplicates in the list: " ++ unwords duplicates)
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n mod
x == 0]
main = do input <- readLn :: IO Int let a = factors input print (length a)
myLength :: [a] -> Int myLength [] = 0 myLength (x:xs) = 1 + myLength xs main = do let lst = [3, 7, 2, 9] putStrLn $ show (myLength lst)
import Data.List (group)
findOddOccurrence :: Eq a => [a] -> a findOddOccurrence xs = head [x | x <- xs, odd (length (filter (==x) xs))]
main :: IO () main = do putStrLn "Enter a list of elements separated by spaces:" input <- getLine let list = words input result = findOddOccurrence list putStrLn ("The number occurring an odd number of times is: " ++ result)
sumEven :: [Int] -> Int sumEven xs = sum [x | x <- xs, even x]
main :: IO () main = do putStrLn "Enter a list of integers:" input <- getLine let xs = map read (words input) let result = sumEven xs putStrLn ("The sum of even numbers in the list is " ++ show result)
printOddNumbers :: [Int] -> IO () printOddNumbers [] = return () printOddNumbers (x:xs) = do if odd x then putStrLn (show x) else return () printOddNumbers xs
main :: IO () main = do putStrLn "Enter a list of integers separated by spaces:" input <- getLine let list = map read (words input) putStrLn "Odd numbers in the list:" printOddNumbers list
productList :: [Int] -> Int productList [] = 1 productList (x:xs) = x * productList xs
main :: IO () main = do putStrLn "Enter a list of integers separated by spaces:" input <- getLine let list = map read (words input) putStrLn ("Product of the list: " ++ show (productList list))
runningTotal :: [Int] -> [Int] runningTotal [] = [] runningTotal xs = scanl1 (+) xs
main :: IO () main = do let nums = [1, 2, 3, 4, 5] putStrLn $ "The running total of " ++ show nums ++ " is " ++ show (runningTotal nums)
searchList xs x
| x elem
xs = "The value " ++ show x ++ " is in the list."
| otherwise = "The value " ++ show x ++ " is not in the list."
main = do putStrLn "Enter a list of elements separated by spaces:" input <- getLine let list = words input putStrLn "Enter a value to search for:" value <- getLine putStrLn (searchList list value)
findSmallest :: [Int] -> Int findSmallest [] = error "empty list" findSmallest [x] = x findSmallest (x:xs) = min x (findSmallest xs)
main :: IO () main = do putStrLn "Enter a list of integers:" input <- getLine let xs = map read (words input) let result = findSmallest xs putStrLn ("The smallest element in the list is " ++ show result)
startEnd :: [a] -> (a, a) startEnd [] = error "List is empty." startEnd [x] = (x, x) startEnd (x:xs) = (x, last xs) main = do let lst = [1, 2, 3, 4, 5] let (start, end) = startEnd lst putStrLn $ "Start: " ++ show start ++ ", End: " ++ show end
concatLists :: [a] -> [a] -> [a] concatLists [] ys = ys concatLists (x:xs) ys = x : concatLists xs ys main = do let lst1 = [4, 8, 1] let lst2 = [5, 9, 2] putStrLn $ show (concatLists lst1 lst2)
searchList :: (Eq a, Show a) => [a] -> a -> String
searchList xs x
| x elem
xs = "The value " ++ show x ++ " is in the list."
| otherwise = "The value " ++ show x ++ " is not in the list."
main :: IO () main = do putStrLn "Enter a list of elements separated by spaces:" input <- getLine let list = words input putStrLn "Enter a value to search for:" value <- getLine putStrLn (searchList list value)