haskell programming language

Learn from examples

1. Program to output a string, integer, and floating point exhl1.hs source code # exhl1.hs example print string, integer, float -- exhl1.hs example print string, integer, float main :: IO () main = do print "haskell exhl1.hs running"; print "next line" print 7 print 'A' print 37.235E-7 print [[1,2], [3,4,5]] print (('a',1), ('c', 2, 123.456)) putStr "Hello " putStr "again" putStr " and again\n" -- new line works putStrLn "more lines" putStrLn ("option "++show(7)++" of show") print "exhl1.hs finished"; Output from execution: "haskell exhl1.hs running" "next line" 7 'A' 3.7235e-6 [[1,2],[3,4,5]] (('a',1),('c',2,123.456)) Hello again and again more lines option 7 of show "exhl1.hs finished" 2. commands to execute the source code at a minimum, Windows, Linux, MacOSX. Windows: ? exhl1.hs Linux: ghc -o exhl1 exhl1.hs ./exhl1 MacOSX ghc -o exhl1 exhl1.hs exhl1 Sample Makefile: 3. You must be able to declare variables and arrays and matrix of various types. exhl3.hs source code -- exhl3.hs example declare and use variables, list, arrays, matrix -- variable names must start lower case -- function names start upper case vec :: [Double] vec = [1.0, 2.0, 3.0] mat :: [[Integer]] mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] main :: IO () main = do print "haskell exhl3.hs running"; let i = 2 putStr "i = " print i let name = "any string" print name let avec = ["a", "b", "c"] putStr "avec = " print avec let a = sqrt 9.0 putStr "sqrt 9.0 = " print a let a = cos pi/2 putStr "cos pi/2 = " print a let a = 3 + (-2) + 1 putStr "3 + (-2) + 1 = " print a let a = 3 -2 + 1 putStr "3 -2 + 1 = " print a putStr "from above " putStr "vec=" print vec putStr "mat=" print mat print "exhl3.hs finished"; Execution output: "haskell exhl3.hs running" i = 2 "any string" avec = ["a","b","c"] sqrt 9.0 = 3.0 cos pi/2 = -0.5 3 + (-2) + 1 = 2 3 -2 + 1 = 2 from above vec=[1.0,2.0,3.0] mat=[[1,2,3],[4,5,6],[7,8,9]] "exhl3.hs finished" 4. You need to be able to have loops, iteration statements -- exhl4.hs example loops iteration, functions in haskell sumAll :: (Num a) => [a] -> a -- define function sumALL sumAll (x:xs) = x + sumAll xs sumAll [] = 0 a :: [Int] a = [1, 2, 3] b :: [Double] b = [1.1, 2.2, 3.3] c :: [Int] c = [] main :: IO () main=do print "haskell exhl4.hs running"; print "define and use function sumALL, " putStr "a = " print a putStr "sumALL = " print (sumAll a) putStr "b = " print b putStr "sumALL = " print (sumAll b) putStr "c = " print c putStr "sumALL = " print (sumAll c) print "exhl4.hs finished"; Execution output: "haskell exhl4.hs running" "define and use function sumALL, " a = [1,2,3] sumALL = 6 b = [1.1,2.2,3.3] sumALL = 6.6 c = [] sumALL = 0 "exhl4.hs finished" 5. You need if then else conditional statements exhl5.hs source code -- exhl5.hs example if then else, must be in functions in haskell factorial :: (Integral a) => a -> a -- special type, multiple precision factorial n = if n < 2 then 1 else n * factorial (n - 1) main :: IO () main = do print "haskell exhl5.hs running"; print "conditional must be inside function" putStr "factorial(3) = " print (factorial(3)) putStr "factorial(20) = " print (factorial(20)) print "exhl5.hs finished"; Execution output: "haskell exhl5.hs running" "conditional must be inside function" factorial(3) = 6 factorial(20) = 2432902008176640000 "exhl5.hs finished" 6. You need to be able to create functions, procedures, subroutines. exhl6.hs source code -- exhl6.hs example create and use functions factorial :: (Integral a) => a -> a -- special type, multiple precision factorial n = if n < 2 then 1 else n * factorial (n - 1) -- factorial Using recursion (with pattern matching) factr :: (Integral a) => a -> a factr 0 = 1 factr n = n * factr (n - 1) -- Using recursion (with guards) factrg :: (Integral a) => a -> a factrg n | n< 2 = 1 | otherwise = n * factrg (n - 1) -- Using a list and the "product" function factp :: (Integral a) => a -> a factp n = product [1..n] calc :: String -> [Float] -- define function calc calc = foldl f [] . words where f (x:y:zs) "+" = y+x:zs f (x:y:zs) "-" = y-x:zs f (x:y:zs) "*" = y*x:zs f (x:y:zs) "/" = y/x:zs f xs y = read y : xs a = "2.0 3.0 *" b = "2.0 3.0 * 1.5 +" c = "2.0 3.0 * 3.0 0.5 * +" main :: IO () main = do print "haskell exhl6.hs running"; print "4 versions of factorial function" putStr "factorial(3) = " print (factorial(3)) putStr "factorial(20) = " print (factorial(20)) putStr "factorial(52) = " print (factorial(52)) putStr "factr(3) = " print (factr(3)) putStr "factrg(3) = " print (factrg(3)) putStr "factp(3) = " print (factp(3)) putStrLn "fact4.hs finished" print "function calc takes reverse polish expressions" putStr(a++" = ") print(calc a) putStr(b++" = ") print(calc b) putStr(c++" = ") print(calc c) print "exhl6.hs finished"; Execution output: "haskell exhl6.hs running" "4 versions of factorial function" factorial(3) = 6 factorial(20) = 2432902008176640000 factorial(52) = 80658175170943878571660636856403766975289505440883277824000000000000 factr(3) = 6 factrg(3) = 6 factp(3) = 6 fact4.hs finished "function calc takes reverse polish expressions" 2.0 3.0 * = [6.0] 2.0 3.0 * 1.5 + = [7.5] 2.0 3.0 * 3.0 0.5 * + = [7.5] "exhl6.hs finished" 7. You need to be able to read and write files in various formats. exhl7.hs source code -- exhl7.hs example read and write files of various types import System.Environment split :: String -> [String] split [] = [""] split (c:cs) | c == '\n' = "" : rest | otherwise = (c : head rest) : tail rest where rest = split cs main :: IO () main = do print "haskell exhl7.hs running" print "exhl7.hs two args in command" [fin,fout] <- getArgs putStr "arg1 = " print fin putStr "arg2 = " print fout print "reading file" s <- readFile fin print s -- entire file as string new lines print "writing file" writeFile fout s print " " print "split lines" let ss = split s print ss print " " let s0 = head ss print s0 let t0 = tail ss let s1 = head t0 print s1 let t1 = tail t0 let s2 = head t1 print s2 print " " print "exhl7.hs finished" Execution output: "haskell exhl7.hs running" "exhl7.hs two args in command" arg1 = "file.txt" arg2 = "junk.out" "reading file" "first line\nsecond line\nlast line\n" "writing file" " " "split lines" ["first line","second line","last line",""] " " "first line" "second line" "last line" " " "exhl7.hs finished" 8. You need to be able to use a number of files combined to build a program. This may include packages, libraries, operating system commands, header files, etc. exhl8.hs source code -- exhl8.hs example use libraries main :: IO () main = do print "haskell exhl8.hs running"; -- coming when known print "exhl8.hs finished"; Execution output: "haskell exhl8.hs running" "exhl8.hs finished"

Last updated 9/19/2019