dr Robert Kowalczyk WMiI UŁ

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Getting Started with Haskell Tim Sheard. Learning a new language Goals – Learn how to design and use data structures – Learn to write programs using the.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
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.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Introduction to Python
Definitions. name :: Type answer :: Int name = expression answer = Definitions associate a name with a value of a certain type is of type greater.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
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.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Haskell Starting Out Piotr Poniatowski Łukasz Reszczyński Maciej Woźniczka.
Lecture #5 Introduction to C++
Overview of the Haskell 98 Programming Language
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Programming Languages
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Programming Languages Programming languages are a compromise between spoken language and formal math. They allow humans to communicate with computers at.
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.
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.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
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.
CS314 – Section 5 Recitation 9
Polymorphic Functions
Functional Programming
Basic concepts of C++ Presented by Prof. Satyajit De
CS314 – Section 5 Recitation 10
Conditional Expressions
Midterm recap Total was 80 points Distribution range
Programming Languages
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Basic 1960s It was designed to emphasize ease of use. Became widespread on microcomputers It is relatively simple. Will make it easier for people with.
dr Robert Kowalczyk WMiI UŁ
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Computing Fundamentals
A lightening tour in 45 minutes
Functional Programming
Haskell.
CSE 3302 Programming Languages
Engineering Innovation Center
Clojure to Haskell (It’s mostly syntax).
Programming Languages
Sridhar Narayan Java Basics Sridhar Narayan
Type & Typeclass Syntax in function
Programming Paradigms and Languages
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Fundamentals of Functional Programming
Java Programming Review 1
PROGRAMMING IN HASKELL
Functional Programming
Functions and patterns
PROGRAMMING IN HASKELL
Functional Programming and Haskell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Programming Paradigms and Languages
Functional Programming and Haskell
Presentation transcript:

dr Robert Kowalczyk WMiI UŁ Summer School Haskell 1 dr Robert Kowalczyk WMiI UŁ

Distribution of programming paradygms Programming languages Imperative Declarative Object-oriented Structured Generic Concurrent Functional Logical SQL, XML Procedural dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Definitions Imperative programming: telling the „machine” (computer) how to do something, and as a result what you want to happen will happen. Declarative programming: telling the „machine” (computer) what you would like to happen, and let the computer figure out how to do it. dr Robert Kowalczyk WMiI UŁ

Imperative vs. declarative programming - example We wish to double all the numbers in an array. Imperative style of programming: var numbers = [1,2,3,4,5] var doubled = [] for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.write(doubled) //=> [2,4,6,8,10] Declarative style of programming: var doubled = numbers.map(function(n) { return n * 2 }) console.log(doubled) //=> [2,4,6,8,10] dr Robert Kowalczyk WMiI UŁ

Declarative vs. imperative programming dr Robert Kowalczyk WMiI UŁ

Functional programming In functional programming (special type of declarative programming), programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of many statements which change global state when executed. dr Robert Kowalczyk WMiI UŁ

Functional programming languages Lisp - a family of programming languages: Common Lisp , Scheme i Clojure Ocaml ML Haskell Erlang Scala Python dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Haskell language First version of Haskell („Haskell 1.0”) was defined in 1990. Haskell is purely functional programming language. Haskell has lazy evaluation. Haskell is a strongly typed programming language. dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Haskell compiler To write Haskell programs, you need a program called a Haskell compiler. A compiler is a program that takes code written in Haskell and translates it into machine code, a more primitive language that the computer understands. dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Haskell platform dr Robert Kowalczyk WMiI UŁ

First program/script in Haskell After you have installed the Haskell Platform, it's now time to write your first Haskell code. You can: write script haskel.hs with code: main = do putStrLn "Hello World" and compile it ghc haskel.hs run ghci (interactive console) and write or load script :l haskel.hs dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Help in Haskell :? or :help – display list of commands :l or :load – load module :cd – change directory :! – run the shell command :! cd – change local directory dr Robert Kowalczyk WMiI UŁ

Haskell as a calculator dr Robert Kowalczyk WMiI UŁ

Functions in Haskell (embedded) abs x => |x| floor x => [x] div => / mod => % max a b => maximum(a,b) min a b => minimum(a,b) sqrt x => x^(1/2) exp x => exp(x) log x => ln(x) sin x => sin(x) cos x => cos(x) tan x => tan(x) pi => 3.141592653589793 dr Robert Kowalczyk WMiI UŁ

Prefix and infix notation in Haskell dr Robert Kowalczyk WMiI UŁ

Comments and a simple function -- one line comment {- block comment -} Prelude>let square x = x * x Prelude>square 3 9 Prelude>square (-3) Write script first.hs square x = x * x then load script :l first.hs and run function square 4 dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Variables Prelude>let r=4 Prelude>r 4 Prelude>let area = pi*r*r error – why? Prelude>let r=4.0 Prelude>area 50.26548245743669 Prelude>let r=5.0 why? 78.53981633974483 dr Robert Kowalczyk WMiI UŁ

