You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using a `scanl`, the final result will be in the last element of the resulting list while a `scanr` will place the result in the head.
797
797
798
798
Scans are used to monitor the progression of a function that can be implemented as a fold.
799
-
Let's answer us this question: **How many elements does it take for the sum of the square roots of all natural numbers to exceed 1000?**
799
+
Let's answer us this question: **How many elements does it take for the sum of the square roots of all natural numbers to exceed $1000$?**
800
800
To get the square roots of all natural numbers, we just do `map sqrt [1..]`.
801
801
Now, to get the sum, we could do a fold, but because we're interested in how the sum progresses, we're going to do a scan.
802
-
Once we've done the scan, we just see how many sums are under 1000.
803
-
The first sum in the scanlist will be 1, normally.
804
-
The second will be 1 plus the square root of 2.
805
-
The third will be that plus the square root of 3.
806
-
If there are X sums under 1000, then it takes X+1 elements for the sum to exceed 1000.
802
+
Once we've done the scan, we just see how many sums are under $1000$.
803
+
The first sum in the scanlist will be $1$, normally.
804
+
The second will be $1 + \sqrt 2$.
805
+
The third will be $1 + \sqrt 2 + \sqrt 3$.
806
+
If there are $n$ sums under 1000, then it takes $n + 1$ elements for the sum to exceed $1000$.
807
+
808
+
807
809
808
810
```{.haskell:hs}
809
811
sqrtSums :: Int
@@ -820,7 +822,7 @@ ghci> sum (map sqrt [1..130])
820
822
```
821
823
822
824
We use `takeWhile` here instead of `filter` because `filter` doesn't work on infinite lists.
823
-
Even though we know the list is ascending, `filter` doesn't, so we use `takeWhile` to cut the scanlist off at the first occurrence of a sum greater than 1000.
825
+
Even though we know the list is ascending, `filter` doesn't, so we use `takeWhile` to cut the scanlist off at the first occurrence of a sum greater than $1000$.
824
826
825
827
## Function application with $ {#function-application}
0 commit comments