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
807
808
808
```{.haskell:hs}
809
809
sqrtSums :: Int
@@ -820,7 +820,7 @@ ghci> sum (map sqrt [1..130])
820
820
```
821
821
822
822
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.
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$.
824
824
825
825
## Function application with $ {#function-application}
Copy file name to clipboardExpand all lines: source_md/starting-out.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -884,7 +884,7 @@ ghci> triangles = [ (a,b,c) | c <- [1..10], a <- [1..10], b <- [1..10] ]
884
884
We're just drawing from three lists and our output function is combining them into a triple.
885
885
If you evaluate that by typing out `triangles` in GHCi, you'll get a list of all possible triangles with sides under or equal to 10.
886
886
Next, we'll add a condition that they all have to be right triangles.
887
-
We'll also modify this function by taking into consideration that side *a* isn't larger than the hypotenuse and that side *b* isn't larger than side *a*.
887
+
We'll also modify this function by taking into consideration that side $a$ isn't larger than the hypotenuse and that side $b$ isn't larger than side $a$.
888
888
889
889
```{.haskell: .ghci}
890
890
ghci> rightTriangles = [ (a,b,c) | c <- [1..10], a <- [1..c], b <- [1..a], a^2 + b^2 == c^2]
0 commit comments