ple

Haskell Reinforcement Practice

Here are a set of problems designed to help you reinforce and retain some useful Haskell knowledge. If you are an Anki fan, consider adding some of these questions into a deck. 😀

  1. Who created Haskell and in what year?

  2. Is Haskell statically or dynamically typed? Strongly or weakly typed? Manifest or inferential?

  3. Haskell is statically typed. What does that mean?

  4. Haskell is a lazy language. What does that mean?

  5. Haskell functions are pure. What are pure functions?

  6. Give five advantages of pure functions over nonpure functions.

  7. Why do we never use the term assignment in Haskell? What do we use instead?

  8. What is the most popular Haskell REPL?

  9. How do you write a “Hello world” Haskell program as a script to be run on the command line?

  10. Why is the Haskell “Hello world” program not simply putStrLn("Hello, world")?

  11. Assuming the file hello.hs contains a (command line based) Haskell program, how do you compile and run the program using ghc?

  12. What does Haskell use instead of curly braces for structure?

  13. Which of the following are legal ways of applying the function f to the argument x: f(x), (f x), (f)x, f x?

  14. To compute the minium value of 3 and 5 we write min 3 5. Can we write min 3 5 8? Why or why not?

  15. To compute the minium value of 3 and 5 we write min 3 5. Can we write min 3? What does this expression mean?

  16. What does the function succ do? What is its type signature? (Hint: you’ll have to know something about typeclasses here.)

  17. Write f $ g $ h $ x without using the $ operator.

  18. Rewrite the expression f(f(g(h(g(x)y))z)) replacing as many parentheses with $ as possible.

  19. Rewrite the definition f x = g(h(g(x))) in point-free form.

  20. Why did the designers of Haskell choose to put the type signatures on a separate line of code instead of embedding the type information into the parameter list as is done in Go, Rust, Swift, C++, C#, Java, and friends?

  21. Define (i.e., give a function definition for) the Haskell function that accepts a number and returns the value of 10 plus that number. Include a type signature in your definition.

  22. Give the Haskell function that accepts a number and returns the value of 10 plus that number, as a function expression.

  23. Haskell‘s precedence relationship between infix operators and prefix operators is very simple. What is it?

  24. How is the multipication operator used in prefix position? Give the expression that multiplies x and y using this prefix form.

  25. Given the definition doubledThenPlus x y = 2 * x + y, give the expression that applies doubledThenPlus to a and b in infix form.

  26. What is the difference between the expressions (/) 2 and (/ 2)?

  27. Why are (/ 2) and (/) 2 different functions, but (elem 21) and (elem) 21 are the same function?

  28. What is inferred as the type of f in the definition f x y z = (x == y) && (z < z)?

  29. What is the inferred type of the expression \(x, y) -> tan x < 5 || y?

  30. Haskell type inference is far more extensive than that of Java, Go, Rust, or Swift. What kinds of expressions can Haskell infer types for that these other languages cannot?

  31. Can a function take multiple arguments in Haskell? Why or why not? If not explain how one simulates a function of no arguments and one of multiple arguments?

  32. What are the six basic types?

  33. What is the difference between Int and Integer?

  34. What does 0.1 + 0.2 evaluate to? What does (toRational 1 / 10) + (toRational 2 / 10) evaluate to?

  35. If a type belongs to the typeclass Fractional does it also belong to the typeclass Floating? Why or why not?

  36. What do the Real and Fractional typeclasses represent?

  37. What does Haskell infer for the type of [2, 3.5]? What does it infer for the type of [2, pi]? Why?

  38. Why aren’t function types in the Eq or Show typeclass?

  39. The tuple type (a,b) is in the Eq typeclass. So does that mean a type which is a tuple a functions would be equatable? Why or why not?

  40. What is the difference between 'a' and "a"? (Show the types of each expression.)

  41. Give four reasons why you should always write type signatures.

  42. What are the elements of type Bool?

  43. What is the “not-equals” operator in Haskell?

  44. What is the difference between the type of an expression being a versus A?

  45. Is () a type? Why or why not? If so, what are the members of this type? If not, what is it?

  46. What is the meaning of uncurry (+)?

  47. Identify the bound and free variables in \x -> \(y, x) -> x - 5 * sin z / p.

  48. Describe in English each of the following sections: (>=0), (`mod` 2), (0-), and (++"s").

  49. Is the function composition operator (.) left-associative or right-associative or neither?

  50. What is the type of the composition operator (.)? (Try to answer this on your own without looking it up.)

  51. The expression let x=E1 in E2 is actually the same as what function application expression?

  52. What is meant by saying lists are homogeneous while tuples are heterogeneous?

  53. In Python, tuples are immutable but lists are mutable. What is the situation in Haskell?

  54. Is the expression [[False, False], [True], []] legal?

  55. Given the list a = [10, 20, 30, 40], what are head a, tail a, last a, and init a?

  56. Does the list expression [x..y] include or exclude y? If inclusive, how do you write an exlcusive expression? If exclusive, how do you write an inclusive one?

  57. How do you write the equivalent of the Python expression [5] * 10 in Haskell?

  58. What is the more common way to write 10:20:30:40:[]?

  59. What does the : operator do, exactly? Is it proper to call it a constructor?

  60. The definition size a = if null a then 0 else 1 + size(tail a) works but is not very Haskell like! Rewrite this definition in the preferred style.

  61. The takeWhile function is totally cool and you should use it whenever it makes sense. But prove that you understand how it works: define the function recursively, with cases. (I think this question is important.)

  62. Give an operator section to extract the third element of a list.

  63. Is Haskell list equality more like Python or JavaScript? How so?

  64. Show how foldl (-) 1 [3,5,2,8] and foldr (-) 1 [3,5,2,8] are computed.

  65. How is the expression sort a++b interpreted?

  66. Why couldn’t Haskell’s designers bring themselves to use + for list concatentation, even if they wanted to?

  67. How do you write the infinite list of all ones?

  68. How do you write the infinite lists of negative numbers divisible by 3?

  69. Does take work with infinite lists? Does drop?

  70. How do you get the code point of a character? How do you get the character corresponding to a code point?

  71. How do you uppercase a string?

  72. Given a list of strings and a character, how do you make a big string by joining all the strings, in order, with the character?

  73. Haskell doesn’t have something called flatMap, but it has something equivalent. Show how to use this to write a function that doubles every letter in a string, e.g., that takes in "abc" and produces "aabbcc".

  74. Write a guard expression that produces "LESS" if the fist element of a tuple is less than its second, "EQUAL" if the two elements are the same, and "GREATER" otherwise.

  75. What would be the type signature of a function that takes in a tuple and produces "LESS" if the fist element of a tuple is less than its second, "EQUAL" if the two elements are the same, and "GREATER" otherwise.

  76. What should functions that “search” or “lookup” values produce?

  77. How is the Maybe datatype defined?

  78. What would be a reasonable type signature for a function that accepts a list of numbers and either sucessfully computes a result which is a number, and can fail in three different ways (where only an error message is needed)?

  79. Define a datatype consisting entirely of the members LOW, NORMAL, and HIGH.

  80. Define a datatype for a tree in which every node has at least 0 children and at most 3 children.

  81. Define a datatype for a tree in which every node can have an arbitrary number of children. Also write a function that computes the size (number of nodes) of such a tree.

  82. Define a datatype for the components of a mini-programming language of expressions. These components, called tokens are either: the plus operator, the times operator, a left parenthesis, a right parenthesis, a number (in which case you want to record the numeric value), or a variable (in which case you want to record its name).

  83. The expression take 5 a create a new list containing the first 5 elements of a. Are the elements of the original list copied or not?

  84. Haskell does not provide for mutable data structures. Instead, Haskell datatypes employ _________ data structures.

  85. Given a = [10,20,30,30,40,30], draw a picture of the world after b = delete 30 a that shows you understand persistent data structures. (delete is from Data.List).

  86. Suppose a was a very long list. Why is 5:a fast but a ++ [5] slow?

  87. Monads are types whose values are wrapped values from an underlying type. The function on monads that wrap a value must be a left and right ______ to an ______ composition operator.

  88. What is the type signature of >>=? What does this operator do, exactly?

  89. What is the difference between >>= and >>?

  90. Write x >> y >> z in do-notation.

  91. Write x >>= y >>= z in do-notation.

  92. What is the mondaic composition operator for the Haskell List type?

  93. Haskell is supposedly a pure functional language but real programs need effects, random numbers, and I/O. How does Haskell do these things, if at all?

  94. What is the type signature of getLine? What does it do?

  95. Write a Haskell script (command line driven program) that writes the values 1 through 100 on a separate line. Explain how Haskell separates the pure and impure parts of this program.

  96. What is the name of the type system that Haskell shares with all the ML family of languages?