PL Categories: Functional PLs Introduction to Haskell Haskell: Functions

Size: px
Start display at page:

Download "PL Categories: Functional PLs Introduction to Haskell Haskell: Functions"

Transcription

1 PL Categories: Functional PLs Introduction to Haskell Haskell: Functions CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, February 22, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks 2017 Glenn G. Chappell

2 Review Shift-Reduce Parsing [1/2] Shift-Reduce is a bottom-up, LR parsing algorithm. It is table-driven. Shift-Reduce is no longer used. However, a number of similar but more complicated algorithms are heavily used. A Shift-Reduce parser is a state machine with an associated stack. A stack item holds a symbol terminal or nonterminal and a state. The current state is the state in the top-of-stack item. The table has two parts: action table (rows are states, columns are terminals) and goto table (rows are states, columns are nonterminals). Operation Begin by pushing an item holding the start state (and any symbol). At each step, do a lookup in the action table, using the current state and the current input symbol. Do what the action table entry says. 22 Feb 2017 CS F331 / CSCE A331 Spring

3 Review Shift-Reduce Parsing [2/2] We constructed the following Shift-Reduce parsing table in class. STATE ACTION TABLE GOTO TABLE Note Number ID ( ) $ all item start: 1 S2 S3 G8 G4 ID 2 R3 ( 3 S2 S3 G5 item 4 S6 (item 5 S7 item$ 6 R1 (item) 7 R2 all 8 ACCEPT 22 Feb 2017 CS F331 / CSCE A331 Spring

4 Review Parsing Wrap-Up Efficiency of Parsing Practical parsing algorithms: Cannot handle all CFLs. Run in linear time. This includes Recursive Descent and Shift-Reduce. There are parsing algorithms that can handle all CFLs. These are much slower and are mostly theoretical curiosities. 22 Feb 2017 CS F331 / CSCE A331 Spring

5 PL Categories: Functional PLs Background [1/3] Functional programming (FP) is a programming style that generally has the following characteristics. Computation is considered primarily in terms of the evaluation of functions (as opposed to execution of code). Thus, functions are a primary concern. Rather than considered as repositories for code, functions are values to be constructed and manipulated. Side effects & mutable data are avoided. The only job of a function is to return a value. A side effect occurs when a function alters data, and this alteration is visible outside the function. Mutable data is data that can be altered. In PLs like C++, Java, and Lua, we mostly do imperative programming: tell the computer what to do. An alternative is declarative programming: tell the computer what is true. FP is one kind of declarative programming. (We will look at another kind later in the semester.) 22 Feb 2017 CS F331 / CSCE A331 Spring

6 PL Categories: Functional PLs Background [2/3] One can do FP, in some sense, in just about any PL. However, some PLs support it better than others. A functional programming language is a PL designed to support FP well. This is thus a somewhat vague term. No one calls C a functional PL. Opinions vary about JavaScript. Everyone agrees that Haskell is a functional PL. PLs generally agreed to be functional include Haskell, the ML family (ML, OCaml, F#), R, and Miranda. In addition, the Lisp family (Common Lisp, Emacs Lisp, Scheme, Clojure, Racket, Logo) generally offers excellent support for FP, but is usually considered as a separate category. 22 Feb 2017 CS F331 / CSCE A331 Spring

7 PL Categories: Functional PLs Background [3/3] FP and functional PLs have been around for many decades, but they remained largely the province of academia until two things happened. In the 1990s the problem of how to do interactive I/O in a purely functional context was solved. In the years around 2000, serious attention was first given to the practical issues of algorithmic efficiency and resource usage in a purely functional context. Today, FP and functional PLs are becoming increasingly mainstream. Constructs inspired by FP are being introduced into a number of PLs. For example, lambda functions became part of C++ in the 2011 Standard. 22 Feb 2017 CS F331 / CSCE A331 Spring

8 PL Categories: Functional PLs Typical Characteristics [1/2] Recall that a type is first-class if its values can be tossed around with the same ease and facility as types like int in C++. More formally, a type is first-class if its values can be used in each of the following ways. New values can be created from existing values at runtime. Values can be passed as arguments to functions and returned from functions. Values can be stored in containers. We are particularly interested in the idea of first-class functions. 22 Feb 2017 CS F331 / CSCE A331 Spring

9 PL Categories: Functional PLs Typical Characteristics [2/2] A typical functional programming language has the following features/characteristics. It has first-class functions. It offers good support for higher-order functions. A higher-order function is a function that acts on functions. It offers good support for recursion. It has a preference for immutable data. A pure functional PL goes further, and does not support mutable data at all. There are no side effects in a pure functional PL. 22 Feb 2017 CS F331 / CSCE A331 Spring

10 Introduction to Haskell History [1/2] The next programming language we will look at is Haskell, named for Logician Haskell Curry ( ). Haskell was created as the result of a meeting in Some members of the FP community felt that their efforts were too fragmented. They decided to create a single PL intended to support research or development by large numbers of people. The initial version of Haskell was released in In the 1990s, the problem of how to do interactive I/O without side effects was solved, allowing Haskell and FP to enter the mainstream. Various language definitions in the 1990s culminated in a longterm standard in 1998 (Haskell 98). This was implemented in Hugs, a simple interactive environment supported on all major platforms. There was also a full-featured compiler: the Glorious Glasgow Haskell Compiler (GHC). Hugs has since been folded into GHC; the interactive environment is now called GHCi. 22 Feb 2017 CS F331 / CSCE A331 Spring

11 Introduction to Haskell History [2/2] 2009 saw the release of the Haskell Platform, a collection of libraries and tools with the goal of creating a high-quality, batteries-included collection of packages that all Haskell developers could have in common. A new release of the Haskell Platform comes once or twice each year. A second Haskell standard was published in 2010 (Haskell 2010, or Haskell Prime). The is currently the most recent standard. A plan for yearly standards was announced, but it has not become a reality. A third Haskell standard is currently in the works. Haskell is now a robust, well supported PL, suitable for large commercial projects. However, its unusual nature has led to a fair amount of resistance from traditionally minded programmers. The most recent TIOBE index February 2017 lists Haskell as the 38th most popular PL (up from 40th a year ago and 46th two years ago). 22 Feb 2017 CS F331 / CSCE A331 Spring

12 Introduction to Haskell Characteristics Type System [1/4] Haskell is a pure functional PL. It has first-class functions and good support for higher-order functions. Haskell has a sound static type system with sophisticated type inference. So typing is largely inferred, and thus implicit; however, we are allowed to use manifest typing, if we wish. Haskell s type-checking standards are difficult to place on the nominal-structural axis. Haskell has few implicit type conversions. New implicit type conversions cannot be defined. 22 Feb 2017 CS F331 / CSCE A331 Spring

13 Introduction to Haskell Characteristics Type System [2/4] Like C++ and Java, Haskell does static typing of both variables and values. Unlike C++ and Java, Haskell includes excellent support for type inference (based on the Hindley-Milner type-inference algorithm). Thus, types usually do not need to be specified. In C++: int n = 3; In Haskell: n = 3 Of course, in Lua we can say that, too. But in contrast to Lua, the variable n has a type in Haskell (Integer in this case); the compiler is able to figure this out for us. 22 Feb 2017 CS F331 / CSCE A331 Spring

14 Introduction to Haskell Characteristics Type System [3/4] Haskell still allows type annotations, if desired. We can say: n :: Integer n = 3 This lets us communicate our intentions to be compiler. For example, the following is legal. s = "abc" But this will not compile: s :: Integer s = "abc" -- Type error: "abc" is not of type Integer 22 Feb 2017 CS F331 / CSCE A331 Spring

15 Introduction to Haskell Characteristics Type System [4/4] We can also use Haskell type annotations to restrict which types are allowed. Below is a function with its natural type annotation. Omitting this annotation would have the same result. blug :: (Eq t, Num t) => t -> t -> Bool blug a b = (a == b+1) The above says that blug is a function that takes two values of type t, where t is a numeric type with equality operations defined, and it returns a boolean. But if we only want blug to take integers, then we can do this: blug :: Integer -> Integer -> Bool blug a b = (a == b+1) 22 Feb 2017 CS F331 / CSCE A331 Spring

16 Introduction to Haskell Characteristics Flow of Control Haskell implementations are required to do tail call optimization (TCO). This means that the last operation in a function is not implemented via a function call, but rather as the equivalent of a goto, never returning to the original function. Iteration is difficult without mutable data. And, indeed, Haskell has no iterative flow-of-control constructs. It uses recursion instead, with tail recursion preferred. The latter will generally be optimized using TCO. 22 Feb 2017 CS F331 / CSCE A331 Spring

17 Introduction to Haskell Characteristics Syntax [1/2] Haskell has a simple syntax, with less punctuation than C++ and even less than Lua. Here are function calls in C++ and Lua. foo(a, b, c); // C++ foo(a, b, c) -- Lua Here is a more or less equivalent function call in Haskell. foo a b c -- Haskell 22 Feb 2017 CS F331 / CSCE A331 Spring

18 Introduction to Haskell Characteristics Syntax [2/2] Haskell has significant indentation. Indenting is the usual way to indicate the start & end of a block. Here is Lua. function bar(a) local b = 42 // Indented, but only for local c = 30 * b + 1 // readability; the compiler return foo(a, b, c) // ignores indentation. end And here is the more or less equivalent Haskell. bar a = foo a b c where b = 42 c = 30 * b We MUST indent this line. 22 Feb 2017 CS F331 / CSCE A331 Spring

19 Introduction to Haskell Characteristics Evaluation [1/2] By default Haskell does lazy evaluation: expressions are not evaluated until they need to be. C++, Java, and Lua do the opposite, evaluating as soon as an expression is encountered; this is eager evaluation (or strict evaluation). For example, here is a function in Lua, and then in Haskell. function f(x, y) end return x+1 -- y is not used f x y = x+1 -- y is not used We look at what eager evaluation vs. lazy evaluation means for these. 22 Feb 2017 CS F331 / CSCE A331 Spring

20 Introduction to Haskell Characteristics Evaluation [2/2] function f(x, y) end return x+1 -- y is not used Lua (eager). Do f(g(1), g(2)). Function g is called with 1. Then g is called with 2. The return values are passed to f. f x y = x+1 -- y is not used Haskell (lazy). Do f (g 1) (g 2). Function f is called; this uses its first parameter (x), so g is called with argument 1, and its return value becomes x. Then f adds 1 to this and returns the result. The second call to g is never made. If the return value of f is not used, then no calls to g are made! Lazy evaluation has other interesting consequences, as we will see. 22 Feb 2017 CS F331 / CSCE A331 Spring

21 Introduction to Haskell Build & Execution [1/3] The standard filename suffix for Haskell source files is.hs. GHC is a Haskell compiler that usually generates native machine code. On the command line, GHC is used much like g++, clang, or any other command-line compiler. ghc myprog.hs o myprog If there are no errors, then an executable named myprog will be generated. Running that file will execute function main in module Main (a module in Haskell is much like a module in Lua). Of course, if you are using an IDE, then things are handled differently. GHC is supported by various IDEs, including Eclipse. 22 Feb 2017 CS F331 / CSCE A331 Spring

22 Introduction to Haskell Build & Execution [2/3] GHCi is an interactive environment that interprets Haskell code. Such an environment is often called a Read-Eval-Print Loop (REPL), a term originating with Lisp. GHCi can load source files or evaluate entered Haskell expressions. Haskell is compiled to a bytecode, which is interpreted. After running GHCi, you are presented with a prompt. GHCi commands begin with colon (:). Some important commands: :l FILENAME.hs Load & compile the given source file. Afterward, calls to functions in the file may be typed in at the prompt. :r Reload the last file loaded. Useful if you change a file. Continued 22 Feb 2017 CS F331 / CSCE A331 Spring

23 Introduction to Haskell Build & Execution [3/3] Continuing with GHCi commands: :t EXPRESSION Get the type of a Haskell expression. The expression can involve variables and functions defined in a file that has been loaded. :i IDENTIFIER Get information about the identifier: its type; precedence and associativity if it is an operator; perhaps the file it is defined in. 22 Feb 2017 CS F331 / CSCE A331 Spring

24 Introduction to Haskell Some Programming I have written a simple example Haskell program that computes Fibonacci numbers. See fibo.hs. 22 Feb 2017 CS F331 / CSCE A331 Spring

25 Haskell: Functions Basic Syntax [1/3] The material for this topic is also covered in a Haskell source file, which is extensively commented. See func.hs. Haskell expression: stream of words separated by blanks where necessary. Optional parentheses for grouping. Each line below is a single Haskell expression. Type it at the GHCi prompt and press <Enter> to see its value. 2+3 (2+3)*5 reverse "abcde" map (\ x -> x*x) [1,2,3,4] 22 Feb 2017 CS F331 / CSCE A331 Spring

26 Haskell: Functions Basic Syntax [2/3] Comments Single line, two dashes to end of line: -- Multi-line, begin with brace-dash, end with dash-brace: {- -} Identifiers begin with letter or underscore, and contain only letters, underscores, digits, and single quotes ('). Normal identifiers begin with lower-case letter or underscore. These are used to name variables and functions. myvariable my_function'_33 Special identifiers begin with UPPER-CASE letter. These are used to name types, modules, and constructors. MyModule 22 Feb 2017 CS F331 / CSCE A331 Spring

27 Haskell: Functions Basic Syntax [3/3] Define a variable by giving its name, and equals sign (=) and an expression for the value. ab'c = 7 * (3 + 2) A variable definition is not an expression in Haskell. The above is legal in a Haskell source file. At the GHCi prompt it must be preceded by let. let ab'c = 7 * (3 + 2) 22 Feb 2017 CS F331 / CSCE A331 Spring

28 Haskell: Functions Defining Functions & Operators [1/3] To define a Haskell function, write what looks like a call to the function, an equals sign, and then an expression for what the function returns. addem a b = a+b We can also define new operators. Infix binary operators have names consisting of special characters. They are defined similarly. a +$+ b = 2*a + b 22 Feb 2017 CS F331 / CSCE A331 Spring

29 Haskell: Functions Defining Functions & Operators [2/3] Function definitions use pattern matching. Define a function differently for different patterns. The first matching pattern is the one used. Here is a factorial function. factorial 0 = 1 factorial n = n * factorial (n-1) 22 Feb 2017 CS F331 / CSCE A331 Spring

30 Haskell: Functions Defining Functions & Operators [3/3] We can use a regular function as an infix operator by surrounding its name with backquotes (`). Having defined function addem, try the following at the GHCi prompt. 2 `addem` 3 And we can use an operator as a regular function by surrounding its name with parentheses. Having defined +$+, try the following at the GHCi prompt. (+$+) Feb 2017 CS F331 / CSCE A331 Spring

31 Haskell: Functions Local Definitions Use where to introduce a block (indent!) of local definitions. plus_minus times a b c d = a_plus_b * c_minus_d where a_plus_b = a + b c_minus_d = c - d Local-definiton blocks can be nested. twicefactorial n = twice (factorial n) where twice k = two*k where two = 2 factorial 0 = 1 factorial curr = curr * factorial prev where prev = curr-1 22 Feb 2017 CS F331 / CSCE A331 Spring

32 Haskell: Functions Function Application & Currying Currying mean simulating a multiparameter function using a single parameter function that returns a function. For example, our function addem really takes one parameter. It returns a function that adds that parameter to something. The following are the same: addem Returns 5 (addem 2) 3 -- Returns 5 We can give the intermediate function a name. add2 = addem 2 add Returns 5 22 Feb 2017 CS F331 / CSCE A331 Spring

33 Haskell: Functions Higher-Order Functions A higher-order function is a function that acts on functions. rev f a b = f b a sub a b = a - b rsub = rev sub sub Returns 3 rsub Returns Feb 2017 CS F331 / CSCE A331 Spring

34 Haskell: Functions Lambda Functions A lambda function (or lambda expression) is a kind of expression whose value is a function. The name comes from the lambda calculus, a mathematical system in which everything is a function. In this system, an unnamed function is introduced using the Greek letter lambda (λ). Haskell uses a backslash (\) since it looks a bit like a lambda. square x = x*x square = \ x -> x*x -- Using lambda function; -- same as above -- Alternate definitions for addem addem = \ x y -> x+y addem a = \ y -> a+y addem = \ x -> (\ y -> x+y) 22 Feb 2017 CS F331 / CSCE A331 Spring

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G. Haskell: Lists CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

Thoughts on Assignment 4 Haskell: Flow of Control

Thoughts on Assignment 4 Haskell: Flow of Control Thoughts on Assignment 4 Haskell: Flow of Control CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 27, 2017 Glenn G. Chappell Department of Computer

More information

Scheme: Expressions & Procedures

Scheme: Expressions & Procedures Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University

More information

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G. Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

More information

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Basics 2 Contents 1. Jump into Haskell: Using ghc and ghci (more detail) 2. Historical Background of Haskell 3. Lazy, Pure, and Functional

More information

CS 11 Haskell track: lecture 1

CS 11 Haskell track: lecture 1 CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of

More information

CS457/557 Functional Languages

CS457/557 Functional Languages CS457/557 Functional Languages Spring 2018 Lecture 1: Course Introduction Andrew Tolmach Portland State University (with thanks to Mark P. Jones) 1 Goals of this course Introduce the beautiful ideas of

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee 1 Contents 1. Historical Background of Haskell 2. Lazy, Pure, and Functional Language 3. Using ghc and ghci 4. Functions 5. Haskell Scripts

More information

Introduction. chapter Functions

Introduction. chapter Functions chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main

More information

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G.

Writing a Lexer. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, Glenn G. Writing a Lexer CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, February 6, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks

More information

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

More information

An introduction introduction to functional functional programming programming using usin Haskell

An introduction introduction to functional functional programming programming using usin Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program

More information

Concepts of Programming Languages

Concepts of Programming Languages Concepts of Programming Languages Lecture 15 - Functional Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages

More information

Writing an Interpreter Thoughts on Assignment 6

Writing an Interpreter Thoughts on Assignment 6 Writing an Interpreter Thoughts on Assignment 6 CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, March 27, 2017 Glenn G. Chappell Department of Computer Science

More information

Programming Language Pragmatics

Programming Language Pragmatics Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken Alan Turing, Alonzo Church, Stephen

More information

Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright 2016 Elsevier 1 Chapter11_Functional_Languages_4e - Tue November 21, 2017 Historical Origins The imperative

More information

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL CIS 194: Homework 3 Due Wednesday, February 11, 2015 Interpreters An interpreter is a program that takes another program as an input and evaluates it. Many modern languages such as Java 1, Javascript,

More information

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered

More information

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

Programming Paradigms

Programming Paradigms PP 2017/18 Unit 11 Functional Programming with Haskell 1/37 Programming Paradigms Unit 11 Functional Programming with Haskell J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE

More information

Introduction to Syntax Analysis Recursive-Descent Parsing

Introduction to Syntax Analysis Recursive-Descent Parsing Introduction to Syntax Analysis Recursive-Descent Parsing CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 10, 2017 Glenn G. Chappell Department of

More information

Scheme: Strings Scheme: I/O

Scheme: Strings Scheme: I/O Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of

More information

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted

More information

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

More information

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS 6110 S14 Lecture 1 Introduction 24 January 2014 CS 6110 S14 Lecture 1 Introduction 24 January 2014 1 Introduction What is a program? Is it just something that tells the computer what to do? Yes, but there is much more to it than that. The basic expressions

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator 1 2 This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator (total) and a counter (i); it works by assigning

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 21 November, 2011 1 / 19 1 2 3 4 2 / 19 Semantics for programming

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

Functional Programming

Functional Programming The Meta Language (ML) and Functional Programming Daniel S. Fava danielsf@ifi.uio.no Department of informatics University of Oslo, Norway Motivation ML Demo Which programming languages are functional?

More information

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Functional Programming Lecture 1: Introduction

Functional Programming Lecture 1: Introduction Functional Programming Lecture 1: Introduction Viliam Lisý Artificial Intelligence Center Department of Computer Science FEE, Czech Technical University in Prague viliam.lisy@fel.cvut.cz Acknowledgements

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Compiler Theory. (Semantic Analysis and Run-Time Environments) Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 Glasgow Haskell Compiler GHC is the leading implementation of Haskell, and comprises a compiler and interpreter; The interactive nature of the interpreter

More information

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

Haskell Types, Classes, and Functions, Currying, and Polymorphism 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Types, Classes, and Functions, Currying, and Polymorphism 2 Types A type is a collection of related values. For example, Bool contains the

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

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

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

More information

Introduction to Haskell

Introduction to Haskell Introduction to Haskell Matt Mullins Texas A&M Computing Society October 6, 2009 Matt Mullins (TACS) Introduction to Haskell October 6, 2009 1 / 39 Outline Introduction to Haskell Functional Programming

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 27 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 18 November, 2014 1 / 18 Two parallel pipelines A large proportion

More information

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 440: Programming Languages and Translators, Spring 2019 Mon Haskell, Part 4 CS 440: Programming Languages and Translators, Spring 2019 Mon 2019-01-28 More Haskell Review definition by cases Chapter 6: Higher-order functions Revisit currying map, filter Unnamed

More information

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019 Comp 411 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright January 11, 2019 Top Down Parsing What is a context-free grammar (CFG)? A recursive definition of a set of strings; it is

More information

Higher Order Functions in Haskell

Higher Order Functions in Haskell Higher Order Functions in Haskell Evan Misshula 2018-09-10 Outline Curried Functions Curried comparison Example partial application partial application of a string function Returned functions ZipWith flip

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

More information

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell CS 360: Programming Languages Lecture 10: Introduction to Haskell Geoffrey Mainland Drexel University Thursday, February 5, 2015 Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia

More information

CS 415 Midterm Exam Spring 2002

CS 415 Midterm Exam Spring 2002 CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming

More information

4/19/2018. Chapter 11 :: Functional Languages

4/19/2018. Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen

More information

7. Introduction to Denotational Semantics. Oscar Nierstrasz

7. Introduction to Denotational Semantics. Oscar Nierstrasz 7. Introduction to Denotational Semantics Oscar Nierstrasz Roadmap > Syntax and Semantics > Semantics of Expressions > Semantics of Assignment > Other Issues References > D. A. Schmidt, Denotational Semantics,

More information

Functional Programming

Functional Programming Functional Programming Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 5, 2017 Practical Information The course is split in two I am responsible

More information

COS 320. Compiling Techniques

COS 320. Compiling Techniques Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

More information

Logical Methods in... using Haskell Getting Started

Logical Methods in... using Haskell Getting Started Logical Methods in... using Haskell Getting Started Jan van Eijck May 4, 2005 Abstract The purpose of this course is to teach a bit of functional programming and logic, and to connect logical reasoning

More information

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Ori Bar El Maxim Finkel 01/11/17 1 History Haskell is a lazy, committee designed, pure functional programming language that

More information

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements Programming Languages Third Edition Chapter 9 Control I Expressions and Statements Objectives Understand expressions Understand conditional statements and guards Understand loops and variation on WHILE

More information

Haskell Scripts. Yan Huang

Haskell Scripts. Yan Huang Haskell Scripts Yan Huang yh33@indiana.edu Last Quiz Objectives Writing Haskell programs in.hs files Note some differences between programs typed into GHCi and programs written in script files Operator

More information

Semantics of programming languages

Semantics of programming languages Semantics of programming languages Informatics 2A: Lecture 28 Mary Cryan School of Informatics University of Edinburgh mcryan@inf.ed.ac.uk 21 November 2018 1 / 18 Two parallel pipelines A large proportion

More information

Types and Type Inference

Types and Type Inference Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

More information

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell) Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 13 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 16 October, 2012 1 / 21 1 Types 2 3 4 2 / 21 Thus far

More information

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE 341, Autumn 2015, Ruby Introduction Summary CSE 341, Autumn 2015, Ruby Introduction Summary Disclaimer: This lecture summary is not necessarily a complete substitute for atting class, reading the associated code, etc. It is designed to be a useful

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413 Type Inference Systems CS412/CS413 Introduction to Compilers Tim Teitelbaum Type inference systems define types for all legal programs in a language Type inference systems are to type-checking: As regular

More information

Introduction to OCaml

Introduction to OCaml Fall 2018 Introduction to OCaml Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References Learn X in Y Minutes Ocaml Real World OCaml Cornell CS 3110 Spring 2018 Data Structures and Functional

More information

Programming Systems in Artificial Intelligence Functional Programming

Programming Systems in Artificial Intelligence Functional Programming Click to add Text Programming Systems in Artificial Intelligence Functional Programming Siegfried Nijssen 8/03/16 Discover thediscover world at the Leiden world University at Leiden University Overview

More information

Functional Programming and Haskell

Functional Programming and Haskell Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming

More information

Chapter 6 Control Flow. June 9, 2015

Chapter 6 Control Flow. June 9, 2015 Chapter 6 Control Flow June 9, 2015 Expression evaluation It s common in programming languages to use the idea of an expression, which might be a simple object function invocation over some number of arguments

More information

Programming Languages Fall 2013

Programming Languages Fall 2013 Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy

More information

3. Functional Programming. Oscar Nierstrasz

3. Functional Programming. Oscar Nierstrasz 3. Functional Programming Oscar Nierstrasz Roadmap > Functional vs. Imperative Programming > Pattern Matching > Referential Transparency > Lazy Evaluation > Recursion > Higher Order and Curried Functions

More information

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

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming About the Tutorial Haskell is a widely used purely functional language. Functional programming is based on mathematical functions. Besides Haskell, some of the other popular languages that follow Functional

More information

Programming Languages, Summary CSC419; Odelia Schwartz

Programming Languages, Summary CSC419; Odelia Schwartz Programming Languages, Summary CSC419; Odelia Schwartz Chapter 1 Topics Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation Criteria Influences on Language Design

More information

Software System Design and Implementation

Software System Design and Implementation Software System Design and Implementation Functional Programming Gabriele Keller The University of New South Wales School of Computer Science and Engineering Sydney, Australia COMP3141 16s1 Course software

More information

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey CS 3110 Lecture 6: Map and Fold Prof. Clarkson Fall 2014 Today s music: Selections from the soundtrack to 2001: A Space Odyssey Review Features so far: variables, operators, let expressions, if expressions,

More information

Java Bytecode (binary file)

Java Bytecode (binary file) Java is Compiled Unlike Python, which is an interpreted langauge, Java code is compiled. In Java, a compiler reads in a Java source file (the code that we write), and it translates that code into bytecode.

More information

Intro to Haskell Notes: Part 5

Intro to Haskell Notes: Part 5 Intro to Haskell Notes: Part 5 Adrian Brasoveanu October 5, 2013 Contents 1 Curried functions and related issues 1 1.1 Curried functions......................................... 1 1.2 Partially applied

More information

COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

More information

CS1 Lecture 3 Jan. 18, 2019

CS1 Lecture 3 Jan. 18, 2019 CS1 Lecture 3 Jan. 18, 2019 Office hours for Prof. Cremer and for TAs have been posted. Locations will change check class website regularly First homework assignment will be available Monday evening, due

More information

An introduction to functional programming. July 23, 2010

An introduction to functional programming. July 23, 2010 An introduction to functional programming July 23, 2010 About Outline About About What is functional programming? What is? Why functional programming? Why? is novel. is powerful. is fun. About A brief

More information

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming The ML Programming Language General purpose programming language designed by Robin Milner in 1970 Meta Language

More information

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

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming The ML Programming Language General purpose programming language designed by Robin Milner in 1970 Meta Language

More information

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

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona 1/37 CSc 372 Comparative Programming Languages 2 : Functional Programming Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/37 Programming Paradigms

More information

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey CS 3110 Lecture 6: Map and Fold Prof. Clarkson Spring 2015 Today s music: Selections from the soundtrack to 2001: A Space Odyssey Review Course so far: Syntax and semantics of (most of) OCaml Today: No

More information

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018 CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018 Typical workflow concrete syntax (string) "(fn x => x + x) 4" Parsing Possible errors / warnings

More information

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

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction. Practical Practical An introduction to functional programming July 21, 2011 Contents Practical Practical is fun, and that s what it s all about! Even if seems strange to you at first, don t give up. Learning

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341, Spring 2013, Final Examination June 13, 2013 CSE341, Spring 2013, Final Examination June 13, 2013 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, except for both sides of one 8.5x11in piece of paper. Please stop

More information

CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 3 Jan. 22, 2018 CS1 Lecture 3 Jan. 22, 2018 Office hours for me and for TAs have been posted, locations will change check class website regularly First homework available, due Mon., 9:00am. Discussion sections tomorrow

More information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Tuesday, February 19, 2013 The lambda calculus (or λ-calculus) was introduced by Alonzo Church and Stephen Cole Kleene in

More information

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell) Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 14 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 17 October 2017 1 / 21 1 Types 2 3 4 2 / 21 So far in

More information

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE 3302 Programming Languages Lecture 8: Functional Programming CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1 Functional Programming Languages

More information