About
rotabc :: String -> String rotabc = map rotate where rotate 'a' = 'b' rotate 'b' = 'c' rotate 'c' = 'a' rotate c = c
quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = let smallerSorted = quicksort [a | a <- xs, a <= x] biggerSorted = quicksort [a | a <- xs, a > x] in smallerSorted ++ [x] ++ biggerSorted
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