CSc 372. Comparative Programming Languages. 3 : Haskell Introduction. Department of Computer Science University of Arizona

Similar documents
CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

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

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

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

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

Introduction to Haskell

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

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

CSc 520. Principles of Programming Languages 11: Haskell Basics

Functional Programming for Logicians - Lecture 1

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Lecture 10 September 11, 2017

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CS 320: Concepts of Programming Languages

CSc 520. Gofer III. Accumulative Recursion. Accumulative Recursion... Stack Recursion. Principles of Programming Languages. Christian Collberg

Standard prelude. Appendix A. A.1 Classes

CS 340 Spring 2019 Midterm Exam

CSc 372 Comparative Programming Languages

CSc 372. Comparative Programming Languages. 1 : Introduction. Department of Computer Science University of Arizona

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Programming Paradigms

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

Background Operators (1E) Young Won Lim 7/7/18

CSc 520 Principles of Programming Languages

CS457/557 Functional Languages

Logic - CM0845 Introduction to Haskell

HIGHER-ORDER FUNCTIONS

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

CSc 372 Comparative Programming Languages

Functional Programming in Haskell for A level teachers

Carlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

CSc 372 Comparative Programming Languages

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

The Haskell HOP: Higher-order Programming

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

A tour of the Haskell Prelude

CS 457/557: Functional Languages

An introduction introduction to functional functional programming programming using usin Haskell

Monad Background (3A) Young Won Lim 11/18/17

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

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

COMP 3141 Software System Design and Implementation

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Modules. Input and Output. Urtė Zubkaitė

Programming Languages Fall 2013

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

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

Monad Background (3A) Young Won Lim 11/8/17

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

Course year Typeclasses and their instances

CSE 3302 Programming Languages Lecture 8: Functional Programming

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Lecture 8: Summary of Haskell course + Type Level Programming

Shell CSCE 314 TAMU. Functions continued

Introduction. chapter Functions

Principles of Programming Languages

CSc 372. Comparative Programming Languages. 29 : Ruby Blocks. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages

CSc 520. Principles of Programming Languages 25: Types Introduction

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

Functional Logic Programming Language Curry

Programming Paradigms

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

CSCE 314 Programming Languages

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

Functional Programming TDA 452, DIT 142

Functional Programming. Overview. Topics. Recall λ-terms. Examples

CS 320 Midterm Exam. Fall 2018

Questions & Answers 28 Sept.

Side Effects (3B) Young Won Lim 11/20/17

CSCI 2041: Lazy Evaluation

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

Functional Programming and Haskell

Programming Languages Fall Prof. Liang Huang

CS 360: Programming Languages Lecture 12: More Haskell

Programming Paradigms: Exercises

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Haskell through HUGS THE BASICS

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

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

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Shell CSCE 314 TAMU. Haskell Functions

INTRODUCTION TO FUNCTIONAL PROGRAMMING

CS 360: Programming Languages Lecture 10: Introduction to Haskell

A Brief Haskell and GHC Refresher

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads

IO Monad (3D) Young Won Lim 1/18/18

Abstract Interpretation Using Laziness: Proving Conway s Lost Cosmological Theorem

Transcription:

1/17 CSc 372 Comparative Programming Languages 3 : Haskell Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg

2/17 What is Haskell? Haskell is a functional programming language. We study Haskell because, compared to other functional languages 1 Haskell is statically typed (the signature of all functions and the types of all variables are known prior to execution); 2 Haskell uses lazy rather than eager evaluation (expressions are only evaluated when needed); 3 Haskell uses type inference to assign types to expressions, freeing the programmer from having to give explicit types; 4 Haskell is pure (it has no side-effects).

3/17 What is Haskell?... Haskell implementations are also interactive which means that the user interface is like a calculator; you enter expressions, the Haskell interpreter checks them, evaluates them, and prints the result. This is called the read-eval-print loop: Read Eval Print > hugs Prelude> (2*5)+3 13

4/17 What is Haskell?... > hugs Prelude> :load /usr/lib64/hugs/demos/eliza.hs Eliza> eliza Hi! I m Eliza. I am your personal therapy computer. Please tell me your problem. > hello How do you...please state your problem. > i m bored! Did you come to me because you are bored?

