Haskell Basics
learnyouahaskell.com/starting-out
45 flashcards · Programming Languages · by charlie42
How to launch Haskell CLI
ghci
Issue with arithmetics and negative numbers
Need to wrap them in parentheses: 5 * (-3)
Symbols for and, or, and not
&& || not
Symbol for not equal
Types usually matter, you can't do True == 5. What is one exception?
5 + 4.0 is legal 5 is sneaky and can act like an integer or a floating-point number. 4.0 can't act like an integer, so 5 is the one that has to adapt.
What kind of function is * function in 5*5?
Infix (between two numbers)
Write a function that find maximum number between two numbers
max 100 101
Get a successor of a product of two numbers
succ (9 * 10)
Another way to write this: div 92 10
92 `div` 10
In what way function definition are similar to function calls?
Functions calls: function name, followed by parameters separated by spaces. Function definition: same, followed by a =, and after that we define what the function does
How to load a script from the CLI?
:l [name of the script without the extension] l stands for load
Extension for haskell scripts
.hs
Opposite of functional languages
Imperative languages
The difference between Haskell's if statement and if statements in imperative languages
The else part is mandatory in Haskel
What's an expression in Haskell (and other languages)?
A statement that returns a value
Write a function that multiplies a number by 2, but only if that number is smaller than or equal to 100
ourFunction x = if x <= 100 then x*2 else x
What is forbidden in function names?
Starting with a capital letter
Write a function without any parameters
myString = "String"
In Haskell, lists are a homogenous data structure. What does this mean?
You can only store objects of the same type in lists
Why this won't work: ghci> lostNumbers = [4,8,15,16,23,42]
You can load functions only from files. To define a variable through the CLI you can use let: ghci> let lostNumbers = [4,8,15,16,23,42]
"hello" is just syntactic sugar for ...
['h','e','l','l','o']
Operator for appending two lists together
++
What ++ is mostly used for?
Appending strings
Why ++ can be slo w?
Haskell has to go through the whole left list every time
Alternative to ++ that is instantaneous?
Appending one value in front of a list
What's the : operator called?
Cons operator
[1,2,3] is syntactic sugar for ...
1:2:3:[]
Another way to write [3]
3:[] (prepend value 3 to an empty list)
Operator to access an item from a list by index
!!
What will this evaluate to and why? [3,2,1] > [2,10,100]
True List are compared in lexicographical order: First the heads are compared. If they are equal then the second elements are compared, etc. Here the condition is true for the first values, the rest is not evaluated.
Visualize the paint-drawn centipede to help remember the 4 functions on lists
How to check if the list is empty
null xs Use this function instead of xs == []
Get the first 5 elements from a list
take 5 [1,2,3,4,5,6]
What can we enumerate over in ranges?
Numbers Letters
Enumeration syntax (numbers from 1 to 20)
[1..20]
Two neat things we can do with ranges
1/ Custom ranges by specifying the second number in range: 2/ Infinite lists take 24 [13,26..]
Two functions that produce infinite lists (not range)
1/ cycle 2/ repeat
3 parts of a set comprehension
Output function Input set Predicate
What does a set comprehension look like for getting all positive integers
Syntax for list comprehension (double numbers from 1 to 10)
List comprehension with multiple predicates
Use list comprehension to double each value over 10 from a list of integers from 1 to 20
[if x > 10 then x * 2 else x | x <- [1..20]]
Write a function that will double each value over 10 from a given list
doubleOver10 xs = [if x > 10 then x * 2 else x | x <- xs]
What will this produce:
2*8 2*10 2*11 5*8 5*10 5*11 10*8 10*10 10*11
Re-implement length using list comprehension
length' xs = sum [ 1 | _ <- xs ]