haskellref.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. Haskell reference
  2. Haskell is a functional (that is, everything is done with function calls),
  3. statically, implicitly typed (types are checked by the compiler, but you don't
  4. have to declare them), lazy (nothing is done until it needs to be) language.
  5. www.haskell.org/platform/
  6. www.haskell.org/downloads/
  7. www.haskell.org/hoogle/
  8. For beginners - https://argumatronic.com/posts/1970-01-01-beginners.html
  9. Documentation:
  10. Haskell Documentation - https://haskell.org/
  11. Haskell Tutorials - https://wiki.haskell.org/Tutorials
  12. Haskell in 5 steps - https://wiki.haskell.org/Haskell_in_5_steps
  13. Haskell Books and tutorials - https://wiki.haskell.org/Books_and_tutorials
  14. Cabal User Guide - https://cabal.readthedocs.io/en/stable/
  15. Haskell Cabal (Common Architecture for Building Applications and Libraries) -
  16. https://www.haskell.org/cabal/index.html#nstall-upgrade
  17. Basic libraries -
  18. https://hackage.haskell.org/package/base-4.17.0.0/docs/Data-List.html
  19. Math functions -
  20. https://hackage.haskell.org/package/ClassyPrelude-0.1/docs/Prelude-Math.html
  21. Collection of tools for numeric computations -
  22. https://hackage.haskell.org/package/math-functions
  23. Introduction to Haskell -
  24. https://www.cis.upenn.edu/~cis1940/spring13/lectures.html
  25. Haskell and Functional Programming -
  26. https://courses.cs.washington.edu/courses/cse341/14wi/haskell/index.html
  27. Glasgow Haskell Compiler -
  28. https://downloads.haskell.org/ghc/latest/docs/users_guide/index.html
  29. Learn you a haskell for great good - http://learnyouhaskell.com/
  30. FP Complete Haskell - https://www.fpcomplete.com/haskell/learn/
  31. Applied Haskell Syllabus - https://www.fpcomplete.com/haskell/syllabus/
  32. Haskell for python developers -
  33. https://www.softwarefactory-project.io/haskell-for-python-developers.html
  34. Installation: https://www.haskell.org/ghcup/install/
  35. $ curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
  36. System requirements [Linux Debian]
  37. build-essential
  38. curl
  39. libffi-dev
  40. libffi6
  41. libgmp-dev
  42. libgmp10
  43. libncurses-dev
  44. libncurses5
  45. libtinfo5
  46. ghcup user guide: https://www.haskell.org/ghcup/guide/
  47. ghcup tui # text-based user interface
  48. ghcup --help # help
  49. ghcup list # list available ghc/cabal versions
  50. ghcup install ghc # install the recommended GHC version
  51. ghcup install ghc 8.2.2 # install a specific GHC version
  52. ghcup set ghc 8.4.4 # set the currently "active" GHC version
  53. ghcup install cabal # install cabal install
  54. ghcup upgrade # update ghcup itself
  55. ghc -- compiles Haskell libraries or applications to binary code
  56. ghci -- an interpreter that lets you write Haskell code and get feedback
  57. -- compile haskell program
  58. $ ghc -o program program.hs
  59. -- alternatively, we can skip the compilation phase
  60. -- runghc interprets the source file instead of compiling it
  61. $ runghc program.hs
  62. -- -Wall flag will enable GHC to emit warnings about our code
  63. $ ghc -Wall program.hs -fforce-recomp
  64. -- build with ghc
  65. $ ghc --make program.hs
  66. -- essential commands
  67. :? -- list commands (show all commands)
  68. :quit (or :q)
  69. :load filename.hs (or :l filename.hs)
  70. :reload -- reload the current file set after you'ved edited it (or :r)
  71. :set editor name -- set editor to "name"
  72. :edit name -- edit current script
  73. :cd -- change to another directory
  74. :type expr -- give the type of an expression (or -- just :t)
  75. :set +t tell Haskell to print type information after evaluation
  76. :kind (or :k) -- kind inspection
  77. :info (or :i) -- information
  78. :print (or :p) -- print the expression
  79. :module (or :m) -- add modules to imports
  80. :add (or :ad) -- load a file into the REPL namespace
  81. :instances (or :in) -- show instances of a typeclass
  82. :browse (or :bro) -- browse all available symbols in the REPL namespace
  83. -- The major namespaces are described below with their naming conventions
  84. Namespace Convention
  85. Modules Uppercase
  86. Functions Lowercase
  87. Variables Lowercase
  88. Type Variables Lowercase
  89. Datatypes Uppercase
  90. Constructors Uppercase
  91. Typeclasses Uppercase
  92. Synonyms Uppercase
  93. Type Families Uppercase
  94. -- the following list of keywords have a special meaning in the language, and
  95. -- cannot be used as the names of functions or their arguments:
  96. case class data default deriving
  97. do else foreign if import in
  98. infix infixl infixr instance let
  99. module newtype of then type where
  100. -- some basic types in Haskell
  101. Type Name Example value
  102. Integers (limited range) Int 42
  103. Integers (infinite range) Integer 7376541234
  104. Reals Float 3.1415927
  105. Booleans Bool False
  106. Characters Char 'a'
  107. Strings String "haskell"
  108. -- some basic operations in Haskell
  109. Name Operation
  110. a == b Equality (a = b)
  111. a /= b Inequality (a != b)
  112. a && b Logical and (a ^ b)
  113. a || b Logical or (a v b)
  114. logBase b x Logarithm (log_b x)
  115. truncate x select integer part only
  116. -- differences between function application in mathematics and in Haskell
  117. Mathematics Haskell
  118. f(x) f x
  119. f(x, y) f x y
  120. f(g(x)) f (g x)
  121. f(x, g(y)) f x (g y)
  122. f(x) g(y) f x * g y
  123. -- single line comments are made with double dashes
  124. {- multi-line comment made
  125. with braces and dashes
  126. -}
  127. -- executing the program in ghci
  128. ghci> :l program.hs (:load)
  129. ghci> :r (:reload)
  130. -- standard prelude
  131. -- select the first element of a non-empty list
  132. > head [1, 2, 3, 4, 5]
  133. 1
  134. -- remove the first element from a non-empty list
  135. > tail [1, 2, 3, 4, 5]
  136. [2, 3, 4, 5]
  137. -- select the nth element of list (counting from zero)
  138. > [1, 2, 3, 4, 5] !! 2
  139. 3
  140. -- select the first n elements of a list
  141. > take 3 [1, 2, 3, 4, 5]
  142. [1, 2, 3]
  143. -- remove the first n elements from a list
  144. > drop 3 [1, 2, 3, 4, 5]
  145. [4, 5]
  146. -- calculate the length of a list
  147. > length [1, 2, 3, 4, 5]
  148. 5
  149. -- calculate the sum of a list of numbers
  150. > sum [1, 2, 3, 4, 5]
  151. 15
  152. -- calculate the product of a list of numbers
  153. > product [1, 2, 3, 4, 5]
  154. 120
  155. -- append two lists
  156. > [1, 2, 3] ++ [4, 5]
  157. [1, 2, 3, 4, 5]
  158. -- reverse a list
  159. > reverse [1, 2, 3, 4, 5]
  160. [5, 4, 3, 2, 1]
  161. -- defining functions
  162. -- let functionName argName
  163. ghci> let addOneTo i = i + 1 -- in this case the argument is i
  164. ghci> addOneTo 5
  165. 6
  166. -- factorial function
  167. factorial :: Int -> Int
  168. factorial n = if n == 0 then 1 else n * factorial(n - 1)
  169. -- factorial computation without the above function
  170. factorial n = product [1..n]
  171. -- fibonacci function
  172. fibonacci :: Int -> Int
  173. fibonacci n = if n == 0 then 0 else if n == 1 then 1 else fibonacci(n - 2) + fibonacci(n - 1)
  174. -- nested function using let
  175. -- function that takes two numbers and outputs two numbers
  176. alwaysEven a b = let isEven x = if even x
  177. then x
  178. else x - 1
  179. in (isEven a, isEven b)
  180. -- in can be written anywhere as long as it is written after the let functions
  181. ghci> alwaysEven 30 23
  182. (30,22)
  183. ghci> isEven 5
  184. <interactive> Not in scope 'isEven'
  185. -- nested function using where
  186. -- function that takes two numbers and outputs two numbers
  187. alwaysEven a b = (isEven a, isEven b)
  188. where isEven x = if even x then x else x - 1
  189. -- equality checks
  190. ghci> 1 /= 2 -- in almost other programming languages 1 != 2
  191. True
  192. ghci> 1 == 1
  193. True
  194. -- function names
  195. function123 -- correct
  196. fUNCTION123 -- correct
  197. function' -- correct
  198. Function -- incorrect
  199. 123function -- incorrect
  200. 'function -- incorrect
  201. -- infix functions: functions that take argument on the left and an argument on the right
  202. a + b
  203. a - b
  204. a * b
  205. a / b
  206. a ^ b
  207. a == b
  208. a /= b
  209. -- prefix functions: two arguments on the right
  210. even a
  211. max a b
  212. min a b
  213. -- infix functions can also be used as prefix functions when wraped in parentheses
  214. (+) 1 2
  215. -- prefix functions are also be used as infix functions
  216. a `max` b
  217. a `min` b
  218. --lists and tuples are used to store multiple items
  219. -- list: can store anything with single type (list of ints, list of characters)
  220. same = ['l', 'c'] == "lc"
  221. -- head: changes its type from list of int to int
  222. head :: [a] -> a
  223. head [1, 2, 3]
  224. 1
  225. head "Institute"
  226. 'I'
  227. -- tail
  228. tail :: [a] -> [a]
  229. tail [1, 2, 3]
  230. [2, 3]
  231. tail "Institute"
  232. "nstitute"
  233. -- last
  234. last :: [a] -> a
  235. last [1, 2, 3]
  236. 3
  237. last "Institute"
  238. 'e'
  239. -- init
  240. init :: [a] -> [a]
  241. init [1, 2, 3]
  242. [1, 2]
  243. init "Institute"
  244. "Institut"
  245. -- list of lists (nested list)
  246. -- the nested list are seen as single values just like characters or ints in a list
  247. let ourList = [[1, 2, 3], [4, 5, 6]]
  248. ourList
  249. [[1, 2, 3], [4, 5, 6]]
  250. head ourList
  251. [1,2,3]
  252. tail ourList
  253. [[4,5,6]]
  254. last ourList
  255. [4,5,6]
  256. init ourList
  257. [[1,2,3]]
  258. -- get certain element from the list
  259. [1, 2, 3, 4, 5, 6] !! 2
  260. 3 -- index starts from 0
  261. -- add values to a list
  262. : is an infix function that takes single value on its left and a list on its right
  263. 1 : [2, 3, 4, 5, 6] -- which are of same type (prepending function)
  264. [1,2,3,4,5,6]
  265. ++ is also an infix function which appends a list to another list which are of same type
  266. [1, 2, 3] ++ [4, 5, 6]
  267. [1,2,3,4,5,6]
  268. -- the function to append list however is slower than the prepending function
  269. -- this because when we prepend a value to a list it will be added almost instantly
  270. -- however when we append a values the program has to go all the way to the end of the
  271. -- list to append it and when dealing with large list this can become quite slow
  272. -- haskell doesn't know where the end of the list is, it only knows what the first item is
  273. -- so it has to check all the items before it can get to the end
  274. -- laziness
  275. -- simply put haskell only evaluates something if it is asked for, the cool thing about
  276. -- that is when we need to get the first item of the list it wont bother with the rest of
  277. -- it, it will evaluate the first item and nothing more, this is great in terms of
  278. -- performance, and enable us to create interesting algorithms
  279. ghci> ourList = [1, 2, 3, error "Whoops", 5, 6, 7]
  280. ghci> ourList
  281. [1,2,3,*** Exception: Whoops
  282. ghci> take 3 ourList
  283. [1,2,3]
  284. ghci> drop 4 ourList
  285. [5,6,7]
  286. ghci> drop 3 ourList
  287. [*** Exception: Whoops
  288. -- this proves that whenever we ask for specific thing in haskell we truely only get that
  289. -- and nothing more
  290. -- range
  291. ghci> [1..10]
  292. [1,2,3,4,5,6,7,8,9,10]
  293. ghci> [0,2..10]
  294. [0,2,4,6,8,10]
  295. ghci> [1..] -- infinite list
  296. ghci> take 3 (drop 5 [1..])
  297. [6,7,8]
  298. -- tuples
  299. -- tuples are quite different from the lists and used for different purposes
  300. -- they can carry multiple types in a single tuple whereas list can hold only one type
  301. ghci> person = (22, "Kevin") -- (id, "name")
  302. -- all the functions that we learned for list cannot be applied for tuples
  303. ghci> fst person
  304. 22
  305. ghci> snd person
  306. "Kevin"
  307. -- these functions only work on the tuples of size 2
  308. -- we can make tuples as large as 15 values but if we are creating a 15 value tuple then
  309. -- the chances are there is better solutions to the problem
  310. -- list of tuples
  311. personList = [(22, "Kevin"), (35, "Zach"), (38, "Aiden")] -- database of users with id and name
  312. ghci> lookup 35 personList
  313. Just "Zach"
  314. ghci> lookup 8 personList
  315. Nothing
  316. -- zip two list together to form a list of tuples
  317. ghci> zip [1,2,3] ["yes","no","maybe"]
  318. [(1,"yes"),(2,"no"),(3,"maybe")]
  319. ghci> [1,2,3] `zip` ["yes","no","maybe"]
  320. [(1,"yes"),(2,"no"),(3,"maybe")]
  321. -- on a final note when we create a list of tuples every tuple must contain a same types
  322. -- or else it wont work, differently constructed tuples are seen as different types
  323. [(Int, String)] -- [(1, "yes"),(2, "no")] - Correct
  324. [(String, Int)] -- [("no", 2),("yes", 1)] - Correct
  325. -- calling functions is done by putting the arguments directly after the
  326. function. There are no parenthesies as part of the function call:
  327. > succ 5
  328. 6
  329. > truncate 6.59
  330. 6
  331. > round 6.59
  332. 7
  333. > sqrt 2
  334. 1.4142135623730951
  335. > not (5 < 3)
  336. True
  337. > gcd 21 14
  338. 7
  339. -- I/O actions can be used to read from and write to the console.
  340. > putStrLn "The quick brown fox jumps over the lazy dog"
  341. The quick brown fox jumps over the lazy dog
  342. > putStr "No newline"
  343. No newline>
  344. > print (5 + 4)
  345. 9
  346. > print (1 < 2)
  347. True
  348. -- The putStr and putStrLn functions output strings to the terminal. The print
  349. function outputs any type of value. (If you print a string, it will have quotes
  350. around it.)
  351. -- If you need multiple I/O actions in one expression, you can use a do block.
  352. Actions are separated by semicolons.
  353. > do { putStr "2 + 2 =" ; print (2 + 2) }
  354. 2 + 2 = 4
  355. > do { putStrLn "ABCDE" ; putStrLn "12345" }
  356. ABCDE
  357. 12345
  358. -- Reading can be done with getLine (which gives back a String) or readLn (which
  359. gives back whatever type of value you want). The <- symbol is used to assign a
  360. name to the result of an I/O action.
  361. > do { n <- readLn ; print (n^2) }
  362. 4
  363. 16
  364. (The 4 was input. The 16 was a result.)
  365. -- There is actually another way to write do blocks. If you leave off the braces
  366. and semicolons, then indentation becomes significant. This doesn't work so well
  367. in ghci, but try putting the file in a source file (say, program.hs) and build
  368. it.
  369. main = do putStrLn "What is 2 + 2?"
  370. x <- readLn
  371. if x == 4
  372. then putStrLn "You're right!"
  373. else putStrLn "You're wrong!"
  374. -- You can build with ghc --make program.hs, and the result will be called
  375. program.
  376. -- Haskell Tutorial
  377. -- An Introduction to Haskell from a non-programmer
  378. -- DistroTube
  379. -- Single line comments are made with double dashes
  380. {- Multi-line comment made with braces and dashes.
  381. - Here is the second.
  382. - And a third !
  383. -}
  384. -- Haskell is a functional programming language.
  385. -- Everything is immutable so you cannot change it!
  386. -- Haskell is lazy which means it only executes something when it needs to.
  387. -- Documentation:
  388. -- http://learnyouahaskell.com/
  389. -- https://haskell.org
  390. -- :l file.hs (to load file.hs in ghci)
  391. -- :r reload (to reload file.hs in ghci)
  392. -- :t variabelName (to find the type of variableName)
  393. -- :!clear (to run built-in shell command from ghci)
  394. -- :q (to quit the ghci)
  395. -- Imports
  396. import Data.List
  397. -- There are five numeric types in the Haskell "prelude" (the part of the
  398. library you get without having to import anything):
  399. Int is an integer with at least 30 bits of precision
  400. Integer is an integer with unlimited precision
  401. Float is a single precision floating point number
  402. Double is a double precision floating point number
  403. Rational is a fraction type, with no rounding error
  404. -- All five are instances of the Num type class. The first two are instances of
  405. Integral, and the last three are instances of Fractional.
  406. -- Types
  407. -- Bool, Int, Integer, Float, Double, Char, [Char], Tuples ()
  408. -- :: Bool
  409. -- True or False
  410. trueAndFalse = True && False
  411. trueOrFalse = True || False
  412. notTrue = not(True)
  413. boolFive = 5 > 4
  414. -- :: Int
  415. -- Whole number: from -2^63 to 2^63
  416. maxInt = maxBound :: Int
  417. minInt = minBound :: Int
  418. -- :: Integer
  419. -- Unbounded whole number
  420. numFive :: Integer
  421. numFive = 5
  422. -- :: Float
  423. numFive' = 5.0 :: Float
  424. myFloat :: Float
  425. myFloat = 1.0 + 2.5
  426. -- :: Double
  427. -- default floating point number in Haskell is Double
  428. -- eleven point precision
  429. myFloat' = 1.0 + 2.5
  430. myDouble = 1.55555555555 + 0.00000000001
  431. -- :: Char
  432. -- Char are single characters and denoted within single quotes
  433. singleChar = 'a'
  434. myName :: [Char]
  435. myName = "Saran"
  436. myName' = ['S', 'a', 'r', 'a', 'n'] -- same as the above
  437. -- Math
  438. addNum = 3 + 6
  439. subNum = 3 - 6
  440. mulNum = 3 * 6
  441. divNum = 3 / 6
  442. modNum = mod 9 2 -- prefix operator
  443. modNum' = 9 `mod` 2 -- infix operator
  444. addNeg = 4 + (-4)
  445. -- pi, exp, log, sin, cos, tan, asin, acos, atan
  446. truncNumber = truncate myDouble -- truncating the number 1.55555555556 to 1
  447. roundNumber = round myDouble -- rounding the number 1.55555555556 to 2
  448. ceilNumber' = ceiling myDouble -- ceiling the number 1.55555555556 to 2
  449. floorNumber = floor myDouble -- flooring the number 1.55555555556 to 1
  450. -- sqrt :: Floating a => a -> a
  451. sqrtFive = sqrt numFive'
  452. -- Lists
  453. numList = [1,2,3,4,5]
  454. numList' = [1..5] -- range of numbers
  455. alphaList = ['a'..'z']
  456. evenNum = [2,4..20]
  457. oddNum' = [1,3..20]
  458. oddAlpha' = ['a','c'..'z']
  459. evenAlpha = ['b','d'..'z']
  460. sumNumList = sum [1..5]
  461. sumNumList' = sum numList
  462. prodNumList = product [1..5]
  463. prodNumList' = product numList
  464. elemList = elem 5 numList -- is element 5 part of numList
  465. elemList' = 5 `elem` numList
  466. fibNum1 = [0,1,1,2,3,5,8]
  467. fibNum2 = [13,21,34,55,89,144]
  468. fibNum' = fibNum1 ++ fibNum2
  469. maxFib = maximum fibNum'
  470. minFib = minimum fibNum'
  471. listOfList = [fibNum1, fibNum2]
  472. addListNum = zipWith (+) [1,2,3,4,5] [6,7,8,9,10]
  473. addFibList = zipWith (+) fibNum1 fibNum2
  474. infiniteNumList = [1,2..]
  475. notInfinite = take 20 infiniteNumList
  476. infiniteFive = repeat 5
  477. notInfiniteFive = take 20 infiniteFive
  478. repeatFive = replicate 20 5 -- replicate 5 for 20 times
  479. cycleFive = cycle [5]
  480. cycleFibn = cycle fibNum'
  481. takeCycle = take 50 (cycle [1,2,3,4,5,6,7,8,9,10]) -- with parentheses
  482. takeCycle' = take 50 $ cycle [1,2,3,4,5,6,7,8,9,10] -- same as the above
  483. dropFibn = drop 5 fibNum' -- drop the first 5 of the list fibNum'
  484. filterFibn = filter (>5) fibNum' -- print greater than 5 values
  485. whileFibn = takeWhile (<=89) fibNum' -- print lesser than or equal to 89 values
  486. mapList = map (*2) [1..10] -- multiply 2 with numbers 1 to 10
  487. unordList = [545,2,34,87,3,897,56,13] -- some unordered list
  488. sortList = sort unordList -- sort the unordered list
  489. firstNum = head fibNum' -- print the first value in the list
  490. restNum' = tail fibNum' -- print the values in the list but the first value
  491. lastNum' = last fibNum' -- print the last value in the list
  492. initNum' = init fibNum' -- print the values in the list but the last value
  493. prodList = foldr (*) 1 [2,3,4,5] -- same as 2 * (3 * (4 * (5 * 1)))
  494. subrList = foldl (-) 1 [2,3,4,5] -- same as (((((1) - 5) - 4) - 3) - 2)
  495. summList = foldl (+) 0 [1..100] -- same as sum [1..100]
  496. appendList = 1 : []
  497. appendList' = 1 : 2 : 3 : 4 : 5 : []
  498. -- Fibonacci Numbers generation
  499. fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci)
  500. -- [0,1] ([1]) -> [0,1,1,2] ([1,1,2]) -> ...
  501. -- Functions
  502. listFunction = [x * y | x <- [1..5], y <- [1..5]]
  503. listFunction' = [x * y | x <- [1..5], y <- [1..5], x * y `mod` 3 == 0]
  504. -- Tuples
  505. tuple' = ("Raman", 18)
  506. getName = fst tuple'
  507. getNumm = snd tuple'
  508. empName = ["John Doe", "Jane Doe", "Mary Jane", "Ben Dover"]
  509. empNumm = [1001, 1002, 1003, 1004]
  510. empList = zip empName empNumm
  511. -- simple functions
  512. summ a b = a + b
  513. prod a b = a * b
  514. func a b c = summ (prod a b) c
  515. -- program
  516. -- to compile the program: ghc program.hs
  517. -- to run inside ghci: main
  518. greeting = "Hello, World!"
  519. --main = do
  520. -- print greeting
  521. -- program to take input
  522. --main = do
  523. -- putStrLn "Enter name: "
  524. -- name <- getLine
  525. -- putStrLn ("Hello, " ++ name ++ "!")
  526. -- program with function
  527. -- Haskell is picky about spacing!
  528. lowerCase :: [Char] -> [Char]
  529. lowerCase s = [c | c <- s, elem c ['a'..'z']]
  530. main = do
  531. let s = lowerCase "Institute of Mathematical Sciences"
  532. print(s)
  533. -- list
  534. : is an infix function that takes single value on its left and a list on its right
  535. 1 : [2, 3, 4, 5, 6] -- which are of same type (prepending function)
  536. [1,2,3,4,5,6]
  537. ++ is also an infix function which appends a list to another list which are of same type
  538. [1, 2, 3] ++ [4, 5, 6]
  539. [1,2,3,4,5,6]
  540. -- quick sorting algorithm function
  541. f [] = []
  542. f (x:xs) = f ys ++ [x] ++ f zs
  543. where
  544. ys = [a | a <- xs, a <= x]
  545. zs = [b | b <- xs, b > x]
  546. -- x = 3 and xs = [1, 4, 2]
  547. f [3, 1, 4, 2] = f (3:[1, 4, 2]) = f [1, 2] ++ [3] ++ f[4]
  548. = f [] ++ [1] ++ f[2] ++ [3] ++ [4]
  549. = [] ++ [1] ++ [2] ++ [3] ++ [4]
  550. = [1, 2, 3, 4]
  551. -- type signatures
  552. Functions map input values to output values, for example, inc maps integers to
  553. integers. Thus we denote the type of inc as Int -> Int.
  554. inc :: Int -> Int -- type signature
  555. inc x = x + 1 -- function equation
  556. -- multiple arguments
  557. average :: Float -> Float -> Float
  558. average a b = (a + b) / 2.0
  559. average 3.0 4.0 = (3.0 + 4.0) / 2.0 = 3.5