Professional Documents
Culture Documents
Haskell
Haskell
1.
halves :: [Int] -> [Int]
halves [] = [] -- Base case: an empty list results in an empty list
halves (x:xs) = (x `div` 2) : halves xs
main :: IO ()
main = do
let originalList = [1, 2, 3, 4, 5]
let result = halves originalList
print result
2.
stack :: [a] -> [a]
stack [] = [] -- If the list is empty, return an empty list
stack (x:xs) = xs ++ [x] -- Move the first element to the back
main :: IO ()
main = do
let originalList = [1, 2, 3, 4, 5]
let result = stack originalList
print result
3.
fibonacci :: Int -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
main :: IO ()
main = do
let n = 10 -- Change n to compute a different Fibonacci number
let result = fibonacci n
putStrLn $ "The " ++ show n ++ "th Fibonacci number is: " ++ show result
4.
factors :: Int -> [Int]
factors n = [x | x <- [2..n-1], n `mod` x == 0]
main :: IO ()
main = do
let n = 12 -- Change n to find factors of a different integer
let result = factors n
putStrLn $ "Factors of " ++ show n ++ ": " ++ show result
5.
pivot :: Ord a => a -> [a] -> ([a], [a])
pivot _ [] = ([], []) -- Base case: empty list
pivot pivotValue (x:xs)
| x <= pivotValue = (x : left, right)
| otherwise = (left, x : right)
where
(left, right) = pivot pivotValue xs
main :: IO ()
main = do
let pivotValue = 3
let inputList = [5, 6, 4, 2, 1, 3]
let (left, right) = pivot pivotValue inputList
putStrLn $ "Elements less than or equal to " ++ show pivotValue ++ ": " ++ show left
putStrLn $ "Elements greater than " ++ show pivotValue ++ ": " ++ show right
6.
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
main :: IO ()
main = do
let tree = Node 1
(Node 2
(Node 4 EmptyTree EmptyTree)
EmptyTree)
(Node 3 EmptyTree EmptyTree)
let height = treeHeight tree
putStrLn $ "Height of the tree: " ++ show height
8.
merge :: Ord a => [a] -> [a] -> [a]
merge [] ys = ys -- If the first list is empty, the result is the second list.
merge xs [] = xs -- If the second list is empty, the result is the first list.
merge (x:xs) (y:ys)
| x <= y = x : merge xs (y:ys) -- If x is smaller or equal, include x in the result.
| otherwise = y : merge (x:xs) ys -- If y is smaller, include y in the result.
main :: IO ()
main = do
let list1 = [1, 3, 5, 7]
let list2 = [2, 4, 6, 8]
let mergedList = merge list1 list2
putStrLn $ "Merged List: " ++ show mergedList
9.
mergeSort :: Ord a => [a] -> [a]
mergeSort [] = [] -- Base case: empty list is already sorted
mergeSort [x] = [x] -- Base case: singleton list is already sorted
mergeSort xs = mergeSortedLists (mergeSort leftHalf) (mergeSort rightHalf)
where
(leftHalf, rightHalf) = splitAt (length xs `div` 2) xs
main :: IO ()
main = do
let myList = [8, 4, 2, 7, 30, 5, 299, 3, 6]
let sortedList = mergeSort myList
putStrLn $ "Sorted List: " ++ show sortedList
12.
main :: IO ()
main = do
let numbers = [1..100]
mapM_ (printNumber) numbers
main :: IO ()
main = do
let inputList = [1, 2, 3, 4, 5, 6, 7]
let k = 2
let rotatedList = rotateList k inputList
putStrLn $ "Input List: " ++ show inputList
putStrLn $ "Rotated List: " ++ show rotatedList
17.
-- Calculate the binomial coefficient (n choose k)
binomial :: Int -> Int -> Int
binomial n k
| k == 0 || k == n = 1
| otherwise = binomial (n - 1) (k - 1) + binomial (n - 1) k
main :: IO ()
main = do
let numRows = 5
let triangle = pascalsTriangle numRows
printPascalsTriangle triangle
18.
main :: IO ()
main = do
let inputList = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
let maxSum = maxSubArraySum inputList
putStrLn $ "Maximum sum of a subarray: " ++ show maxSum