Lecture 1 August 9, 2017

Similar documents
Introduction to Programming, Aug-Dec 2008

Functional Programming in Haskell Part I : Basics

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS457/557 Functional Languages

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

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

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Introduction to Functional Programming. Slides by Koen Claessen and Emil Axelsson

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Pace University. Fundamental Concepts of CS121 1

Programming Language Concepts: Lecture 14

CS 320: Concepts of Programming Languages

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

Topic 1: Introduction

Haskell Scripts. Yan Huang

Basic types and definitions. Chapter 3 of Thompson

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

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

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Getting Started with Haskell (10 points)

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Abstract Types, Algebraic Types, and Type Classes

CS 11 Haskell track: lecture 1

First Haskell Exercises

CSCE 314 Programming Languages

CS 360: Programming Languages Lecture 10: Introduction to Haskell

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

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

Programming in Haskell Aug Nov 2015

CSc 520. Principles of Programming Languages 11: Haskell Basics

CS1 Lecture 3 Jan. 18, 2019

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

22c:111 Programming Language Concepts. Fall Types I

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Getting Started with Haskell (10 points)

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

Introduction to Programming: Lecture 3

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

GHCi: Getting started (1A) Young Won Lim 5/26/17

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

CS 320: Concepts of Programming Languages

Exercise 1 ( = 24 points)

Module 10: Imperative Programming, Modularization, and The Future

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

Programming Paradigms

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

Types and Static Type Checking (Introducing Micro-Haskell)

Intro to Haskell Notes: Part 5

CSci 1113, Fall 2015 Lab Exercise 4 (Week 5): Write Your Own Functions. User Defined Functions

Object Oriented Methods : Deeper Look Lecture Three

Types and Static Type Checking (Introducing Micro-Haskell)

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSCE 314 Programming Languages

REVIEW. The C++ Programming Language. CS 151 Review #2

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

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Haskell through HUGS THE BASICS

Programming Languages Fall 2013

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Exercise: Using Numbers

Key Differences Between Python and Java

Programming Language Concepts, cs2104 Lecture 04 ( )

Full file at

Introductory Example

Algorithms and Programming I. Lecture#12 Spring 2015

Bits, Words, and Integers

CS1 Lecture 3 Jan. 22, 2018

Lesson 10: Representing, Naming, and Evaluating Functions

Logic - CM0845 Introduction to Haskell

Variables. Data Types.

SAMS Programming - Section C. Lecture 1: Introduction + Basic Building Blocks of Programming

CS558 Programming Languages

Lecture 5: Lazy Evaluation and Infinite Data Structures

Side Effects (3A) Young Won Lim 1/13/18

Functional Programming for Logicians - Lecture 1

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Exercise 1 ( = 22 points)

Bits and Bytes January 13, 2005

CSCE 121 ENGR 112 List of Topics for Exam 1

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

An introduction to functional programming. July 23, 2010

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lecture 19: Functions, Types and Data Structures in Haskell

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

More Lambda Calculus and Intro to Type Systems

Haskell An Introduction

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Introduction to Haskell

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

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

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

CIS 110: Introduction to Computer Programming

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

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Transcription:

Programming in Haskell S P Suresh http://www.cmi.ac.in/~spsuresh Lecture 1 August 9, 2017

Administrative Mondays and Wednesdays at 9.10 am at Lecture Hall 6 Evaluation: Quizzes, 4 5 programming assignments, exams TAs: Agnishom Chattopadhyay, Kishlaya Jaiswal Moodle page: http://moodle.cmi.ac.in/course/view.php?id=231 Course page: http://www.cmi.ac.in/~spsuresh/teaching/prgh17

Resources http://www.haskell.org Introduction to Functional Programming using Haskell (Richard Bird) Thinking Functionally with Haskell (Richard Bird) Real World Haskell http://book.realworldhaskell.org/read/ Learn You a Haskell for Great Good! http://learnyouahaskell.com/chapters Haskell Programming: from first principles (Christopher Allen & Julie Moronuki) http://haskellbook.com Plenty of other resources

Programs as functions Functions transform inputs to outputs x f f(x) Program: rules to produce output from input Computation: process of applying the rules

Building up programs How do we describe the rules? Start with built in functions Use these to build more complex functions

Building up programs Suppose... we have the whole numbers, {0,1,2, } and the successor function, succ succ 0 = 1 succ 1 = 2 succ 2 = 3 Note: we that write succ 0, not succ(0)

Building up programs We can compose succ twice to build a new function plustwo n = succ (succ n) If we compose plustwo and succ we get plusthree n = succ (plustwo n)

