--- Lists and recursion import Data.Char -- Integral data type is a typeclass whose instances support integer division and conversion to the Integer type -- functions even and odd require an Integral data type -- Returns a list with the digits from the input list digitsOfList :: String -> String digitsOfList [] = [] digitsOfList (x : xs) | isDigit x = x : digitsOfList xs | otherwise = digitsOfList xs -- Returns the min value of a list of integer numbers minOfList :: [Int] -> Int minOfList (x : []) = x minOfList (x : xs) = x `min` minOfList xs -- Returns the max value of a list of integer numbers maxOfList :: [Int] -> Int maxOfList (x : []) = x maxOfList (x : xs) = x `max` maxOfList xs -- Returns a list having n times the element x addToList :: Int -> a -> [a] addToList 0 x = [] addToList n x = x : addToList(n - 1) x -- Returns the square of the elements of a list of numbers numToSquare :: Num a => [a] -> [a] numToSquare [] = [] numToSquare (x : xs) = x*x : numToSquare xs -- multiplies element by itself then calls the recursive function -- Returns the square of the elements of a list of numbers using map numToSquareUsingMap :: Num a => [a] -> [a] -- example of a higher function?? numToSquareUsingMap x = map (^2) x -- Sum the numbers of a list sumOfList :: Num a => [a] -> a sumOfList [] = 0 sumOfList (x : xs) = x + sumOfList xs -- Multiply the numbers of a list multOfList :: Num a => [a] -> a multOfList [] = 1 -- If base case list is empty -> product is 1. Otherwise, product -> first element of list, multiplied by product of remaining elements multOfList (x : xs) = x * multOfList xs -- Main program main :: IO () main = do let vowels = ["a","e","i","o","u"] let numbers = [1,2,3,4,5,6,7,8,9,10] let empty = [] putStr "Min of numbers " print (minOfList numbers) putStr "Max of numbers " print (maxOfList numbers) -- list containing letters and digits let lettersDigits = "1a2e3i4o5u" putStr "LettersDigits " print (lettersDigits) putStr "Digits " print (digitsOfList lettersDigits) print (sumOfList numbers) print (multOfList empty)
Write, Run & Share Haskell code online using OneCompiler's Haskell online compiler for free. It's one of the robust, feature-rich online compilers for Haskell language, running the latest Haskell version 8.6. Getting started with the OneCompiler's Haskell editor is easy and fast. The editor shows sample boilerplate code when you choose language as Haskell and start coding.
OneCompiler's Haskell online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Haskell program which takes name as input and prints hello message with your name.
main = do
name <- getLine
putStrLn ("Hello " ++ name ++ ", Happy learning!")
Haskell is purely a functional programming language which was introduced in 1990's.
Data-type | Description |
---|---|
Numbers | Haskell is intelligent to identify numbers without specifying data type |
Characters | Haskell is intelligent to identify characters and strings without specifying data type |
Tuple | To declare multiple values in a single data type. Tuples are represented in single paranthesis. For example (10, 20, 'apple') |
Boolean | To represent boolean values, true or false |
List | To declare same type of values in a single data type. Lists are represented in square braces.For example [1, 2, 3] or `['a','b','c','d'] |
When ever you want to perform a set of operations based on a condition or set of conditions, then If-Else/ Nested-If-Else are used.
main = do
let age = 21
if age > 18
then putStrLn "Adult"
else putStrLn "child"
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Functions play an important role in Haskell, since it is a purely functional language.
multiply :: Integer -> Integer -> Integer --declaration of a function
multiply x1 x2 = x1 * x2 --definition of a function
main = do
putStrLn "Multiplication value is:"
print(multiply 10 5) --calling a function