Haskell Basics

learnyouahaskell.com/starting-out

45 flashcards · Programming Languages · by charlie42

  1. How to launch Haskell CLI

    ghci

  2. Issue with arithmetics and negative numbers

    Need to wrap them in parentheses: 5 * (-3)

  3. Symbols for and, or, and not

    && || not

  4. Symbol for not equal

  5. 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.

  6. What kind of function is * function in 5*5?

    Infix (between two numbers)

  7. Write a function that find maximum number between two numbers

    max 100 101

  8. Get a successor of a product of two numbers

    succ (9 * 10)

  9. Another way to write this: div 92 10

    92 `div` 10

  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

  11. How to load a script from the CLI?

    :l [name of the script without the extension] l stands for load

  12. Extension for haskell scripts

    .hs

  13. Opposite of functional languages

    Imperative languages

  14. The difference between Haskell's if statement and if statements in imperative languages

    The else part is mandatory in Haskel

  15. What's an expression in Haskell (and other languages)?

    A statement that returns a value

  16. 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

  17. What is forbidden in function names?

    Starting with a capital letter

  18. Write a function without any parameters

    myString = "String"

  19. In Haskell, lists are a homogenous data structure. What does this mean?

    You can only store objects of the same type in lists

  20. 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]

  21. "hello" is just syntactic sugar for ...

    ['h','e','l','l','o']

  22. Operator for appending two lists together

    ++

  23. What ++ is mostly used for?

    Appending strings

  24. Why ++ can be slo w?

    Haskell has to go through the whole left list every time

  25. Alternative to ++ that is instantaneous?

    Appending one value in front of a list

  26. What's the : operator called?

    Cons operator

  27. [1,2,3] is syntactic sugar for ...

    1:2:3:[]

  28. Another way to write [3]

    3:[] (prepend value 3 to an empty list)

  29. Operator to access an item from a list by index

    !!

  30. 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.

  31. Visualize the paint-drawn centipede to help remember the 4 functions on lists

  32. How to check if the list is empty

    null xs Use this function instead of xs == []

  33. Get the first 5 elements from a list

    take 5 [1,2,3,4,5,6]

  34. What can we enumerate over in ranges?

    Numbers Letters

  35. Enumeration syntax (numbers from 1 to 20)

    [1..20]

  36. 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..]

  37. Two functions that produce infinite lists (not range)

    1/ cycle 2/ repeat

  38. 3 parts of a set comprehension

    Output function Input set Predicate

  39. What does a set comprehension look like for getting all positive integers

  40. Syntax for list comprehension (double numbers from 1 to 10)

  41. List comprehension with multiple predicates

  42. 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]]

  43. 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]

  44. What will this produce:

    2*8 2*10 2*11 5*8 5*10 5*11 10*8 10*10 10*11

  45. Re-implement length using list comprehension

    length' xs = sum [ 1 | _ <- xs ]