Building up programs How do we define plus? plus n m means apply succ to n, m times Again note: plus n m, not plus(n,m) plus n 1 = succ n plus n 2 = succ (plus n 1) = succ (succ n) plus n i = succ(succ( (succ n) ) i times How do we capture this rule for all n, i

Inductive/recursive definitions plus n 0 = n, for every n plus n 1 = succ n = succ (plus n 0) Assume we know how to compute plus n m Then, plus n (succ m) is succ (plus n m)

Computation Unravel the definition plus 7 3 = plus 7 (succ 2) = succ (plus 7 2) = succ (plus 7 (succ 1)) = succ (succ (plus 7 1)) = succ (succ (plus 7 (succ 0))) = succ (succ (succ (plus 7 0))) = succ (succ (succ 7))

Recursive definitions Multiplication is repeated addition mult n m means apply plus n, m times mult n 0 = 0, for every n mult n (succ m) = plus n (mult n m)

Summary Functional programs are rules describing how outputs are derived from inputs Basic operation is function composition Recursive definitions allow repeated function composition, depending on the input

Building up programs Start with built in functions Use function composition, recursive definitions to build more complex functions What kinds of values do functions manipulate?

Types Functions work on values of a fixed type succ takes a whole number as input and produces a whole number as output plus and mult take two whole numbers as input and produce a whole number as output Can also define analogous functions for real numbers

Types How about sqrt, the square root function? Even if the input is a whole number, the output need not be may have a fractional part Number with fractional values are a different type from whole numbers In Mathematics, whole numbers are often treated as a subset of fractional or real numbers

Types Other types capitalize a = A, capitalize b = B, Inputs and outputs are letters or characters

Functions and types We will be careful to ensure that any function we define has a well defined type The function plus that adds two whole numbers will be different from another function plus that adds two fractional numbers

Functions have types A function that takes inputs of type A and produces output of type B has a type A B In Mathematics, we write f: S T for a function with domain S and codomain T A type is a just a set of permissible values, so this is equivalent to providing the type of f

Collections It is often convenient to deal with collections of values of a given type A list of integers A sequence of characters words or strings Pairs of numbers Such collections are also types of values

Summary Functions manipulate values Each input and output value comes from a well defined set of possible values a type We will only allow functions whose type can be defined Functions themselves inherit a type Collections of values also types

Haskell A programming language for describing functions A function description has two parts Type of inputs and outputs Rule for computing outputs from inputs Example sqr :: Int -> Int sqr x = x * x Type definition Computation rule

Basic types Int, Integers Operations: +, -, *, / (Note: / produces Float) Functions: div, mod Float, Floating point ( real numbers ) Char, Characters, a, %, 7, Bool, Booleans, True and False

Basic types Bool, Booleans, True and False Boolean expressions Operations: &&,, not Relational operators to compare Int, Float, ==, /=, <, <=, >, >=

Defining functions xor (Exclusive or) Input two values of type Bool Check that exactly one of them is True xor :: Bool -> Bool -> Bool (why?) xor b1 b2 = (b1 && (not b2)) ((not b1) && b2)

Defining functions inorder Input three values of type Int Check that the numbers are in order inorder :: Int -> Int -> Int -> Bool inorder x y z = (x <= y) && (y <= z)

Pattern matching Multiple definitions, by cases xor :: Bool -> Bool -> Bool xor True False = True xor False True = True xor b1 b2 = False Use first definition that matches, top to bottom xor False True matches second definition xor True True matches third definition

Pattern matching When does a function call match a definition? If the argument in the definition is a constant, the value supplied in the function call must be the same constant If the argument in the definition is a variable, any value supplied in the function call matches, and is substituted for the variable (the usual case)

Pattern matching Can mix constants and variables in a definition or :: Bool -> Bool -> Bool or True b = True or b True = True or b1 b2 = False or True False matches first definition or False True matches second definition or False False matches third definition

Pattern matching Another example and :: Bool -> Bool -> Bool and True b = b and False b = False In the first definition, the argument b is used in the definition In the second, b is ignored

Summary A Haskell function consists of a type definition and a computation rule Can have multiple rules for the same function Rules are matched top to bottom Use patterns to split cases

Running Haskell programs Haskell interpreter ghci Interactively call builtin functions Load user-defined Haskell code from a text file Similar to how Python works

Setting up ghci Download and install the Haskell Platform https://www.haskell.org/platform/ Available for Windows, Linux, macos

Using ghci Create a text file (extension.hs) with your Haskell function definitions Run ghci at the command prompt Load your Haskell code :load myfile.hs Call functions interactively within ghci

Compiling ghc is a compiler that creates a standalone executable from a.hs file ghc stands for Glasgow Haskell Compiler ghci is the associated interpreter Using ghc requires some advanced concepts We will come to this later in the course

Summary ghci is a user-friendly interpreter Can load and interactively execute user defined functions ghc is a compiler But we need to know more Haskell before we can use it