5/17 What is Haskell?... eliza = interact (writestr hi $ session initial []) where hi = "\n\ \Hi! I m Eliza. I am your personal...\n\ \Please tell me your problem.\n\ \\n" session rs prev = readline "> " (\l -> let ws = words (trim l) (response,rs ) = if prev==ws then repeated rs else answer rs in writestr (response ++ "\n\n") $ session rs ws)

6/17 commaint A Haskell Program Real functional programs are, naturally, a bit more complex. They make heavy use of 1 higher-order functions, functions which take functions as arguments. 2 function composition, which is a way to combine simple functions into more powerful ones. 3 function libraries, collections of functions that have proven useful. The standard.prelude that you ve seen that the Haskell interpreter loads on start-up, is one such collection.

7/17 So what does a real functional Haskell program look like? Let s have a quick look at one simple (?) function, commaint. commaint works on strings, which are simply lists of characters. You are not supposed to understand this! Yet... From the commaint documentation: [commaint] takes a single string argument containing a sequence of digits, and outputs the same sequence with commas inserted after every group of three digits,

8/17? commaint "1234567" 1,234,567 Sample interaction: commaint in Haskell: commaint = reverse. foldr1 (\x y->x++","++y). group 3. reverse where group n = takewhile (not.null). map (take n).iterate (drop n)

9/17 "1234567" reverse "7654321" iterate (drop 3) ["7654321","4321","1","","",...] map (take 3) ["765","432","1","","",...] takewhile (not.null) g r o u p 3 ["765", "432", "1"] foldr1 (\x y >x++","++y) "765,432,1" reverse "1,234,567"

10/17 commaint in Haskell: commaint = reverse. foldr1 (\x y->x++","++y). group 3. reverse where group n = takewhile (not.null). map (take n).iterate (drop n) commaint in English: First reverse the input string. Take the resulting string and separate into chunks of length 3. Then append the chunks together, inserting a comma between chunks. Reverse the resulting string.

11/17 commaint = reverse. foldr1 (\x y->x++","++y). group 3. reverse where group n = takewhile (not.null). map (take n).iterate (drop n) group n is a local function. It takes a string and an integer as arguments. It divides the string up in chunks of length n. reverse reverses the order of the characters in a string. drop n xs returns the string that remains when the first n characters of xs are removed.

12/17 commaint = reverse. foldr1 (\x y->x++","++y). group 3. reverse where group n = takewhile (not.null). map (take n).iterate (drop n) iterate (drop 3) s returns the infinite (!) list of strings [s, drop 3 s, drop 3 (drop 3 s), drop 3 (drop 3 (drop 3 s)), ] take n s returns the first n characters of s.

13/17 commaint = reverse. foldr1 (\x y->x++","++y). group 3. reverse where group n = takewhile (not.null). map (take n).iterate (drop n) map (take n) s takes a list of strings as input. It returns another list of strings, where each string has been shortened to n characters. (take n) is a function argument to map. takewhile (not.null) removes all empty strings from a list of strings.

14/17 commaint = reverse. foldr1 (\x y->x++","++y). group 3. reverse where group n = takewhile (not.null). map (take n).iterate (drop n) foldr1 (\x y->x++","++y) s takes a list of strings s as input. It appends the strings together, inserting a comma inbetween each pair of strings.

15/17 Since Haskell is an interactive language, we can always try out (parts of) functions that we don t understand.? reverse "1234567" 7654321? take 3 "dasdasdasd" das? map (take 3) ["1234","23423","45324",""] ["123", "234", "453", []]? iterate (drop 3) "7654321" ["7654321", "4321", "1", [], [], {interrupt!}

16/17 hugs vs. ghci vs.... There are several implementations of Haskell. They are mostly the same, but differ in which libraries they support. In these slides the examples use the hugs Haskell interpreter. A better choice these days is the Haskell platform, which you can download from here: http://hackage.haskell.org/platform. The Haskell platform comes with the ghci Haskell interpreter.

17/17 ghci modules To get some of the examples in these slides to work you may need to import some libraries that ghci needs but that hugs loads automatically. Here s a list of ghci libraries: http://www.haskell.org/ghc/docs/latest/html/libraries. In particular, you may need these libraries: Data.Char (for character operations such as toupper) Data.List (for list operations such as sort) To load these libraries in your programs say import Data.Char import Data.List To load these libraries interactively when running ghci, type :m Data.Char Data.List