mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-09 16:26:53 +02:00
Merge pull request #3157 from alexgrejuc/master
[haskell/en] tweaked tuple information to demonstrate wildcards and a more useful tuple function
This commit is contained in:
@@ -124,6 +124,9 @@ last [1..5] -- 5
|
|||||||
fst ("haskell", 1) -- "haskell"
|
fst ("haskell", 1) -- "haskell"
|
||||||
snd ("haskell", 1) -- 1
|
snd ("haskell", 1) -- 1
|
||||||
|
|
||||||
|
-- pair element accessing does not work on n-tuples (i.e. triple, quadruple, etc)
|
||||||
|
snd ("snd", "can't touch this", "da na na na") -- error! see function below
|
||||||
|
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
-- 3. Functions
|
-- 3. Functions
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
@@ -159,8 +162,8 @@ fib 1 = 1
|
|||||||
fib 2 = 2
|
fib 2 = 2
|
||||||
fib x = fib (x - 1) + fib (x - 2)
|
fib x = fib (x - 1) + fib (x - 2)
|
||||||
|
|
||||||
-- Pattern matching on tuples:
|
-- Pattern matching on tuples
|
||||||
foo (x, y) = (x + 1, y + 2)
|
sndOfTriple (_, y, _) = y -- use a wild card (_) to bypass naming unused value
|
||||||
|
|
||||||
-- Pattern matching on lists. Here `x` is the first element
|
-- Pattern matching on lists. Here `x` is the first element
|
||||||
-- in the list, and `xs` is the rest of the list. We can write
|
-- in the list, and `xs` is the rest of the list. We can write
|
||||||
@@ -207,7 +210,7 @@ foo 5 -- 60
|
|||||||
-- to a given parameter. In contrast to standard function application, which
|
-- to a given parameter. In contrast to standard function application, which
|
||||||
-- has highest possible priority of 10 and is left-associative, the `$` operator
|
-- 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
|
-- has priority of 0 and is right-associative. Such a low priority means that
|
||||||
-- the expression on its right is applied as the parameter to the function on its left.
|
-- the expression on its right is applied as a parameter to the function on its left.
|
||||||
|
|
||||||
-- before
|
-- before
|
||||||
even (fib 7) -- false
|
even (fib 7) -- false
|
||||||
|
Reference in New Issue
Block a user