Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Haskell examples #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Haskell/1-comments.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- Single comment.

{-
Multiline
comments.
-}
22 changes: 22 additions & 0 deletions Haskell/2-let-where.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
globalX = 5

main = do
-- Use the global definition.
print globalX -- 5

-- Define a constant localX with the value 6.
let localX = 6
print localX -- 6

-- Multiline let.
let addX = 7
newX = localX + addX
print newX -- 13

-- Define constants with 'where' syntax.
let newY = localY + addY
where
localY = 8
addY = 9

print newY -- 17
51 changes: 51 additions & 0 deletions Haskell/4-types.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Text.Printf(printf)

main = do
-- Built-in types

-- Singnature specification '::' is optional.
-- In the case of ambiguity like Integer vs Int or Double vs Float
-- default types Integer or Double will be used.
let integer = 5 :: Integer
int = 5 :: Int
double = 10.3 :: Double
float = 10.3 :: Float
char = 'c' :: Char
string1 = "Hello" :: String -- String and [Char] are the same
string2 = "Hello" :: [Char] -- String and [Char] are the same
bool = True :: Bool

print integer -- 5
print int -- 5
print double -- 10.3
print float -- 10.3
print char -- 'c'
print string1 -- "Hello"
print string2 -- "Hello"
print bool -- True

-- Tuples
let person = ("Marcus Aurelius", 121, "Roma", "emperor")
(_, _, city, _) = person -- _ means discard the value

print person -- ("Marcus Aurelius", 121, "Roma", "emperor")
print city -- "Roma"

let point = (20, 30) :: (Int, Int)
x = fst point
y = snd point

print point
printf "%v %v" x y

let cities = ["Athens", "Roma", "London", "Beijing", "Kiev", "Riga"]
cities' = cities ++ ["Odessa"] -- ' is the valid character in the name
cities'' = "New York" : cities'
cities''' = tail cities''
cities'''' = init cities'''

print cities -- ["Athens","Roma","London","Beijing","Kiev","Riga"]
print cities' -- ["Athens","Roma","London","Beijing","Kiev","Riga","Odessa"
print cities'' -- ["New York","Athens","Roma","London","Beijing","Kiev","Riga","Odessa"]
print cities''' -- ["Athens","Roma","London","Beijing","Kiev","Riga","Odessa"]
print cities'''' -- ["Athens","Roma","London","Beijing","Kiev","Riga"]
6 changes: 6 additions & 0 deletions Haskell/5-NaN-Infinity.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
main = do
let count = 0 / 0
print count

print (1 / 0)
print (-1 / 0)
88 changes: 88 additions & 0 deletions Haskell/6-read.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Data.Char (isHexDigit, ord)
import Numeric (readInt, readFloat, readOct, readDec, readHex, readSigned)
import Text.Printf (printf)

main = do
print $ readInt 2 (`elem` "01") (\x -> ord x - ord '0') "11" -- [(3, "")]
-- The same as 'print (readOct "11")'.
print $ readOct "11" -- [(9, "")]

print $ readDec "5" -- [(5, "")]
print $ readDec "+5" -- []
print $ readDec "-5" -- []
print $ readDec "5mm" -- [(5, "mm")]
print $ readSigned readDec "5" -- [(5, "")]
print $ readSigned readDec "+5" -- []
print $ readSigned readDec "-5" -- [(-5, "")]
print $ readSigned readDec "5mm" -- [(5, "mm")]

print $ readDec "(5)" -- []
print $ readDec "\"5\"" -- []
print $ readDec "[5]" -- []

print $ readDec "NaN" -- []
print $ readDec "Infinity" -- []
print $ readDec "-Infinity" -- []

-- Type specification is required to resolve the ambiguity of whether we should read Int or Integer.
print $ readDec "5.1"
-- 5.1 [(5, ".1")]
print $ readDec "5.1e50"
-- 5.1 [(5, ".1e50")]
print $ readDec "0.000000000005"
-- [(0, ".000000000005")]
print $ readDec "0.0000005"
-- [(0, ".0000005")]
print $ readDec "0.000005"
-- [(0, ".0000005")]

print $ readHex "fF" -- [(255, "")]
print $ readHex "0xff" -- [(0, "xff")]
print $ readHex "0xFf" -- [(0, "xFf")]
print $ readHex " 0xFf " -- []

print $ readInt 2 (`elem` "01") (\x -> ord x - ord '0') "ff" -- []
print $ readOct "ff" -- []
print $ readDec "ff" -- []
print $ readHex "ff" -- [(255, "")]

let readHexDigit x = fst $ head $ readHex [x]
print $ readInt 16 isHexDigit readHexDigit "ff" -- [(255, "")]
print $ readInt 17 isHexDigit readHexDigit "ff" -- [(270, "")]
print $ readInt 20 isHexDigit readHexDigit "ff" -- [(315, "")]
print $ readInt 30 isHexDigit readHexDigit "ff" -- [(465, "")]
print $ readInt 31 isHexDigit readHexDigit "ff" -- [(480, "")]
print $ readInt 32 isHexDigit readHexDigit "ff" -- [(495, "")]
print $ readInt 33 isHexDigit readHexDigit "ff" -- [(510, "")]
print $ readInt 34 isHexDigit readHexDigit "ff" -- [(525, "")]
print $ readInt 35 isHexDigit readHexDigit "ff" -- [(540, "")]
print $ readInt 36 isHexDigit readHexDigit "ff" -- [(555, "")]
print $ readInt 37 isHexDigit readHexDigit "ff" -- [(570, "")]

