PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Similar documents
PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CSCE 314 Programming Languages

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

Haskell Scripts. Yan Huang

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

CS457/557 Functional Languages

Introduction. chapter Functions

Lecture 1 August 9, 2017

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

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

First Haskell Exercises

Functional Programming in Haskell for A level teachers

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

Introduction to Programming, Aug-Dec 2008

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

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

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

Haskell through HUGS THE BASICS

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

Programming Languages 3. Definition and Proof by Induction

CS 11 Haskell track: lecture 1

Shell CSCE 314 TAMU. Haskell Functions

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Introduction to Programming, Aug-Dec 2006

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

Template Haskell based implementation of printf

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

CS 320: Concepts of Programming Languages

INTRODUCTION TO FUNCTIONAL PROGRAMMING

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

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

Programming Language Concepts: Lecture 14

A Brief Haskell and GHC Refresher

Shell CSCE 314 TAMU. Functions continued

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

Computational Programming with Python

An introduction introduction to functional functional programming programming using usin Haskell

Functional Programming in Haskell Part I : Basics

List Functions, and Higher-Order Functions

Getting started with Hugs on Linux

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

SECOND PUBLIC EXAMINATION. Compilers

CSCC24 Functional Programming Scheme Part 2

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lecture 19: Functions, Types and Data Structures in Haskell

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

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

Desktop Command window

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

Lists. Michael P. Fourman. February 2, 2010

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

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Scheme: Expressions & Procedures

CSc 520. Principles of Programming Languages 11: Haskell Basics

CSc 372 Comparative Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

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

LECTURE 16. Functional Programming

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

Linux Shell Script. J. K. Mandal

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

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

SML A F unctional Functional Language Language Lecture 19

Functional Languages. Hwansoo Han

Programming Paradigms

Formal Systems and their Applications

Logic - CM0845 Introduction to Haskell

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

A Brief Introduction to Scheme (I)

Introduction to Scheme

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme

Algorithms and Programming I. Lecture#12 Spring 2015

Programming Languages Third Edition

Functional Programming Lecture 1: Introduction

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

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

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

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Concepts of programming languages

This book is licensed under a Creative Commons Attribution 3.0 License

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

11.3 Function Prototypes

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Basic concepts. Chapter Toplevel loop

Lecture 8: Summary of Haskell course + Type Level Programming

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

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Applicative, traversable, foldable

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

Functional Programming

FP Foundations, Scheme

Python Programming Exercises 1

Transcription:

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 makes it well suited for teaching and prototyping; GHC is freely available from: www.haskell.org/platform 1

Starting GHCi The interpreter can be started from the terminal command prompt $ by simply typing ghci: $ ghci GHCi, version X: http://www.haskell.org/ghc/ :? for help Prelude> The GHCi prompt > means that the interpreter is now ready to evaluate an expression. 2

For example, it can be used as a desktop calculator to evaluate simple numeric expresions: > 2+3*4 14 > (2+3)*4 20 > sqrt (3^2 + 4^2) 5.0 3

The Standard Prelude Haskell comes with a large number of standard library functions. In addition to the familiar numeric functions such as + and *, the library also provides many useful functions on lists. Select the first element of a list: > head [1,2,3,4,5] 1 4

Remove the first element from a list: > tail [1,2,3,4,5] [2,3,4,5] Select the nth element of a list: > [1,2,3,4,5]!! 2 3 Select the first n elements of a list: > take 3 [1,2,3,4,5] [1,2,3] 5

Remove the first n elements from a list: > drop 3 [1,2,3,4,5] [4,5] Calculate the length of a list: > length [1,2,3,4,5] 5 Calculate the sum of a list of numbers: > sum [1,2,3,4,5] 15 6

Calculate the product of a list of numbers: > product [1,2,3,4,5] 120 Append two lists: > [1,2,3] ++ [4,5] [1,2,3,4,5] Reverse a list: > reverse [1,2,3,4,5] [5,4,3,2,1] 7

Function Application In mathematics, function application is denoted using parentheses, and multiplication is often denoted using juxtaposition or space. f(a,b) + c d Apply the function f to a and b, and add the result to the product of c and d. 8

In Haskell, function application is denoted using space, and multiplication is denoted using *. f a b + c*d As previously, but in Haskell syntax. 9

Moreover, function application is assumed to have higher priority than all other operators. f a + b Means (f a) + b, rather than f (a + b). 10

Examples Mathematics f(x) f(x,y) f(g(x)) f(x,g(y)) f(x)g(y) Haskell f x f x y f (g x) f x (g y) f x * g y 11

Haskell Scripts As well as the functions in the standard library, you can also define your own functions; New functions are defined within a script, a text file comprising a sequence of definitions; By convention, Haskell scripts usually have a.hs suffix on their filename. This is not mandatory, but is useful for identification purposes. 12

My First Script When developing a Haskell script, it is useful to keep two windows open, one running an editor for the script, and the other running GHCi. Start an editor, type in the following two function definitions, and save the script as test.hs: double x = x + x quadruple x = double (double x) 13

Leaving the editor open, in another window start up GHCi with the new script: $ ghci test.hs Now both the standard library and the file test.hs are loaded, and functions from both can be used: > quadruple 10 40 > take (double 2) [1,2,3,4,5,6] [1,2,3,4] 14

Leaving GHCi open, return to the editor, add the following two definitions, and resave: factorial n = product [1..n] average ns = sum ns `div` length ns Note: div is enclosed in back quotes, not forward; x `f` y is just syntactic sugar for f x y. 15

GHCi does not automatically detect that the script has been changed, so a reload command must be executed before the new definitions can be used: > :reload Reading file "test.hs" > factorial 10 3628800 > average [1,2,3,4,5] 3 16

Useful GHCi Commands Command Meaning :load name load script name :reload reload current script :set editor name set editor to name :edit name edit script name :edit edit current script :type expr show type of expr :? show all commands :quit quit GHCi 17

Naming Requirements Function and argument names must begin with a lower-case letter. For example: myfun fun1 arg_2 x By convention, list arguments usually have an s suffix on their name. For example: xs ns nss 18

The Layout Rule In a sequence of definitions, each definition must begin in precisely the same column: a = 10 b = 20 c = 30 a = 10 b = 20 c = 30 a = 10 b = 20 c = 30 19

The layout rule avoids the need for explicit syntax to indicate the grouping of definitions. a = b + c where b = 1 c = 2 d = a * 2 means a = b + c where {b = 1; c = 2} d = a * 2 implicit grouping explicit grouping 20

Exercises (1) (2) Try out slides 2-7 and 13-16 using GHCi. Fix the syntax errors in the program below, and test your solution using GHCi. N = a div length xs where a = 10 xs = [1,2,3,4,5] 21

(3) Show how the library function last that selects the last element of a list can be defined using the functions introduced in this lecture. (4) Can you think of another possible definition? (5) Similarly, show how the library function init that removes the last element from a list can be defined in two different ways. 22