Skip to content

Commit 5502feb

Browse files
committed
Add Haskell examples
1 parent ee0a414 commit 5502feb

7 files changed

Lines changed: 272 additions & 0 deletions

File tree

Haskell/1-comments.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Single comment.
2+
3+
{-
4+
Multiline
5+
comments.
6+
-}

Haskell/2-let-where.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
globalX = 5
2+
3+
main = do
4+
-- Use the global definition.
5+
print globalX -- 5
6+
7+
-- Define a constant localX with the value 6.
8+
let localX = 6
9+
print localX -- 6
10+
11+
-- Multiline let.
12+
let addX = 7
13+
newX = localX + addX
14+
print newX -- 13
15+
16+
-- Define constants with 'where' syntax.
17+
let newY = localY + addY
18+
where
19+
localY = 8
20+
addY = 9
21+
22+
print newY -- 17

Haskell/4-types.hs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import Text.Printf(printf)
2+
3+
main = do
4+
-- Built-in types
5+
6+
-- Singnature specification '::' is optional.
7+
-- In the case of ambiguity like Integer vs Int or Double vs Float
8+
-- default types Integer or Double will be used.
9+
let integer = 5 :: Integer
10+
int = 5 :: Int
11+
double = 10.3 :: Double
12+
float = 10.3 :: Float
13+
char = 'c' :: Char
14+
string1 = "Hello" :: String -- String and [Char] are the same
15+
string2 = "Hello" :: [Char] -- String and [Char] are the same
16+
bool = True :: Bool
17+
18+
print integer -- 5
19+
print int -- 5
20+
print double -- 10.3
21+
print float -- 10.3
22+
print char -- 'c'
23+
print string1 -- "Hello"
24+
print string2 -- "Hello"
25+
print bool -- True
26+
27+
-- Tuples
28+
let person = ("Marcus Aurelius", 121, "Roma", "emperor")
29+
(_, _, city, _) = person -- _ means discard the value
30+
31+
print person -- ("Marcus Aurelius", 121, "Roma", "emperor")
32+
print city -- "Roma"
33+
34+
let point = (20, 30) :: (Int, Int)
35+
x = fst point
36+
y = snd point
37+
38+
print point
39+
printf "%v %v" x y
40+
41+
let cities = ["Athens", "Roma", "London", "Beijing", "Kiev", "Riga"]
42+
cities' = cities ++ ["Odessa"] -- ' is the valid character in the name
43+
cities'' = "New York" : cities'
44+
cities''' = tail cities''
45+
cities'''' = init cities'''
46+
47+
print cities -- ["Athens","Roma","London","Beijing","Kiev","Riga"]
48+
print cities' -- ["Athens","Roma","London","Beijing","Kiev","Riga","Odessa"
49+
print cities'' -- ["New York","Athens","Roma","London","Beijing","Kiev","Riga","Odessa"]
50+
print cities''' -- ["Athens","Roma","London","Beijing","Kiev","Riga","Odessa"]
51+
print cities'''' -- ["Athens","Roma","London","Beijing","Kiev","Riga"]

Haskell/5-NaN-Infinity.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
main = do
2+
let count = 0 / 0
3+
print count
4+
5+
print (1 / 0)
6+
print (-1 / 0)

