From 073d64a5e705baa3e7d9b1e02efee9f20167b1ef Mon Sep 17 00:00:00 2001 From: Anton Kalinin Date: Tue, 21 Dec 2021 09:11:54 +0300 Subject: [PATCH 1/6] jun3/haskell: fix questions and add some new --- backend/junior-3/haskell.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/junior-3/haskell.md b/backend/junior-3/haskell.md index a3981a39..13cd44e2 100644 --- a/backend/junior-3/haskell.md +++ b/backend/junior-3/haskell.md @@ -44,7 +44,7 @@ * [Introduction to Tagless Final](https://serokell.io/blog/2018/12/07/tagless-final) -## TypeOperators and type classes extensions: +## TypeOperators and type classes extensions * `TypeOperators` * `FlexibleContexts` & `FlexibleInstances` @@ -127,7 +127,7 @@ * `ScopedTypeVariables` * What is the main goal of this extension? * Higher ranked types - * What is a "rank" of a function? + * What is a higher rank function? * Give examples of rank-1, rank-2, rank-3 functions. * What is the main goal of `RankNTypes` extension? * Can we create datatypes and newtypes with `RankNTypes`? @@ -197,17 +197,18 @@ * `1:(thunk)` * `1:2:(thunk)` * `1:2:3:[]` + * `thunk:thunk` * `15` * `\x -> x * 2` * `(\x -> x + 1) 3` * Can haskell evaluate in strict mode? -* Why strict functions in Haskell evaluate values to WHNF and not NF? * What is the function `seq` (and operator `$!`)? * What is the function `deepseq` (and operator `$!!`)? * Could using `seq` change the returned value of the function? +* What is the expression result: ``(a + b) `seq` ((a + b) : list)``? Why? * What is the GHC extension `BangPatterns`? * Make examples when bang pattern is useless. - * Make examples when bang pattern has less power, than it could be supposed. + * What form does the bang pattern force values to be calculated to? * Show the difference between this two definitions: * `f1` @@ -223,14 +224,16 @@ f2 x False = False ``` - * Does bang patterns force execution, when they are nested in constructors inside of `let` or `where` expressions? - * There is a `!` inside a `Maybe`, which is inside a `let` expression, and `let` expressions are lazy: + * Do bang patterns force execution when they are nested in constructors inside + `let` or `where` expressions? Does this force `a` to be evaluated and when + it does? ```haskell let (Just !a) = x in 1 + 1 ``` * What are the GHC extensions `Strict` and `StrictData`? +* What form do Haskell functions calculate values to with `Strict` extension enabled? Why not to another form? #### Resources From 576b9024cb3a5762cd7532e6725b3680d3e7603f Mon Sep 17 00:00:00 2001 From: Anton Kalinin Date: Wed, 22 Dec 2021 16:40:30 +0300 Subject: [PATCH 2/6] fixup! jun3/haskell: fix questions and add some new --- backend/junior-3/haskell.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/backend/junior-3/haskell.md b/backend/junior-3/haskell.md index 13cd44e2..aec2be4c 100644 --- a/backend/junior-3/haskell.md +++ b/backend/junior-3/haskell.md @@ -202,6 +202,8 @@ * `\x -> x * 2` * `(\x -> x + 1) 3` * Can haskell evaluate in strict mode? +* Why are values in Haskell typically (when using bang patterns, `Strict`, + `seq`, `foldl'`, `modifyIORef'` etc) calculated to WHNF, not to NF? * What is the function `seq` (and operator `$!`)? * What is the function `deepseq` (and operator `$!!`)? * Could using `seq` change the returned value of the function? @@ -225,15 +227,15 @@ ``` * Do bang patterns force execution when they are nested in constructors inside - `let` or `where` expressions? Does this force `a` to be evaluated and when + `let` or `where` expressions? Does this force `a` to be evaluated or when it does? ```haskell let (Just !a) = x in 1 + 1 + let (Just !a) = x in [a] ``` * What are the GHC extensions `Strict` and `StrictData`? -* What form do Haskell functions calculate values to with `Strict` extension enabled? Why not to another form? #### Resources From c6c18b819258be3b184b0909a4d6e8e28e7241f6 Mon Sep 17 00:00:00 2001 From: Anton Kalinin Date: Wed, 22 Dec 2021 17:11:56 +0300 Subject: [PATCH 3/6] fixup! jun3/haskell: fix questions and add some new --- backend/junior-3/haskell.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/junior-3/haskell.md b/backend/junior-3/haskell.md index aec2be4c..7e0deb48 100644 --- a/backend/junior-3/haskell.md +++ b/backend/junior-3/haskell.md @@ -202,8 +202,8 @@ * `\x -> x * 2` * `(\x -> x + 1) 3` * Can haskell evaluate in strict mode? -* Why are values in Haskell typically (when using bang patterns, `Strict`, - `seq`, `foldl'`, `modifyIORef'` etc) calculated to WHNF, not to NF? +* Why are values in Haskell typically (when using `Strict`, `seq`, `foldl'`, + `modifyIORef'` etc) calculated to WHNF, not to NF? * What is the function `seq` (and operator `$!`)? * What is the function `deepseq` (and operator `$!!`)? * Could using `seq` change the returned value of the function? From 79e1635013988eaa36113a12213ffb331e386823 Mon Sep 17 00:00:00 2001 From: Anton Kalinin Date: Thu, 23 Dec 2021 19:11:21 +0300 Subject: [PATCH 4/6] fixup! jun3/haskell: fix questions and add some new --- backend/junior-3/haskell.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/junior-3/haskell.md b/backend/junior-3/haskell.md index 7e0deb48..30975f38 100644 --- a/backend/junior-3/haskell.md +++ b/backend/junior-3/haskell.md @@ -207,7 +207,9 @@ * What is the function `seq` (and operator `$!`)? * What is the function `deepseq` (and operator `$!!`)? * Could using `seq` change the returned value of the function? -* What is the expression result: ``(a + b) `seq` ((a + b) : list)``? Why? +* In which way does runtime behavior of the following expressions differ? + * ``(a + b) `seq` ((a + b) : list)`` + * ``let s = a + b in s `seq` (s : list)`` * What is the GHC extension `BangPatterns`? * Make examples when bang pattern is useless. * What form does the bang pattern force values to be calculated to? From bbbf19296cc8f9fc4ab3b96eda5aab41a06a8b49 Mon Sep 17 00:00:00 2001 From: Anton Kalinin Date: Thu, 23 Dec 2021 19:31:31 +0300 Subject: [PATCH 5/6] fixup! jun3/haskell: fix questions and add some new --- backend/junior-3/haskell.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/junior-3/haskell.md b/backend/junior-3/haskell.md index 30975f38..84c67055 100644 --- a/backend/junior-3/haskell.md +++ b/backend/junior-3/haskell.md @@ -208,8 +208,8 @@ * What is the function `deepseq` (and operator `$!!`)? * Could using `seq` change the returned value of the function? * In which way does runtime behavior of the following expressions differ? - * ``(a + b) `seq` ((a + b) : list)`` - * ``let s = a + b in s `seq` (s : list)`` + * ``a + b `seq` (a + b) : list`` + * ``let s = a + b in s `seq` s : list`` * What is the GHC extension `BangPatterns`? * Make examples when bang pattern is useless. * What form does the bang pattern force values to be calculated to? From 22766f5dc055d7e69929e53144b9b08557acc1d7 Mon Sep 17 00:00:00 2001 From: Anton Kalinin Date: Tue, 22 Mar 2022 12:31:58 +0300 Subject: [PATCH 6/6] fixup! jun3/haskell: fix questions and add some new --- backend/junior-3/haskell.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/junior-3/haskell.md b/backend/junior-3/haskell.md index 84c67055..fc80a50d 100644 --- a/backend/junior-3/haskell.md +++ b/backend/junior-3/haskell.md @@ -210,10 +210,11 @@ * In which way does runtime behavior of the following expressions differ? * ``a + b `seq` (a + b) : list`` * ``let s = a + b in s `seq` s : list`` +* What are conditions when `seq` or `deepseq` can actually evaluate a value? * What is the GHC extension `BangPatterns`? - * Make examples when bang pattern is useless. - * What form does the bang pattern force values to be calculated to? - * Show the difference between this two definitions: + * Make examples when a bang pattern is useless. + * Do bang patterns force values to WHNF or NF? + * Show the difference between these two definitions: * `f1` ```haskell @@ -267,6 +268,8 @@ * [Brief normal forms explanation with Haskell - Medium article](https://medium.com/@aleksandrasays/brief-normal-forms-explanation-with-haskell-cd5dfa94a157) * [All About Strictness. - FP Complete](https://www.fpcomplete.com/blog/2017/09/all-about-strictness) * [Does a function in Haskell always evaluate its return value? - Stackoverflow](https://stackoverflow.com/questions/27685224/does-a-function-in-haskell-always-evaluate-its-return-value) + * See "Learning to use `seq`", + [Real World Haskell](http://book.realworldhaskell.org/read/functional-programming.html) book. ## Lists