Functional Programming Principles in Scala. Martin Odersky

Similar documents
Classical Themes of Computer Science

Functional Programming

Functional Programming

Concepts in Programming Languages

Functional Programming

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

Concepts of Programming Languages

CS 242. Fundamentals. Reading: See last slide

Refactoring to Functional. Hadi Hariri

Functional Programming Lecture 1: Introduction

CS457/557 Functional Languages

Welcome to. Instructor Marc Pomplun CS 470/670. Introduction to Artificial Intelligence 1/26/2016. Spring Selectivity in Complex Scenes

Concepts of Programming Languages

Functional Programming. Ionut G. Stan - OpenAgile 2010

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Functional Programming Lecture 13: FP in the Real World

Class Structure. Prerequisites

Robot Programming with Lisp

COS 326 Functional programming: an elegant weapon for the modern age

Stop coding Pascal. Saturday, April 6, 13

Functional Programming. Big Picture. Design of Programming Languages

What is a programming language?

CS205: Scalable Software Systems

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

Functional Programming Invades Architecture. George Fairbanks SATURN May 2017

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

What Is Computer Science? The Scientific Study of Computation. Expressing or Describing

Programming Languages, Summary CSC419; Odelia Schwartz

IA010: Principles of Programming Languages

CS558 Programming Languages

The Trouble with Types

CSE341: Programming Languages Interlude: Course Motivation. Zach Tatlock Winter 2018

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Com S 541. Programming Languages I

Programming Languages 2nd edition Tucker and Noonan"

It's Time To Get Functional 이건희

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

Imperative Functional Programming

Introduction to Functional Programming and Haskell. Aden Seaman

Chapter 11 :: Functional Languages

Presented By Andrew Butt

Anders Møller Michael Schwartzbach Erik Ernst Hans Hüttel

CSE341: Programming Languages Lecture 26 Course Victory Lap. Dan Grossman Spring 2016

PROGRAMMING LANGUAGE PARADIGMS & THE MAIN PRINCIPLES OF OBJECT-ORIENTED PROGRAMMING

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

COSC-589 Web Search and Sense-making Information Retrieval In the Big Data Era. Spring Instructor: Grace Hui Yang

State, Concurrency & Complexity. Does programming really need to be so hard?

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Wayne State University Department of Computer Science CSC 5991: Advanced Web Technologies. Functional (Scala) Programming for the Web.

EECS 700 Functional Programming

G Programming Languages - Fall 2012

SD314 Outils pour le Big Data

More Lambda Calculus and Intro to Type Systems

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

Introduction. chapter Functions

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

More Lambda Calculus and Intro to Type Systems

Programming Languages and Techniques (CIS120)

CSc 372 Comparative Programming Languages

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Learning Scala: Practical Functional Programming For The JVM By Jason Swartz

CS383 PROGRAMMING LANGUAGES. Kenny Q. Zhu Dept. of Computer Science Shanghai Jiao Tong University

LECTURE 16. Functional Programming

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

Lecture 8: Summary of Haskell course + Type Level Programming

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

What is programming? Elements of Programming Languages. From machine code to programming languages. What is a programming language?

Getting Started with Functional Programming in JavaScript. Eric Normand

Introduction to Nate Foster Spring 2018

Programming Systems in Artificial Intelligence Functional Programming

More Lambda Calculus and Intro to Type Systems

Scala : an LLVM-targeted Scala compiler

Seminar in Programming Languages

Functional Languages. Hwansoo Han

Mostly functional programming does not work

Introduction to Functional Programming

Introduction, Functions

Programming Paradigms

Programming in Scala Second Edition

Programming Language Pragmatics

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

G Programming Languages - Fall 2018

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

15. Functional Programming

Chapter 1. Preliminaries

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Programming Language Concepts

Introduction to Functional Programming in Haskell 1 / 56

Adv. Course in Programming Languages

ITT8060 Advanced Programming

CMSC 330: Organization of Programming Languages. Administrivia

Immutability and Design Patterns in Ruby

Chapter 1. Preliminaries

CSE341: Programming Languages Lecture 22 OOP vs. Functional Decomposition; Adding Operators & Variants; Double-Dispatch. Dan Grossman Autumn 2018

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Transcription:

Functional Programming Principles in Scala Martin Odersky

Programming Paradigms Paradigm: In science, a paradigm describes distinct concepts or thought patterns in some scientific discipline. Main programming paradigms: imperative programming functional programming logic programming Orthogonal to it: object-oriented programming

