Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

Similar documents
CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CS457/557 Functional Languages

CS 11 Haskell track: lecture 1

Programming Languages Fall 2013

CS 360: Programming Languages Lecture 10: Introduction to Haskell

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Functional Programming and Haskell

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Haskell through HUGS THE BASICS

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Programming Paradigms

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

Standard prelude. Appendix A. A.1 Classes

CS 320: Concepts of Programming Languages

Lecture 1 August 9, 2017

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Software System Design and Implementation

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Functional Programming Languages (FPL)

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Logic - CM0845 Introduction to Haskell

A general introduction to Functional Programming using Haskell

Haskell An Introduction

Functional Programming Lecture 1: Introduction

CS 440: Programming Languages and Translators, Spring 2019 Mon

Shell CSCE 314 TAMU. Higher Order Functions

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Module 10: Imperative Programming, Modularization, and The Future

Programming in Haskell Aug Nov 2015

Functional Programming in Haskell for A level teachers

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Lecture 19: Functions, Types and Data Structures in Haskell

Programming Languages Fall Prof. Liang Huang

Introduction to Functional Programming

Haskell Overview II (2A) Young Won Lim 8/23/16

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Basic types and definitions. Chapter 3 of Thompson

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lecture 4: Higher Order Functions

CSCE 314 Programming Languages

Logical Methods in... using Haskell Getting Started

Haskell Overview II (2A) Young Won Lim 8/9/16

LECTURE 16. Functional Programming

First Haskell Exercises

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Higher Order Functions in Haskell

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Introduction to Functional Programming and Haskell. Aden Seaman

The Haskell HOP: Higher-order Programming

An introduction to functional programming. July 23, 2010

Functional Programming. Big Picture. Design of Programming Languages

Scala : an LLVM-targeted Scala compiler

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Chapter 15. Functional Programming Languages

Lecture 8: Summary of Haskell course + Type Level Programming

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Course year Typeclasses and their instances

Python lab session 1

3. Functional Programming. Oscar Nierstrasz

Programming Languages and Techniques (CIS120)

INTRODUCTION TO HASKELL

CSc 520. Principles of Programming Languages 11: Haskell Basics

Introduction to Programming

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Programming Paradigms and Languages Introduction. dr Robert Kowalczyk WMiI UŁ

More on functional programming

Haskell Overview III (3A) Young Won Lim 10/4/16

Introduction to Haskell

CSCE 314 Programming Languages

Programming Paradigms and Languages Introduction to Prolog. dr Robert Kowalczyk WMiI UŁ

Abstract Types, Algebraic Types, and Type Classes

Exercise 1 ( = 22 points)

A tour of the Haskell Prelude

Example: Haskell algebraic data types (1)

Side Effects (3A) Young Won Lim 1/13/18

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Principles of Programming Languages

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Chapter 11 :: Functional Languages

Functional Programming Lecture 13: FP in the Real World

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Introduction to Functional Programming in Haskell 1 / 56

An introduction introduction to functional functional programming programming using usin Haskell

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

Chapter 15 Functional Programming Languages

CSC324 Principles of Programming Languages

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS 340 Spring 2019 Midterm Exam

Haskell Types COMP360

Box-and-arrow Diagrams

Summary of basic C++-commands

Transcription:

Programming Paradigms and Languages Introduction to Haskell 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Ł 2

Declarative vs. imperative programming dr Robert Kowalczyk WMiI UŁ 3

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Ł 4

Haskell Haskell is a modern general-purpose functional language created in order to combine all the advantages of functional programming in one elegant, powerful and freely available programming language. Haskell is unique in two ways. First, it is a pure functional programming language. If you have a function and you call it twice in two different places with the same arguments then it will return exactly the same value both times. Second, Haskell provides a very modern type system which incorporates features like typeclasses and generalized algebraic data types. Advanced type system helps catch mistakes, both silly and profound. dr Robert Kowalczyk WMiI UŁ 5

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. Using the above analogy, the compiler is the oven that bakes your batter (code) into a cookie (executable file), and it's difficult to get the recipe from an executable once it's compiled. dr Robert Kowalczyk WMiI UŁ 6

Haskell platform dr Robert Kowalczyk WMiI UŁ 7

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 -o first haskel.hs run ghci (interactive console) and write putstrln "Hello World" or load script :l haskel.hs dr Robert Kowalczyk WMiI UŁ 8

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

Haskell as a calculator Prelude>8+4 12 Prelude>34-35 -1 Prelude>3.5*5676 19866 Prelude>23/6 3.833333333333335 Prelude>2^4 16 dr Robert Kowalczyk WMiI UŁ 10

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

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

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 Prelude>area 50.26548245743669 why? Prelude>let area = pi*r*r Prelude>area 78.53981633974483 dr Robert Kowalczyk WMiI UŁ 13

Excercise 1 Define the function writes x which write 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Ł 14

Checking the type of objects To check the type of object we 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Ł 15

Simple types in Haskell Int - [-2^29.. 2^29-1] Integer very big integer numbers Float real numbers Double real numbers Char - (Unicode) character Bool logical type dr Robert Kowalczyk WMiI UŁ 16

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

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Ł 18

Lists and lazy evaluation Prelude>"Haskell" "Haskell" Prelude>'H':'a':'s':'k':'e':'l':'l':[] "Haskell" 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Ł 19

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Ł 20

product :: (Num a) => [a] -> a product [-2,-1,0,1,2] 0 Functions on lists 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Ł 21

The comprehension list [expression(x) x <- list, condition(x)] Prelude> [x^2 x <- [1..10], even x] [4,16,36,64,100] Prelude> l=[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] dr Robert Kowalczyk WMiI UŁ 22

Excercise 2 Using the list comprehension write the function unitaryn x which will build nxn 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 dr Robert Kowalczyk WMiI UŁ 23

Homework Create a function listi x that returns list of integers numbers [1..x]. Then, create a function sumsqrtlist (listi x) that returns the sum of square roots of the list listi x. For example, if you run sumsqrtlist (listi 5) you should see on the screen: 8.382332347441762 dr Robert Kowalczyk WMiI UŁ 24

Haskell - complex excercise Type annotation (optional) factorial :: Integer -> Integer Using recursion factorial 0 = 1 factorial n n > 0 = n * factorial (n - 1) Using recursion but written without pattern matching factorial n = if n > 0 then n * factorial (n-1) else 1 Using a list factorial n = product [1..n] Using fold (implements product) factorial n = foldl1 (*) [1..n] Point-free style factorial = foldr (*) 1. enumfromto 1 dr Robert Kowalczyk WMiI UŁ 25

Any Quetions? dr Robert Kowalczyk WMiI UŁ 26