Checking the type of objects To check the type of object we can use the command :t or :type Prelude>:t "dog" "dog"::[Char] Prelude>:t 'e' 'e'::Char Prelude>let i=5 Prelude>:t i i::Integer Prelude>lez z=5.6 Prelude>:t z z::Double dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Classtypes in Haskell dr Robert Kowalczyk WMiI UŁ

Simple types in Haskell Int - numbers in the range -229…229-1 Integer - very big integer numbers Float - real numbers Double - real numbers Char - (Unicode) character Bool - logical type dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Logical operators Prelude>True && (1<4) True Prelude>False || False False Prelude>2 /= 3 Prelude>not True dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Lists Prelude> let names = ["Jane", "George", "Kate"] Prelude> let numbers = [-2,-1,0,1,2] Prelude> -3 : numbers [-3,-2,-1,0,1,2] Prelude> numbers [-2,-1,0,1,2] Prelude> head numbers -2 Prelude> tail numbers [-1,0,1,2] dr Robert Kowalczyk WMiI UŁ

Lists and lazy evaluation Prelude>['H','a','s','k','e','l','l'] "Haskell" Prelude>'H':'a':'s':'k':'e':'l':'l':[] Prelude>[1,2..10] [1,2,3,4,5,6,7,8,9,10] Prelude>[5,3..(-1)] [5,3,1,-1] Prelude>[1,2..] 1,2,3,4,…. let pitagoras = [(a, b, c)| c <- [1 ..] , b <- [1 .. c] , a <- [1 .. b] , a^2 + b^2 == c^2] Prelude> [[1,2,3],[2,3,4],[3,4,5]] [[1,2,3],[2,3,4],[3,4,5]] dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Functions on lists (!!) :: [a] -> Int -> a (!!) [-2,-1,0,1,2] 1 -1 length :: [a] -> Int length [-2,-1,0,1,2] 5 (++) :: [a] -> [a] -> [a] (++) [1,2,3] [4,5,6] [1,2,3,4,5,6] drop: Int -> [a] -> [a] drop 3 [-2,-1,0,1,2] [1,2] sum :: (Num a) => [a] -> a sum [-2,-1,0,1,2] 0 dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Functions on lists product :: (Num a) => [a] -> a product [-2,-1,0,1,2] 0 map :: (a->b) -> [a] -> [b] map (+2) [-2,-1,0,1,2] [0,1,2,3,4] filter :: (a -> Bool) -> [a] -> [a] filter (>0) [-2,-1,0,1,2] [1,2] null :: [a] -> Bool null [] true take :: Int -> [a] -> [a] take 2 [-2,-1,0,1,2] [-2,-1] dr Robert Kowalczyk WMiI UŁ

The comprehension list [exp(x) | x <- list, cond(x)] Prelude> [x^2 | x <- [1..10], even x] [4,16,36,64,100] Prelude> [a+b| a<-[1..5], b<-[-5..(-1)]] [-4,-3,-2,-1,0,-3,-2,-1,0,1,-2,-1,0,1,2,-1,0,1,2,3,0,1,2,3,4] Prelude> [head x | x <- [[1..10],[20..30],[30..40]] [1,20,30] dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Tuppels Prelude>let p = (2,3) Prelude>fst p 2 Prelude>snd p 3 Prelude>(”Martin”,”Depp”,23) (”Martin”,”Depp”,23) Prelude> zip [1,2,3,4,5] [5,5,5,5,5] [(1,5),(2,5),(3,5),(4,5),(5,5)] Prelude>unzip [(1,5),(2,5),(3,5),(4,5),(5,5)] ([1,2,3,4,5],[5,5,5,5,5]) dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Excercise 1 Define the function writeS x which writes string x on the screen. For example, if you run writeS "Robert" you should see on the screen: "Hello Robert!" Hint: If you want to add two strings you can use ++ operator. dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Excercise 2 Using the list comprehension construction write the function unitaryN which will build unitary matrix. For example, if you run unitaryN 5 you should see on the screen: [[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1]] Hint: use if command: if condition then instruction1 else instruction2 Simpler version: using the list comprehension construction write the function vectorNM which will build unitary vector as in the following example: vectorNM 5 7 returns vector: [0,0,0,0,1,0,0] dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Excercise 3 Build a list of integer numbers such that: numbers are three digits numbers are divisible by 3 all digits in each number are different. Of course these are the numbers: 102,105,…,987 dr Robert Kowalczyk WMiI UŁ

dr Robert Kowalczyk WMiI UŁ Coffee break  dr Robert Kowalczyk WMiI UŁ