Review: Imperative programming Imperative programming is about modifying mutable variables, using assignments and control structures such as if-then-else, loops, break, continue, return. The most common informal way to understand imperative programs is as instruction sequences for a Von Neumann computer.

Imperative Programs and Computers There s a strong correspondence between Mutable variables memory cells Variable dereferences load instructions Variable assignments store instructions Control structures jumps Problem: Scaling up. How can we avoid conceptualizing programs word by word? Reference: John Backus, Can Programming Be Liberated from the von. Neumann Style?, Turing Award Lecture 1978.

Scaling Up In the end, pure imperative programming is limited by the Von Neumann bottleneck: One tends to conceptualize data structures word-by-word. We need other techniques for defining high-level abstractions such as collections, polynomials, geometric shapes, strings, documents. Ideally: Develop theories of collections, shapes, strings,

What is a Theory? A theory consists of one or more data types operations on these types laws that describe the relationships between values and operations Normally, a theory does not describe mutations!

Theories without Mutation For instance the theory of polynomials defines the sum of two polynomials by laws such as: (a*x + b) + (c*x + d) = (a+c)*x + (b+d) But it does not define an operator to change a coefficient while keeping the polynomial the same!

Theories without Mutation For instance the theory of polynomials defines the sum of two polynomials by laws such as: (a*x + b) + (c*x + d) = (a+c)*x + (b+d) But it does not define an operator to change a coefficient while keeping the polynomial the same! Whereas in an imperative program one can write: class Polynomial { double [] coefficient ; } Polynomial p =...; p. coefficient [0] = 42;

Theories without Mutation Other example: The theory of strings defines a concatenation operator ++ which is associative: (a ++ b) ++ c = a ++ (b ++ c) But it does not define an operator to change a sequence element while keeping the sequence the same! (This one, some languages do get right; e.g. Java s strings are immutable)

Consequences for Programming If we want to implement high-level concepts following their mathematical theories, there s no place for mutation. The theories do not admit it. Mutation can destroy useful laws in the theories. Therefore, let s concentrate on defining theories for operators expressed as functions, avoid mutations, have powerful ways to abstract and compose functions.

Functional Programming In a restricted sense, functional programming (FP) means programming without mutable variables, assignments, loops, and other imperative control structures. In a wider sense, functional programming means focusing on the functions. In particular, functions can be values that are produced, consumed, and composed. All this becomes easier in a functional language.

Functional Programming Languages In a restricted sense, a functional programming language is one which does not have mutable variables, assignments, or imperative control structures. In a wider sense, a functional programming language enables the construction of elegant programs that focus on functions. In particular, functions in a FP language are first-class citizens. This means they can be defined anywhere, including inside other functions like any other value, they can be passed as parameters to functions and returned as results as for other values, there exists a set of operators to compose functions

Some functional programming languages In the restricted sense: Pure Lisp, XSLT, XPath, XQuery, FP Haskell (without I/O Monad or UnsafePerformIO) In the wider sense: Lisp, Scheme, Racket, Clojure SML, Ocaml, F# Haskell (full language) Scala Smalltalk, Ruby (!)

History of FP languages 1959 Lisp 1975-77 ML, FP, Scheme 1978 Smalltalk 1986 Standard ML 1990 Haskell, Erlang 1999 XSLT 2000 OCaml 2003 Scala, XQuery 2005 F# 2007 Clojure

Recommended Book (1) Structure and Interpretation of Computer Programs. Harold Abelson and Gerald J. Sussman. 2nd edition. MIT Press 1996. A classic. Many parts of the course and quizzes are based on it, but we change the language from Scheme to Scala. The full text can be downloaded here.

Recommended Book (2) Programming in Scala. Martin Odersky, Lex Spoon, and Bill Venners. 2nd edition. Artima 2010. A comprehensive step-by-step guide Programming in Scala Second Edition Updated for Scala 2.8 artima Martin Odersky Lex Spoon Bill Venners The standard language introduction and reference.

Other Recommended Books The first part of Scala for the Impatient is available for free download.

Why Functional Programming? Functional Programming is becoming increasingly popular, because it offers the following benefits. simpler reasoning principles better modularity good for exploiting parallelism for multicore and cloud computing. To find out more, see the video of my 2011 Oscon Java keynote Working Hard to Keep it Simple (16.30 minutes). The slides for the video are available separately.