mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 15:51:41 +03:00
[haskell/en] some minor fixes (#2550)
* [haskell/en] some minor fixes * Minor fixes after comments from @vendethiel
This commit is contained in:
parent
5d6fe5601e
commit
d064763e95
@ -68,7 +68,7 @@ not False -- True
|
||||
----------------------------------------------------
|
||||
|
||||
-- Every element in a list must have the same type.
|
||||
-- These two lists are the same:
|
||||
-- These two lists are equal:
|
||||
[1, 2, 3, 4, 5]
|
||||
[1..5]
|
||||
|
||||
@ -77,11 +77,11 @@ not False -- True
|
||||
|
||||
-- You can create a step in a range.
|
||||
[0,2..10] -- [0, 2, 4, 6, 8, 10]
|
||||
[5..1] -- This doesn't work because Haskell defaults to incrementing.
|
||||
[5..1] -- [] (Haskell defaults to incrementing)
|
||||
[5,4..1] -- [5, 4, 3, 2, 1]
|
||||
|
||||
-- indexing into a list
|
||||
[1..10] !! 3 -- 4
|
||||
[1..10] !! 3 -- 4 (zero-based indexing)
|
||||
|
||||
-- You can also have infinite lists in Haskell!
|
||||
[1..] -- a list of all the natural numbers
|
||||
@ -152,8 +152,8 @@ fib x
|
||||
| otherwise = fib (x - 1) + fib (x - 2)
|
||||
|
||||
-- Pattern matching is similar. Here we have given three different
|
||||
-- definitions for fib. Haskell will automatically call the first
|
||||
-- function that matches the pattern of the value.
|
||||
-- equations for fib. Haskell will automatically use the first
|
||||
-- equation whose left hand side pattern matches the value.
|
||||
fib 1 = 1
|
||||
fib 2 = 2
|
||||
fib x = fib (x - 1) + fib (x - 2)
|
||||
@ -198,11 +198,11 @@ foo 5 -- 15
|
||||
-- multiplies the result of that by 4, and then returns the final value.
|
||||
foo = (4*) . (10+)
|
||||
|
||||
-- 4*(10 + 5) = 60
|
||||
-- 4*(10+ 5) = 60
|
||||
foo 5 -- 60
|
||||
|
||||
-- fixing precedence
|
||||
-- Haskell has another operator called `$`. This operator applies a function
|
||||
-- Haskell has an operator called `$`. This operator applies a function
|
||||
-- to a given parameter. In contrast to standard function application, which
|
||||
-- has highest possible priority of 10 and is left-associative, the `$` operator
|
||||
-- has priority of 0 and is right-associative. Such a low priority means that
|
||||
@ -244,10 +244,10 @@ double x = x * 2
|
||||
-- 6. Control Flow and If Expressions
|
||||
----------------------------------------------------
|
||||
|
||||
-- if expressions
|
||||
-- if-expressions
|
||||
haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome"
|
||||
|
||||
-- if expressions can be on multiple lines too, indentation is important
|
||||
-- if-expressions can be on multiple lines too, indentation is important
|
||||
haskell = if 1 == 1
|
||||
then "awesome"
|
||||
else "awful"
|
||||
@ -295,11 +295,10 @@ data Color = Red | Blue | Green
|
||||
|
||||
-- Now you can use it in a function:
|
||||
|
||||
|
||||
say :: Color -> String
|
||||
say Red = "You are Red!"
|
||||
say Blue = "You are Blue!"
|
||||
say Green = "You are Green!"
|
||||
say Red = "You are Red!"
|
||||
say Blue = "You are Blue!"
|
||||
say Green = "You are Green!"
|
||||
|
||||
-- Your data types can have parameters too:
|
||||
|
||||
@ -384,8 +383,8 @@ main'' = do
|
||||
-- The type `IO` is an example of a "monad". The way Haskell uses a monad to
|
||||
-- do IO allows it to be a purely functional language. Any function that
|
||||
-- interacts with the outside world (i.e. does IO) gets marked as `IO` in its
|
||||
-- type signature. This lets us reason about what functions are "pure" (don't
|
||||
-- interact with the outside world or modify state) and what functions aren't.
|
||||
-- type signature. This lets us reason about which functions are "pure" (don't
|
||||
-- interact with the outside world or modify state) and which functions aren't.
|
||||
|
||||
-- This is a powerful feature, because it's easy to run pure functions
|
||||
-- concurrently; so, concurrency in Haskell is very easy.
|
||||
|
Loading…
Reference in New Issue
Block a user