About
quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (x:xs) = quicksort smaller ++ [x] ++ quicksort larger where smaller = filter (<x) xs larger = filter (>=x) xs
main :: IO () main = do putStrLn "Enter a list of numbers separated by spaces:" input <- getLine let nums = map read $ words input :: [Int] putStrLn $ "Sorted list: " ++ show (quicksort nums)
fold: main :: IO () main = do putStrLn "Enter a list of integers:" input <- getLine let nums = map read $ words input :: [Int] putStrLn $ "Sum using foldr: " ++ show (foldr (+) 0 nums) putStrLn $ "Sum using foldl: " ++ show (foldl (+) 0 nums)
lists: main = do putStrLn "Enter a list of numbers (separated by spaces): " input <- getLine let numbers = map read $ words input :: [Int] putStrLn $ "The sum of the numbers is: " ++ show (sum numbers)
functions: square :: Int -> Int square x = x * x
main = do putStrLn "Enter a number to square: " input <- getLine let number = read input :: Int putStrLn $ "The square of the number is: " ++ show (square number)
higher order: applyTwice :: (a -> a) -> a -> a applyTwice f x = f (f x)
main = do putStrLn "Enter a number to double twice: " input <- getLine let number = read input :: Int putStrLn $ "The result is: " ++ show (applyTwice (*2) number)
pattern mach endsWithA :: String -> Bool endsWithA "" = False endsWithA (x:xs) = if x == 'a' then True else endsWithA xs
main = do putStrLn "Enter a word to check if it ends with 'a': " word <- getLine putStrLn $ if endsWithA word then "The word ends with 'a'." else "The word does not end with 'a'."
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
quicksort :: Ord a => [a] -> [a] quicksort [] = [] quicksort (x:xs) = quicksort smaller ++ [x] ++ quicksort larger where smaller = filter (<x) xs larger = filter (>=x) xs
quick sort main :: IO () main = do putStrLn "Enter a list of numbers separated by spaces:" input <- getLine let nums = map read $ words input :: [Int] putStrLn $ "Sorted list: " ++ show (quicksort nums)
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