mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[haskell/en] Minor changes that were bothering me (#5207)
This commit is contained in:
parent
c8af038bda
commit
d9225142d0
@ -66,6 +66,7 @@ True || False -- True
|
|||||||
['H', 'e', 'l', 'l', 'o'] -- "Hello"
|
['H', 'e', 'l', 'l', 'o'] -- "Hello"
|
||||||
|
|
||||||
-- Lists can be indexed with the `!!` operator followed by an index
|
-- Lists can be indexed with the `!!` operator followed by an index
|
||||||
|
-- but this is an O(n) operation because lists are linked lists
|
||||||
"This is a string" !! 0 -- 'T'
|
"This is a string" !! 0 -- 'T'
|
||||||
|
|
||||||
|
|
||||||
@ -273,7 +274,7 @@ case args of
|
|||||||
map (*2) [1..5] -- [2, 4, 6, 8, 10]
|
map (*2) [1..5] -- [2, 4, 6, 8, 10]
|
||||||
|
|
||||||
-- you can make a for function using map
|
-- you can make a for function using map
|
||||||
for array func = map func array
|
for list func = map func list
|
||||||
|
|
||||||
-- and then use it
|
-- and then use it
|
||||||
for [0..5] $ \i -> show i
|
for [0..5] $ \i -> show i
|
||||||
@ -281,6 +282,9 @@ for [0..5] $ \i -> show i
|
|||||||
-- we could've written that like this too:
|
-- we could've written that like this too:
|
||||||
for [0..5] show
|
for [0..5] show
|
||||||
|
|
||||||
|
-- filter keeps only the elements in a list that satisfy a condition
|
||||||
|
filter even [1..10] -- [2, 4, 8, 10]
|
||||||
|
|
||||||
-- You can use foldl or foldr to reduce a list
|
-- You can use foldl or foldr to reduce a list
|
||||||
-- foldl <fn> <initial value> <list>
|
-- foldl <fn> <initial value> <list>
|
||||||
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43
|
foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43
|
||||||
|
Loading…
Reference in New Issue
Block a user