Main Points of Haskell Functional programming Laziness

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Functional Programming Lecture 10 - type checking.
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Interlude: Functional Programming CSE 331 Section 2 James Daly.
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.
Haskell Chapter 8. Input and Output  What are I/O actions?  How do I/O actions enable us to do I/O?  When are I/O actions actually performed?
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Going Functional Primož Gabrijelčič. Functional programming.
Python Functions.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Overview Go over parts of quiz? Another iteration structure for loop.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Functional Programming IN NON-FUNCTIONAL LANGUAGES.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
Functional Programming Lecture 1 - Introduction Professor Muffy Calder.
Advanced Functional Programming 2010
Functional Programming
CS314 – Section 5 Recitation 9
Haskell Chapter 8.
Laziness and Infinite Datastructures
Programming Languages
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Lesson #6 Modular Programming and Functions.
Theory of Computation Lecture 4: Programs and Computable Functions II
Lesson #6 Modular Programming and Functions.
Methods Chapter 6.
Principles of programming languages 4: Parameter passing, Scope rules
A lightening tour in 45 minutes
Functional Programming
Haskell.
Functional Programming Lecture 2 - Functions
Lesson #6 Modular Programming and Functions.
Control Flow Chapter 6.
Programming Languages
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
PROGRAMMING IN HASKELL
Type & Typeclass Syntax in function
CSE-321 Programming Languages Introduction to Functional Programming
6.001 SICP Further Variations on a Scheme
6.001 SICP Variations on a Scheme
Lesson #6 Modular Programming and Functions.
Haskell Dealing with impurity 8-Apr-19.
Nate Brunelle Today: Conditional Decision Statements
Functional Programming
Introduction to the Lab
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 2 - Functions
Haskell Dealing with impurity 29-May-19.
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 28-Jun-19.
Presentation transcript:

Main Points of Haskell Functional programming Laziness Purity (i.e. no side effects)

Main Points of Haskell Functional programming No states. Just values. No commands. Just math functions and declarations. x = 1 Imperative: assignment Functional: declaration

Main Points of Haskell Functional programming if … then … else Imperative: if <Bool> then <Commands> else <Commands> Functional: if <Bool> then <Value> else <Value> “if” can be viewed as a function: if :: Bool -> a -> a -> a if-then-else is just a syntactic sugar.

Main Points of Haskell Functional programming “Pure” math functions. Not procedures. Transform inputs to outputs. No side effects.

Main Points of Haskell Functional programming No loops. Use recursion instead. Why? Recursion: define sth using itself To solve a problem on recursively-defined data (e.g. a list, a tree), think of a recursive solution

Main Points of Haskell Functional programming No states. Just values. No commands. Just “pure” math functions and declarations. Not procedures. Laziness (i.e. call-by-need) Purity (i.e. no side effects)

Main Points of Haskell Laziness: Evaluation of function arguments is delayed as long as possible f x y = x + 2 f 5 (22^3579): 22^3579 will not be evaluated When to evaluate an argument expression?

Main Points of Haskell Laziness: Argument expressions are only evaluated when pattern-matched data Maybe = Nothing | Just Int f1 :: Maybe -> [Maybe] f1 m = [m, m] f2 :: Maybe -> [Int] f2 Nothing = [] f2 (Just x) = [x] Both f1 and f2 “uses” the argument. But for f1 m, m can remain unevaluated.

Main Points of Haskell Laziness: Argument expressions are only evaluated when pattern-matched Only as far as necessary for the match to proceed, and no farther! f2 Nothing = [] f2 (Just x) = [x] f2 (safeHead [3^500, 2]): f2 will force the evaluation of the call to (safeHead [3^500, 2]), which’ll evaluate to (Just 3^500) Whether the 3^500 gets evaluated later depends on how the result of f2 is used.

Main Points of Haskell Purity (no side effects) Side effects: anything that causes evaluation of an expression to interact with something outside itself. The issue: those effects are time-sensitive. E.g. Modifying a global variable Printing to the screen Reading from a file

Main Points of Haskell Laziness forces purity Lazy evaluation makes it hard to reason about when things will be evaluated But sides effects are time-sensitive Hence including side effects in a lazy language would be extremely unintuitive f (print(“hello”), print(“bye”)) Eager languages should specify the evaluation order, otherwise it will be confusing

Main Points of Haskell Functional programming Laziness Purity (i.e. no side effects)