Presentation is loading. Please wait.

Presentation is loading. Please wait.

Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka.

Similar presentations


Presentation on theme: "Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka."— Presentation transcript:

1 Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka

2 Prelude>15+5 20 Prelude>18-6 12 Prelude>5/2 2.5 Prelude>2*2 4 Prelude>3^3 27 Prelude>sin 1 0.841470984807896 Prelude>pi 3.141592653589793 Prelude> (50 * 100) - 4999 1 Prelude> 50 * 100 - 4999 1 Prelude> 50 * (100 - 4999) -244950

3 Prelude> True && False False Prelude> True && True True Prelude> False || True True Prelude> not False True Prelude> not ( True && True ) False Prelude> 5 == 5 True Prelude> 1 == 0 False Prelude> 5 /= 5 False Prelude> 5 /= 4 True Prelude> " hello " == " hello " True

4 (!!) returns the item in the list at integer position Input: [1,2,3] !! 0 Output: 1 Input: [6,5,4,3,2,1] !! 2 Output: 4 Input: "Hello" !! 1 Output: 'e'

5 ($) right-associating infix application operator (f $ x = f x), useful in continuation-passing style Input: abs $ -12 Output: 12

6 (%) The operator (%) forms the ratio of two integral numbers, reducing the fraction to terms with no common factor and such that the denominator is positive. Input: 12 % 4 Output: 3 % 1 Input: 5 % 7 Output: 5 % 7 Input: 0 % 23 Output: 0 % 1 Input: (-3) % 6 Output: -1 % 2 Input: 3 % (-6) Output: -1 % 2 Input: (-3) % (-6) Output: 1 % 2

7 (**) the power, returns the value of raising the first argument by the second one Input: 2 ** 16 Output: 65536.0 Input: 10**3 Output: 1000.0

8 (++) concatenates two lists Input: [1,2,3]++[3,2] Output: [1,2,3,3,2] Input: "Hello"++", "++"world"++"!" Output: "Hello, world!"

9 (:) inserts the first argument to a list passed as the second argument Input: 1 : [3,4,5] Output: [1,3,4,5] Input: 'a':"efg " Output: "aefg"

10 (\\) list difference (non-associative) Input: [1,2,3,4] \\ [2,3] Output: [1,4] Input: [1,1,2,2] \\ [2,3] Output: [1,1,2]

11 (!), (!!), ($), ($!), (%) (&&), (*), (**), (+), (++) (-), (.), (/), (//), (/=) (:), (:+), ( ), (>=), (>>), (>>=) (\\), (^), (^^), (||)

12 abs - wartosc bezwzgledna atan - arcus tanges ceiling - calosc + 1 cos - cosinus div - dzielenie calkowite exp - exponent floor - calosc log - logarytm max - maksimum min - minimum mod - operator reszty z dzielenia pi - wartosc pi round - zaokragla do najblizszej calkowitej sin - sinus sqrt - pierwiastek tan - tanges succ - operator ktory zwieksza argument o jeden pred - operator ktory zmniejsza argument o jeden

13 all - returns True if all items in the list fulfill the condition Input: all (<10) [1,3,5,7,9] Output: True any - returns True if at least one item in the list fulfills the condition Input: any (1==) [0,1,2,3,4,5] Output: True break - creates a tuple of two lists from the original one separated at condition boundary Input: break (3==) [1,2,3,4,5] Output: ([1,2],[3,4,5])

14 concat - accepts a list of lists and concatenates them Input: concat [[1,2,3], [1,2,3]] Output: [1,2,3,1,2,3] delete - removes the first occurrence of the specified element from its list argument Input: delete 2 [1,2,3,2,1] Output: [1,3,2,1] divMod - the function returns a tuple containing the result of integral division and modulo Input: divMod 3 5 Output: (0,3)

15 elemIndex - returns the index of the first occurrence, if any, of value in list enumFrom - returns an array of members of an enumeration starting with the argument, it is equvalent to syntax. Input: take 10 (enumFrom 'a') Output: "abcdefghij" many else...

16 In doubleMeFile.hs file write: doubleMe x = x + x Insde GHCI load your file: Prelude> :l doubleMeFile [1 of 1] Compiling Main ( doubleMeFile.hs, interpreted ) Ok, modules loaded : Main. Prelude> doubleMe 9 18 Prelude> doubleMe 8.3 16.6

17 doubleSmallNumber x = if x > 100 then x else x *2

18 Note: We can use the let keyword to define a name right in GHCI Prelude> let lostNumbers = [4,8,15,16,23,48] Prelude> lostNumbers [4,8,15,16,23, 48]

19 Note: [], [[]] and [[],[],[]] are all different things. The first one is an empty list, the seconds one is a list that contains one empty list, the third one is a list that contains three empty lists.

20 basic functions that operate on lists head takes a list and returns its head. The head of a list is basically its first element. Prelude> head [5,4,3,2, 1] 5

21 basic functions that operate on lists tail takes a list and returns its tail. In other words, it chops off a list’s head. Prelude> tail [5,4,3,2,1] [4,3,2,1]

22 basic functions that operate on lists last takes a list and returns its last element. Prelude> last [5,4,3,2,1] 1

23 basic functions that operate on lists length takes a list and returns its length, obviously Prelude> length [5,4,3,2,1] 5

24 basic functions that operate on lists reverse reverses a list. Prelude> reverse [5,4,3,2,1] [1,2,3,4, 5]

25 basic functions that operate on lists null, take, drop, maximum, minium, product, elem

26 Prelude> [1..20] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 20] Prelude> [’a ’.. ’ z ’] " abcdefghijklmnopqrstuvwxyz " Prelude> [’K ’.. ’ Z ’] " KLMNOPQRSTUVWXYZ " Prelude> [2,4..20] [2,4,6,8,10,12,14,16,18,20] Prelude> [3,6..20] [3,6,9,12,15,18]

27 A basic comprehension for a set that contains the first ten even natural numbers is S = {2 · x|x ∈ N, x ≤ 10}. The part before the pipe is called the output function, x is the variable, N is the input set and x <= 10 is the predicate. That means that the set contains the doubles of all natural numbers that satisfy the predicate. Prelude> [x *2 | x <- [1..10]] [2,4,6,8,10,12,14,16,18, 20] Prelude> [x *2 | x = 12] [12,14,16,18,20]

28 Cool, it works. How about if we wanted all numbers from 50 to 100 whose remainder when divided with the number 7 is 3?

29 Prelude> [ x | x <- [50..100], x ‘mod ‘ 7 == 3] [52,59,66,73,80,87,94]

30 If we have two lists, [2,5,10] and [8,10,11] and we want to get the products of all the possible combinations between numbers in those lists?

31 Prelude> [ x*y| x <- [2,5,10], y <- [8,10,11]] [16,20,22,40,50,55,80,100,110]


Download ppt "Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka."

Similar presentations


Ads by Google