print $ readFloat "3.14" -- [(3.14, "")]
print $ readFloat "314e-2" -- [(3.14, "")]
print $ readFloat "3.14text" -- [(3.14, "text")]
print $ readFloat "0.0314E+2" -- [(3.14, "")]

print $ readFloat "5" -- [(5.0, "")]
print $ readFloat "5.0" -- [(5.0, "")]
print $ readFloat "5.0000000000000001" -- [(5.0, "")]

print $ readFloat "5.1" -- [(5.1, "")]
print $ readFloat "5.000000000000001" -- [(5.000000000000001, "")]

print $ readFloat "100" -- [(100, "")]
print $ readSigned readFloat "-100" -- [(-100, "")]
print $ readFloat "+100" -- []

print $ readFloat "5" -- [(5, "")]
print $ readFloat "+5" -- []
print $ readFloat "5mm" -- [(5, "mm")]

print $ readFloat "(5)" -- []
print $ readFloat "\"5\"" -- []
print $ readFloat "[5]" -- []

print $ readFloat "NaN" -- []
print $ readFloat "Infinity" -- []
print $ readFloat "-Infinity" -- []
52 changes: 52 additions & 0 deletions Haskell/8-bitwise.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Data.Char (chr, ord)
import Data.Bits ((.&.), (.|.), xor, complement, shift, shiftL, shiftR, rotate)
import Numeric (showIntAtBase, showSigned)
import Text.Printf(printf)

main = do
let toBinary :: Int -> String
toBinary n = showSigned (showIntAtBase 2 toBinDigit) 0 n ""
where toBinDigit d = chr (ord '0' + d)

let a = 9
b = 14
c = -9

let aBinary = toBinary a
bBinary = toBinary b
cBinary = toBinary c

printf "%v to base 2: %v\n" a aBinary
printf "%v to base 2: %v\n" b bBinary

print "Bitwise operators"

printf "%v .&. %v = %v\n" a b $ a .&. b
printf "%v .&. %v = %v\n" aBinary bBinary $ toBinary $ a .&. b

printf "%v .|. %v = %v\n" a b $ a .|. b
printf "%v .|. %v = %v\n" aBinary bBinary $ toBinary $ a .|. b

printf "%v `xor` %v = %v\n" a b $ a `xor` b
printf "%v `xor` %v = %v\n" aBinary bBinary $ toBinary $ a `xor` b

printf "complement %v = %v\n" a $ complement a
printf "complement %v = %v\n" aBinary $ toBinary $ complement a

printf "%v `shiftL` 2 = %v\n" a $ a `shiftL` 2
printf "%v `shiftL` 2 = %v\n" aBinary $ toBinary $ a `shiftL` 2

printf "%v `shiftR` 2 = %v\n" b $ b `shiftR` 2
printf "%v `shiftR` 2 = %v\n" bBinary $ toBinary $ b `shiftR` 2

printf "%v `shift` 2 = %v\n" a $ a `shift` 2
printf "%v `shift` 2 = %v\n" aBinary $ toBinary $ a `shift` 2

printf "%v `shift` (-2) = %v\n" b $ b `shift` (-2)
printf "%v `shift` (-2) = %v\n" bBinary $ toBinary $ b `shift` (-2)

printf "%v `rotate` 2 = %v\n" b $ b `rotate` 2
printf "%v `rotate` 2 = %v\n" b $ toBinary $ b `rotate` 2

printf "%v `rotate` (-2) = %v\n" c $ c `rotate` (-2)
printf "%v `rotate` (-2) = %v\n" c $ toBinary $ c `rotate` (-2)
47 changes: 47 additions & 0 deletions Haskell/9-bigint.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Text.Printf(printf)
import Data.List(sort)

main = do
let maxInt = maxBound :: Int
integer = fromIntegral maxInt :: Integer

printf "maxInt = %v\n" (maxInt :: Int)
printf "maxInt + 1 = %v\n" (maxInt + 1 :: Int)
printf "maxInt + 2 = %v\n" (maxInt + 2 :: Int)
putStrLn ""

printf "maxInt = %v\n" maxInt
printf "integer = %v\n" integer
putStrLn ""

printf "fromIntegral maxInt == integer: %v\n"
$ show
$ fromIntegral maxInt
== integer
putStrLn ""

printf "integer = %v\n" integer
printf "integer + 1 = %v\n" $ integer + 1
printf "integer + 2 = %v\n" $ integer + 2
putStrLn ""

printf "15 `div` 3 = %v\n" (15 `div` 3 :: Int)
printf "3 `div` 2 = %v\n" (3 `div` 2 :: Int)
-- printf "3 / 2 = %v\n" (3 / 2 :: Int)
-- Doesn't compile because operator / isn't defined for Int and Integer.
-- let a = 1.5 :: Integer
-- Doesn't compile because Fractional can't be converted to Integer.

printf "(1000 ^ 200) / 12321 = %v\n" (1000 ^ 200 `div` 12321 :: Integer)
putStrLn ""

let array2 = [3, -2, 7, -5, -1, 2, 5, 0] :: [Integer]
print array2
print $ sort array2
putStrLn ""

let array1 :: Num a => [a]
array1 = [-2, 7, 1, 3, -2, 8, 5, -4]
print (array1 :: [Integer])
print $ sort (array1 :: [Int])
print $ sort (array1 :: [Integer])