Haskell/6-read.hs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import Data.Char (isHexDigit, ord)
2+
import Numeric (readInt, readFloat, readOct, readDec, readHex, readSigned)
3+
import Text.Printf (printf)
4+
5+
main = do
6+
print $ readInt 2 (`elem` "01") (\x -> ord x - ord '0') "11" -- [(3, "")]
7+
-- The same as 'print (readOct "11")'.
8+
print $ readOct "11" -- [(9, "")]
9+
10+
print $ readDec "5" -- [(5, "")]
11+
print $ readDec "+5" -- []
12+
print $ readDec "-5" -- []
13+
print $ readDec "5mm" -- [(5, "mm")]
14+
print $ readSigned readDec "5" -- [(5, "")]
15+
print $ readSigned readDec "+5" -- []
16+
print $ readSigned readDec "-5" -- [(-5, "")]
17+
print $ readSigned readDec "5mm" -- [(5, "mm")]
18+
19+
print $ readDec "(5)" -- []
20+
print $ readDec "\"5\"" -- []
21+
print $ readDec "[5]" -- []
22+
23+
print $ readDec "NaN" -- []
24+
print $ readDec "Infinity" -- []
25+
print $ readDec "-Infinity" -- []
26+
27+
-- Type specification is required to resolve the ambiguity of whether we should read Int or Integer.
28+
print $ readDec "5.1"
29+
-- 5.1 [(5, ".1")]
30+
print $ readDec "5.1e50"
31+
-- 5.1 [(5, ".1e50")]
32+
print $ readDec "0.000000000005"
33+
-- [(0, ".000000000005")]
34+
print $ readDec "0.0000005"
35+
-- [(0, ".0000005")]
36+
print $ readDec "0.000005"
37+
-- [(0, ".0000005")]
38+
39+
print $ readHex "fF" -- [(255, "")]
40+
print $ readHex "0xff" -- [(0, "xff")]
41+
print $ readHex "0xFf" -- [(0, "xFf")]
42+
print $ readHex " 0xFf " -- []
43+
44+
print $ readInt 2 (`elem` "01") (\x -> ord x - ord '0') "ff" -- []
45+
print $ readOct "ff" -- []
46+
print $ readDec "ff" -- []
47+
print $ readHex "ff" -- [(255, "")]
48+
49+
let readHexDigit x = fst $ head $ readHex [x]
50+
print $ readInt 16 isHexDigit readHexDigit "ff" -- [(255, "")]
51+
print $ readInt 17 isHexDigit readHexDigit "ff" -- [(270, "")]
52+
print $ readInt 20 isHexDigit readHexDigit "ff" -- [(315, "")]
53+
print $ readInt 30 isHexDigit readHexDigit "ff" -- [(465, "")]
54+
print $ readInt 31 isHexDigit readHexDigit "ff" -- [(480, "")]
55+
print $ readInt 32 isHexDigit readHexDigit "ff" -- [(495, "")]
56+
print $ readInt 33 isHexDigit readHexDigit "ff" -- [(510, "")]
57+
print $ readInt 34 isHexDigit readHexDigit "ff" -- [(525, "")]
58+
print $ readInt 35 isHexDigit readHexDigit "ff" -- [(540, "")]
59+
print $ readInt 36 isHexDigit readHexDigit "ff" -- [(555, "")]
60+
print $ readInt 37 isHexDigit readHexDigit "ff" -- [(570, "")]
61+
62+
print $ readFloat "3.14" -- [(3.14, "")]
63+
print $ readFloat "314e-2" -- [(3.14, "")]
64+
print $ readFloat "3.14text" -- [(3.14, "text")]
65+
print $ readFloat "0.0314E+2" -- [(3.14, "")]
66+
67+
print $ readFloat "5" -- [(5.0, "")]
68+
print $ readFloat "5.0" -- [(5.0, "")]
69+
print $ readFloat "5.0000000000000001" -- [(5.0, "")]
70+
71+
print $ readFloat "5.1" -- [(5.1, "")]
72+
print $ readFloat "5.000000000000001" -- [(5.000000000000001, "")]
73+
74+
print $ readFloat "100" -- [(100, "")]
75+
print $ readSigned readFloat "-100" -- [(-100, "")]
76+
print $ readFloat "+100" -- []
77+
78+
print $ readFloat "5" -- [(5, "")]
79+
print $ readFloat "+5" -- []
80+
print $ readFloat "5mm" -- [(5, "mm")]
81+
82+
print $ readFloat "(5)" -- []
83+
print $ readFloat "\"5\"" -- []
84+
print $ readFloat "[5]" -- []
85+
86+
print $ readFloat "NaN" -- []
87+
print $ readFloat "Infinity" -- []
88+
print $ readFloat "-Infinity" -- []

