summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAlex Grejuc <grejuca@oregonstate.edu>2018-07-10 15:12:23 -0700
committerAlex Grejuc <grejuca@oregonstate.edu>2018-07-10 15:12:23 -0700
commit8c30522d58e6c006274952a75c5acd4d104c8828 (patch)
tree9056bfd8332613b3ed99bc061328a0cbeb8d9495
parent0d211d341913305bd1d97e5d097898eb7faa1354 (diff)
added info about tuples, integrated wild card use into a function definition
-rw-r--r--haskell.html.markdown15
1 files changed, 9 insertions, 6 deletions
diff --git a/haskell.html.markdown b/haskell.html.markdown
index 266cf11b..cad036f1 100644
--- a/haskell.html.markdown
+++ b/haskell.html.markdown
@@ -124,6 +124,9 @@ last [1..5] -- 5
fst ("haskell", 1) -- "haskell"
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 to get around this
+
----------------------------------------------------
-- 3. Functions
----------------------------------------------------
@@ -159,8 +162,8 @@ fib 1 = 1
fib 2 = 2
fib x = fib (x - 1) + fib (x - 2)
--- Pattern matching on tuples:
-foo (x, y) = (x + 1, y + 2)
+-- Pattern matching on tuples, using wild card (_) to bypass naming an unused value
+sndOfTriple (_, y, _) = y
-- Pattern matching on lists. Here `x` is the first element
-- in the list, and `xs` is the rest of the list. We can write
@@ -203,9 +206,9 @@ foo = (4*) . (10+)
foo 5 -- 60
-- fixing precedence
--- 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
+-- 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
-- the expression on its right is applied as the parameter to the function on its left.
@@ -223,7 +226,7 @@ even . fib $ 7 -- false
-- 5. Type signatures
----------------------------------------------------
--- Haskell has a very strong type system, and every valid expression has a type.
+-- Haskell has a very strong type system, and every valid expression has a type.
-- Some basic types:
5 :: Integer