{- This file contains the datatypes for Op and Expr. As well as the various methods for their display and usage. -} -- Arithmetic operators data Op = Add | Sub | Mul | Div instance Show Op where show Add = "+" show Sub = "-" show Mul = "*" show Div = "/" -- A function to check if the specified operation is valid valid :: Op -> Int -> Int -> Bool valid Add _ _ = True valid Sub x y = x > y valid Mul _ _ = True valid Div x y = x `mod` y == 0 -- A function to perform the specified operation between two ints apply :: Op -> Int -> Int -> Int apply Add x y = x + y apply Sub x y = x - y apply Mul x y = x * y apply Div x y = x `div` y -- Numeric expressions data Expr = Val Int | App Op Expr Expr -- brak is an inline function definition that allows us to construct strings and properly place parenthesis around mathematical terms instance Show Expr where show (Val n) = show n show (App o l r) = brak l ++ show o ++ brak r where brak (Val n) = show n brak e = "(" ++ show e ++ ")" -- Creates an Int array containing only values found inside the Expr, none of the operations values :: Expr -> [Int] values (Val n) = [n] values (App _ l r) = values l ++ values r -- Evaluates an Expr down to a single item Int array eval :: Expr -> [Int] eval (Val n) = [n | n > 0] eval (App o l r) = [apply o x y | x <- eval l, y <- eval r, valid o x y] -- Questions 1 and 2 of In Class Practive reverser :: Expr -> Expr reverser (App Add i r) = App Sub (reverser i) (reverser r) reverser (App Sub i r) = App Add (reverser i) (reverser r) reverser (App Mul i r) = App Div (reverser i) (reverser r) reverser (App Div i r) = App Mul (reverser i) (reverser r) minMaxVal :: [Expr] -> [Int] minMaxVal (xs) = [minElement, maxElement] where maxElement = maximum (valueArray(xs)) minElement = minimum (ValueArray(xs)) valueArray :: [Expr] -> [Int] valueArray [] = [] valueArray (p:xs) = element: (valueArray(xs)) where element = head(eval p) x :: Expr x = App Div (App Mul (Val 3) (Val 7)) (App Add (App Sub (Val 5) (Val 4)) (App Add (Val 2) (Val 4))) y :: Expr y = App Mul (App Add (Val 4) (Val 10)) (App Sub (App Div (Val 14) (Val 2)) (App Mul (Val 2) (Val 2))) z :: Expr z = App Sub (App Div (App Mul (Val 6) (App Add (Val 5) (App Sub (Val 9) (Val 4)))) (App Add (Val 3) (Val 2))) (App Mul (Val 3) (App Sub (App Div (Val 18) (Val 9)) (Val 1))) main = do print x print y print z print (head (eval x)) print (head (eval y)) print (head (eval z)) print (values x) print (values y) print (values z)
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