Last active
August 5, 2016 17:51
-
-
Save FernandoBasso/0b60baeaa913becb2554dc945b43dc1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
━━━━━━━━━━━━━━━━ | |
HASKELL-NOTES1 | |
Fernando Basso | |
━━━━━━━━━━━━━━━━ | |
Table of Contents | |
───────────────── | |
1 Intro | |
.. 1.1 More stuff | |
..... 1.1.1 Even more text | |
2 Some code examples | |
.. 2.1 Example One: Doubles | |
.. 2.2 Example Two: Rectangle Area | |
1 Intro | |
═══════ | |
Haskell is purely functional. | |
No changing state. | |
“In purely functional programming you don't tell the computer what to | |
do as such but rather you tell it what stuff is. The factorial of a | |
number is the product of all the numbers from 1 to that number, the | |
sum of a list of numbers is the first number plus the sum of all the | |
other numbers, and so on. You express that in the form of functions.” | |
1.1 More stuff | |
────────────── | |
No side effects. Functions calculate something and return that value. | |
Referential Transparency: if you call the same function multiple times | |
with the same arguments, it is guaranteed to always return the same | |
result. | |
Evaluates things /lazily/ (only when needed). | |
Think of programs as a series of *transformations on data*. | |
*Statically Typed*: (types are checked at compile time instead of at | |
runtime). | |
1.1.1 Even more text | |
╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ | |
*Type Inference* | |
`.hs' extension, generally. | |
Load and reload code from a file in ghci: | |
`ghci' to start the interactive prompt, then `:load functions.hs' from | |
the same directory. Tab completion worksr; extension is not required. | |
`:reload' reloads the loaded file. | |
`:load' can be just `:l', and `:reload' can be just `:r'. | |
┌──── | |
│ 2 * (-2) -- Omitting the parenthesis is an error. | |
└──── | |
`==' means ‘equal to’. `/=' means ‘not equal to’, or ‘different | |
than’. (like `!=' in other languages). `not' negates (like `!' in | |
other languages). | |
`+' works for things that can be considered numbers (~'5'~ and ~"5"~ | |
do cannot). `==' works for things that can be compared (`5 == 5' or | |
~"foo" `= "bar"~ can be comapared, but 5 =' "5" cannot). | |
5 can act like 5.0, but 5.0 cannot act like 5 (it would lose | |
precision). | |
`*' is a function and it is used with _infix_ notation, contrary to | |
functions that are used with _prefix_ notation. | |
`5 `div` 2.5' is an error. `5 / 2.5' is not | |
Function Application (invoke/call a function) has the highest | |
precedence. | |
Syntax test: | |
Copy `~/.emacs.d/foo.el' to wherever yo want. | |
2 Some code examples | |
════════════════════ | |
May the source be with you. | |
2.1 Example One: Doubles | |
──────────────────────── | |
doubles: | |
┌──── | |
│ double num = num * 2 | |
│ quadruple someNum = double (double someNum) | |
│ square i = i * 2 | |
│ half x = x / 2 | |
└──── | |
2.2 Example Two: Rectangle Area | |
─────────────────────────────── | |
Example: Rectangle Area: | |
┌──── | |
│ areaRect l w = l * w | |
│ areaTriang b h = (b * h) / 2 | |
└──── |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment