Haskell Review COMP360

Similar documents
Functional Languages. Hwansoo Han

Chapter 11 :: Functional Languages

Programming Language Pragmatics

Haskell Types COMP360

Haskell Types COMP360

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

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

To figure this out we need a more precise understanding of how ML works

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

CSE 113 A. Announcements - Lab

To figure this out we need a more precise understanding of how ML works

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

Functional Programming

Functional Programming, Tail Recursion, & Spooky Stamps

Programming Paradigms

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

CS 340 Spring 2019 Midterm Exam

Bjarne Stroustrup. creator of C++

Functional Programming for Logicians - Lecture 1

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Chapter 15. Functional Programming Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Math 152: Applicable Mathematics and Computing

Randomized Algorithms: Element Distinctness

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

COMP3141 Assignment 2

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Functional Programming

Functional Programming Mid-term exam Tuesday 3/10/2017

Which of the following expressions has the type int list?

CS457/557 Functional Languages

Lecture #2: Programming Structures: Loops and Functions

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

Announcements Wednesday, August 23

MORE RECURSIVE FUNCTIONS

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Introduction to Haskell

JVM ByteCode Interpreter

61A Lecture 4. Monday, September 9

CS61A Notes Disc 11: Streams Streaming Along

Announcements Wednesday, August 23

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Programming in C ++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Scheduling. Monday, November 22, 2004

Introduction to Functional Programming

Example Scheme Function: equal

Shell CSCE 314 TAMU. Haskell Functions

Programming Paradigms Written Exam

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CS 11 Haskell track: lecture 1

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Haskell An Introduction

Functional Programming in C++

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Thoughts on Assignment 4 Haskell: Flow of Control

Lecture 8: Summary of Haskell course + Type Level Programming

Chapter 6 Control Flow. June 9, 2015

CS 314 Principles of Programming Languages. Lecture 16

CS 3304 Comparative Languages. Lecture 1: Introduction

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

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

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

Programming Paradigms

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Programming Languages and Techniques (CIS120)

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

Imperative languages

CS162 Week 1. Kyle Dewey. Friday, January 10, 14

List Functions, and Higher-Order Functions

CSE 3302 Programming Languages Lecture 8: Functional Programming

An introduction introduction to functional functional programming programming using usin Haskell

CSCE 314 Programming Languages

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

Exam for 2G1512 DatalogiII, Apr 12th

Loops. GEEN163 Introduction to Computer Programming

MITOCW watch?v=kz7jjltq9r4

Shell CSCE 314 TAMU. Higher Order Functions

Skill 1: Multiplying Polynomials

CS 3110 Lecture 1 Course Overview

Concepts of Programming Languages

Programming Languages and Techniques (CIS120)

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

UNIT 2B An Introduction to Programming. Announcements

Arrays and while Loops. Lecture 15 - COMP110 - Spring 2018

Functional Programming Languages (FPL)

Compilers. Computer Science 431

CS 360: Programming Languages Lecture 12: More Haskell

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Haskell Refresher Informatics 2D

Is Functional Programming (FP) for me? ACCU Conference 2008 Hubert Matthews

Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

Transcription:

Haskell Review COMP360

Some people talk in their sleep. Lecturers talk while other people sleep Albert Camus

Remaining Schedule Friday, April 7 Haskell Monday, April 10 Logic Programming read chapter 12 Monday, April 17 Types Monday, April 24 Concurrent Programming read chapter 13 Monday, May 1 Exam 3 Wednesday, April 12 Prolog Wednesday, April 19 Drag & drop programming Wednesday, April 26 Concurrent Programming Wednesday, May 3 final review Friday, April 14 Good Friday (no classes) Friday, April 21 Drag & drop programming Friday, April 28 Concurrent Programming Tuesday, May 9 Final Exam 10:30am 12:30pm

Return Values Functional programs return values Imagine the first word in each Haskell function is RETURN Whatever value the function creates is returned

Recursion Basics A recursive method calls itself with a (usually) smaller parameter A recursive method must have some base case that indicates when the recursion stops Most recursive functions (any language) start with an if statement to determine if this is the base case

Recursion not Iteration Recursion is used to repeat something, not iteration A method to sum all elements of a list could be: mysum str = if length str == 0 then 0 else head str + mysum (tail str )

Logical NOT The built-in not function in Haskell performs a logical inverse not (4 < 5) returns False not (6 > 9) returns True

Infinite Lists You can create an infinite list is Haskell [3,5..] creates [3,5,7,9,11,13,15,17 and so forth] Haskell uses lazy evaluation. It will not create a list until it needs to do so take 4 [3,5..] returns [3,5,7,9] Haskell only had to create the first four elements of the list

List Comprehensions List comprehensions build lists You can specify the requirements for the elements of the list The elements of the list can come from one or more existing lists Sometimes it is useful to build lists from newly created lists, such as [1,2..N] Recursion is usually not required with list comprehensions

List comprehensions A list comprehension contains [ output function input set, predicate] such as [x*x x <- [0..5]] returns [0,1,4,9,16,25] or [x*x x <- [0..10], x*x < 50] returns [0,1,4,9,16,25,36,49]

Multiple Predicates You can specify multiple restrictions on the elements of a list An element is included only if it meets all predicates Think of the predicates as being ANDed together tryit cat = [ x x <- cat, x > 5, x < 30]

Multiple Lists A list comprehension may have input from multiple lists Assume you want to create a list of tuples with all combinations of two lists allpair cow bull = [(x,y) x <- cow, y <- bull ] allpair ["COMP", "ECE", "MATH"] [1,2] returns [("COMP",1),("COMP",2),("ECE",1),("ECE",2),("MATH",1),("MATH",2)]

Infinite Lists You need to be careful with infinite lists in comprehensions Assume you want to create a list of tuples numbering all the items in a list allpair cow = [(x,y) x <- cow, y <- [1..] ] allpair ["COMP", "ECE", "MATH"] returns [(1,"COMP"),(2,"COMP"),(3,"COMP"),[(4,"COMP"), (5,"COMP"),(6,"COMP"), forever

Parameter Values You can specify values in the parameters of a function If the function is called with that specific parameter, that definition of the function will be used roll 7 = "you win" roll 11 = "you win" roll 2 = "you lose" roll x = "you continue" -- matches anything

Matching List Parameters You can match an empty list The form [h:tl] matches a list where h is the head and tl is the tail doublesum :: (Num a) => [a] -> a doublesum [] = 0 doublesum (h:tl) = 2 * h + doublesum tl

Observations Do not use "a" or "t" as variable names Avoid duplicating existing function names Do not use tab in a function definition Functions should start in column 1 of a file

Functional Programming Advantages lack of side effects makes programs easier to understand lack of explicit evaluation order (in some languages) offers possibility of parallel evaluation (e.g. MultiLisp) lack of side effects and explicit evaluation order simplifies some things for a compiler (provided you don't blow it in other ways) programs are often surprisingly short language can be extremely small and yet powerful

Functional Programming Problems difficult (but not impossible!) to implement efficiently on von Neumann machines lots of copying of data through parameters (apparent) need to create a whole new array in order to change one element heavy use of pointers (space/time and locality problem) frequent procedure calls heavy space use for recursion requires garbage collection requires a different mode of thinking by the programmer difficult to integrate I/O into purely functional model

Remaining Schedule Friday, April 7 Haskell Monday, April 10 Logic Programming read chapter 12 Monday, April 17 Types Monday, April 24 Concurrent Programming read chapter 13 Monday, May 1 Exam 3 Wednesday, April 12 Prolog Wednesday, April 19 Drag & drop programming Wednesday, April 26 Concurrent Programming Wednesday, May 3 final review Friday, April 14 Good Friday (no classes) Friday, April 21 Drag & drop programming Friday, April 28 Concurrent Programming Tuesday, May 9 Final Exam 10:30am 12:30pm