Haskell/8-bitwise.hs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Data.Char (chr, ord)
2+
import Data.Bits ((.&.), (.|.), xor, complement, shift, shiftL, shiftR, rotate)
3+
import Numeric (showIntAtBase, showSigned)
4+
import Text.Printf(printf)
5+
6+
main = do
7+
let toBinary :: Int -> String
8+
toBinary n = showSigned (showIntAtBase 2 toBinDigit) 0 n ""
9+
where toBinDigit d = chr (ord '0' + d)
10+
11+
let a = 9
12+
b = 14
13+
c = -9
14+
15+
let aBinary = toBinary a
16+
bBinary = toBinary b
17+
cBinary = toBinary c
18+
19+
printf "%v to base 2: %v\n" a aBinary
20+
printf "%v to base 2: %v\n" b bBinary
21+
22+
print "Bitwise operators"
23+
24+
printf "%v .&. %v = %v\n" a b $ a .&. b
25+
printf "%v .&. %v = %v\n" aBinary bBinary $ toBinary $ a .&. b
26+
27+
printf "%v .|. %v = %v\n" a b $ a .|. b
28+
printf "%v .|. %v = %v\n" aBinary bBinary $ toBinary $ a .|. b
29+
30+
printf "%v `xor` %v = %v\n" a b $ a `xor` b
31+
printf "%v `xor` %v = %v\n" aBinary bBinary $ toBinary $ a `xor` b
32+
33+
printf "complement %v = %v\n" a $ complement a
34+
printf "complement %v = %v\n" aBinary $ toBinary $ complement a
35+
36+
printf "%v `shiftL` 2 = %v\n" a $ a `shiftL` 2
37+
printf "%v `shiftL` 2 = %v\n" aBinary $ toBinary $ a `shiftL` 2
38+
39+
printf "%v `shiftR` 2 = %v\n" b $ b `shiftR` 2
40+
printf "%v `shiftR` 2 = %v\n" bBinary $ toBinary $ b `shiftR` 2
41+
42+
printf "%v `shift` 2 = %v\n" a $ a `shift` 2
43+
printf "%v `shift` 2 = %v\n" aBinary $ toBinary $ a `shift` 2
44+
45+
printf "%v `shift` (-2) = %v\n" b $ b `shift` (-2)
46+
printf "%v `shift` (-2) = %v\n" bBinary $ toBinary $ b `shift` (-2)
47+
48+
printf "%v `rotate` 2 = %v\n" b $ b `rotate` 2
49+
printf "%v `rotate` 2 = %v\n" b $ toBinary $ b `rotate` 2
50+
51+
printf "%v `rotate` (-2) = %v\n" c $ c `rotate` (-2)
52+
printf "%v `rotate` (-2) = %v\n" c $ toBinary $ c `rotate` (-2)

Haskell/9-bigint.hs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Text.Printf(printf)
2+
import Data.List(sort)
3+
4+
main = do
5+
let maxInt = maxBound :: Int
6+
integer = fromIntegral maxInt :: Integer
7+
8+
printf "maxInt = %v\n" (maxInt :: Int)
9+
printf "maxInt + 1 = %v\n" (maxInt + 1 :: Int)
10+
printf "maxInt + 2 = %v\n" (maxInt + 2 :: Int)
11+
putStrLn ""
12+
13+
printf "maxInt = %v\n" maxInt
14+
printf "integer = %v\n" integer
15+
putStrLn ""
16+
17+
printf "fromIntegral maxInt == integer: %v\n"
18+
$ show
19+
$ fromIntegral maxInt
20+
== integer
21+
putStrLn ""
22+
23+
printf "integer = %v\n" integer
24+
printf "integer + 1 = %v\n" $ integer + 1
25+
printf "integer + 2 = %v\n" $ integer + 2
26+
putStrLn ""
27+
28+
printf "15 `div` 3 = %v\n" (15 `div` 3 :: Int)
29+
printf "3 `div` 2 = %v\n" (3 `div` 2 :: Int)
30+
-- printf "3 / 2 = %v\n" (3 / 2 :: Int)
31+
-- Doesn't compile because operator / isn't defined for Int and Integer.
32+
-- let a = 1.5 :: Integer
33+
-- Doesn't compile because Fractional can't be converted to Integer.
34+
35+
printf "(1000 ^ 200) / 12321 = %v\n" (1000 ^ 200 `div` 12321 :: Integer)
36+
putStrLn ""
37+
38+
let array2 = [3, -2, 7, -5, -1, 2, 5, 0] :: [Integer]
39+
print array2
40+
print $ sort array2
41+
putStrLn ""
42+
43+
let array1 :: Num a => [a]
44+
array1 = [-2, 7, 1, 3, -2, 8, 5, -4]
45+
print (array1 :: [Integer])
46+
print $ sort (array1 :: [Int])
47+
print $ sort (array1 :: [Integer])

0 commit comments

Comments
 (0)