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

Slides:



Advertisements
Similar presentations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Advertisements

Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
1 Programming Languages and Paradigms Lisp Programming.
Pemrograman Dasar - Data Types1 OPERATOR. Pemrograman Dasar - Data Types2 Arithmetic operator  + - * /  / operator denotes integer division if both.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
Haskell Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
732A44 Programming in R.  Self-studies of the course book  2 Lectures (1 in the beginning, 1 in the end)  Labs (computer). Compulsory submission of.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Operators, Functions and Modules1 Pattern Matching & Recursion.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Overview of the Haskell 98 Programming Language
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Geoff Holmes Date Math Weighted Distr Strings String methods Tokenizers System Examples Utility Classes (Chapter 17) import java.util.*;
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
ICS 3U Math Class. ActionScript 3 – Math Class Rounding ceil – closest integer >= number (rounds up) floor – closest integer
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
CIS Intro to JAVA Lecture Notes Set 5 26-May-05.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
H ASKELL Session 2 Ronald L. Ramos Proglan DLS-CSB 2 nd Term SY
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
CSE 110: Programming Language I Afroza Sultana UB 1230.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Polymorphic Functions
dr Robert Kowalczyk WMiI UŁ
Types CSCE 314 Spring 2016.
Haskell Chapter 1, Part II.
Functions and patterns
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Functional Programming
Haskell.
Higher-Order Functions
PROGRAMMING IN HASKELL
Installation and exercises
Computer Science 312 Haskell Lists 1.
Clojure to Haskell (It’s mostly syntax).
Higher Order Functions
PROGRAMMING IN HASKELL
4TC00 Model-based systems engineering 2.2 Types and values
Topics Sequences Introduction to Lists List Slicing
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
CSE 3302 Programming Languages
CHAPTER 3: String And Numeric Data In Python
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
Topics Sequences Introduction to Lists List Slicing
Functional Programming
Functions and patterns
PROGRAMMING IN HASKELL
Presentation transcript:

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

Prelude> Prelude> Prelude>5/2 2.5 Prelude>2*2 4 Prelude>3^3 27 Prelude>sin Prelude>pi Prelude> (50 * 100) Prelude> 50 * Prelude> 50 * ( )

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

(!!) 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'

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

(%) 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

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

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

(:) 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"

(\\) 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]

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

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

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])

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)

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...

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

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

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]

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.

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

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]

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

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

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

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

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]

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]

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

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

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?

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