Created
June 14, 2011 19:39
-
-
Save matagus/1025683 to your computer and use it in GitHub Desktop.
Learning Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- this a single line comment | |
-- | |
{- and this | |
- is a multiline | |
- comment | |
- in haskell code -} | |
a = "my string" | |
b = ['m', 'y', 's', 't', 'i', 'n', 'g'] | |
c = 'c' -- this is a character | |
s = "My long\ | |
string" | |
-- numbers | |
-- | |
1 | |
1.0 | |
1e10 | |
0o1 | |
0O1 | |
0x1 | |
0X1 | |
-- enumerations | |
-- | |
[1..10] | |
[1..] | |
[110..100] -- empty list | |
[110,109..100] -- from 110 to 100 step -1 | |
[0, -1..] -- natural negative integers | |
[1,3..99] -- from 1 to 99 step 2 | |
['a' .. 'z'] -- any Enum allow this behaviour | |
-- lists: must have same-type elements | |
-- | |
[] -- empty list | |
-- these two lists are identical | |
[1, 2, 3] | |
1 : 2 : 3 : [] | |
-- and this expressions are too | |
"abc" | |
'a' : 'b' : 'c' : [] | |
-- tuples are like lists, but allow heterogenous elements | |
-- | |
(head, tail, 3, 'a') | |
-- Indentation: The general rule is: always indent. When | |
-- the compiler complains, indent more. | |
-- Braces represent scope: they can be sused after | |
-- where, do, let and of | |
square2 x = result | |
where { result = x * x } | |
-- Semicolons terminate expressions: | |
-- | |
-- Function definitions: always indent the body | |
-- using at least one space from the function name | |
square2 x = | |
x * x | |
-- unless a where clause is present | |
square2 x = | |
x2 | |
where x2 = | |
x * x | |
-- or using let <exp1> in <exp2> | |
square2 x = | |
let x2 = | |
x * x | |
in x2 | |
-- Function names must start with a lowercase letter or an underscore | |
-- Pattern matching: this is: multiple 'clauses' of a function | |
-- can be *defined* by pattern-matching on the values of the arguments: | |
factorial 0 = 1 | |
beginswith 'y':_ = "this word begins with y!" | |
beginswith _ = "oh! we don't know more" -- the underscore matches every other value not matched in previous clauses | |
-- or we may specify a parameter so we can bind its value and use it in the second member: | |
beginswith str = "We don't know anything about " ++ str | |
-- Boolean functions can be used as “guards” | |
-- in function definitions along with pattern matching: | |
which n | |
| n == 0 = "zero!" | |
| even n = "even!" | |
| otherwise = "odd!" | |
-- A list comprehension consists of four types of elements: | |
-- generators, guards, local bindings, and targets. A list | |
-- comprehension creates a list of target values based on | |
-- the generators and guards given. | |
-- * Guards allow certain elements to be excluded. | |
-- * Local bindings provide new definitions for use in | |
-- the generated expression or subsequent genera- | |
-- tors and guards. | |
-- * local bindings with let <var> = <exp> | |
-- * taget values are the results generated | |
squares = [x * x | x <- [1..10]] | |
-- and now using them to define a function: | |
divisors n = | |
[d | d <- [1..(n `div` 2)], | |
n `mod` d == 0] | |
strange = [(a,z) | a <-[1..3], | |
b <-[1..3], | |
c <- [1..3], | |
let z = min a b, | |
z < c] | |
-- Operators are simply functions that take two arguments | |
-- and have special syntactic support. Any so-called | |
-- operator can be applied as a prefix function using | |
-- parentheses: | |
-- | |
3 + 4 == (+) 3 4 | |
-- To define a new operator, simply define it as a nor- | |
-- mal function, except the operator appears between | |
-- the two arguments: | |
first ## last = | |
... | |
-- Allowed symbols which can be used as operators are: | |
-- # $ % & * + . / < = > ? @ \ ^ | - ~ | |
-- However, there are several “operators” which can- | |
-- not be redefined. They are: <-, -> and = | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment