import Data.Char import Debug.Trace main = do putStrLn(msg) where msg = show score --TESTING CODE: DO NOT CHANGE score = scConvert + scVNString + scInt2Base + scNum2Char + scDig2Int + scIndexInto where scIndexInto = test2 "test: indexInto " indexInto [0,5,10] [[0..10],[0..10],[0..10]] [0,5,10] 10 scDig2Int = test1 "test: dig2Int " dig2Int ['0','9','A','Z'] [0,9,10,35] 10 scNum2Char = test1 "test: num2char " num2char [0,9,10,35] ['0','9','A','Z'] 10 scInt2Base = test2 "test: int2Base " int2Base [10,10,10,34,34,71,115] [2,16,30,35,34,35,11] ["1010","A","A","Y","10","21","A5"] 30 scVNString = test2 "test: valNumString " valNumString ["1010","A","A","Y","10","21","A5"] [2,16,30,35,34,35,11] [10,10,10,34,34,71,115] 30 scConvert = test3 "test: convert " convert ["1010","1010","1010","1111","1111","13"] [2,2,2,2,2,12] [10,16,33,10,12,2] ["10","A","A","15","13","1111"] 10 test1 msg f [] [] value = trace (msg++" passed!") value test1 msg f (arg:args) (expected:expecteds) value | result == expected = test1 msg f args expecteds value | otherwise = trace (msg ++ "on input "++ show arg ++ " returned " ++ show result ++ ", expected " ++ show expected ) 0 where result = f arg test2 msg f [] [] [] value = trace (msg++" passed!") value test2 msg f (arg1:args1) (arg2:args2) (expected:expecteds) value | result == expected = test2 msg f args1 args2 expecteds value | otherwise = trace (msg ++ "on inputs "++ show arg1 ++" "++ show arg2++ " returned " ++ show result ++ ", expected " ++ show expected ) 0 where result = f arg1 arg2 test3 msg f [] [] [] [] value = trace (msg++" passed!") value test3 msg f (arg1:args1) (arg2:args2) (arg3:args3) (expected:expecteds) value | result == expected = test3 msg f args1 args2 args3 expecteds value | otherwise = trace (msg ++ "on inputs "++ show arg1 ++" "++ show arg2++ " " ++ show arg3 ++" returned " ++ show result ++ ", expected " ++ show expected ) 0 where result = f arg1 arg2 arg3
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