From 5502febb8d11caa71a5e846ef2894a27e11494ba Mon Sep 17 00:00:00 2001 From: DzyubSpirit Date: Fri, 4 Oct 2019 12:55:31 +0300 Subject: [PATCH] Add Haskell examples --- Haskell/1-comments.hs | 6 +++ Haskell/2-let-where.hs | 22 ++++++++++ Haskell/4-types.hs | 51 +++++++++++++++++++++++ Haskell/5-NaN-Infinity.hs | 6 +++ Haskell/6-read.hs | 88 +++++++++++++++++++++++++++++++++++++++ Haskell/8-bitwise.hs | 52 +++++++++++++++++++++++ Haskell/9-bigint.hs | 47 +++++++++++++++++++++ 7 files changed, 272 insertions(+) create mode 100644 Haskell/1-comments.hs create mode 100644 Haskell/2-let-where.hs create mode 100644 Haskell/4-types.hs create mode 100644 Haskell/5-NaN-Infinity.hs create mode 100644 Haskell/6-read.hs create mode 100644 Haskell/8-bitwise.hs create mode 100644 Haskell/9-bigint.hs diff --git a/Haskell/1-comments.hs b/Haskell/1-comments.hs new file mode 100644 index 0000000..0b33cd6 --- /dev/null +++ b/Haskell/1-comments.hs @@ -0,0 +1,6 @@ +-- Single comment. + +{- + Multiline + comments. +-} diff --git a/Haskell/2-let-where.hs b/Haskell/2-let-where.hs new file mode 100644 index 0000000..47c2ca3 --- /dev/null +++ b/Haskell/2-let-where.hs @@ -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 diff --git a/Haskell/4-types.hs b/Haskell/4-types.hs new file mode 100644 index 0000000..1d504b9 --- /dev/null +++ b/Haskell/4-types.hs @@ -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"] diff --git a/Haskell/5-NaN-Infinity.hs b/Haskell/5-NaN-Infinity.hs new file mode 100644 index 0000000..fae8335 --- /dev/null +++ b/Haskell/5-NaN-Infinity.hs @@ -0,0 +1,6 @@ +main = do + let count = 0 / 0 + print count + + print (1 / 0) + print (-1 / 0) diff --git a/Haskell/6-read.hs b/Haskell/6-read.hs new file mode 100644 index 0000000..d69e1d9 --- /dev/null +++ b/Haskell/6-read.hs @@ -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" -- [] diff --git a/Haskell/8-bitwise.hs b/Haskell/8-bitwise.hs new file mode 100644 index 0000000..b0a3fe5 --- /dev/null +++ b/Haskell/8-bitwise.hs @@ -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) diff --git a/Haskell/9-bigint.hs b/Haskell/9-bigint.hs new file mode 100644 index 0000000..d78e081 --- /dev/null +++ b/Haskell/9-bigint.hs @@ -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]) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy