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

Introduction. chapter Functions

Haskell through HUGS THE BASICS

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

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

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

CS457/557 Functional Languages

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

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

CS 320: Concepts of Programming Languages

Programming Language Concepts: Lecture 14

An introduction introduction to functional functional programming programming using usin Haskell

Programming Languages 3. Definition and Proof by Induction

Introduction to Programming, Aug-Dec 2008

Shell CSCE 314 TAMU. Functions continued

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

Shell CSCE 314 TAMU. Haskell Functions

Introduction to Programming, Aug-Dec 2006

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

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

Getting started with Hugs on Linux

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Lecture 19: Functions, Types and Data Structures in Haskell

Functional Programming in Haskell for A level teachers

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

CSc 372 Comparative Programming Languages

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

List Functions, and Higher-Order Functions

CSCC24 Functional Programming Scheme Part 2

Linux Shell Script. J. K. Mandal

Formal Systems and their Applications

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

A Brief Introduction to Scheme (I)

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

Lists. Michael P. Fourman. February 2, 2010

Logical Methods in... using Haskell Getting Started

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

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

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

Computational Programming with Python

Scheme: Expressions & Procedures

Organization of Programming Languages CS3200/5200N. Lecture 11

11.3 Function Prototypes

LECTURE 16. Functional Programming

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

First Haskell Exercises

Desktop Command window

CSc 520. Principles of Programming Languages 11: Haskell Basics

Chapter 3: Programming with MATLAB

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Exercise 2: Automata Theory

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

Introduction to Scheme

lci Manual Kostas Chatzikokolakis 12 March 2006

Functional Programming Lecture 1: Introduction

CS 320: Concepts of Programming Languages

CSCE 314 Programming Languages

SECOND PUBLIC EXAMINATION. Compilers

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Scheme: Strings Scheme: I/O

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

Shell CSCE 314 TAMU. Higher Order Functions

Algorithms and Programming I. Lecture#12 Spring 2015

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

Introduction to Functional Programming in Haskell 1 / 56

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

COMPUTER SCIENCE 123. Foundations of Computer Science. 5. Strings

CSC 533: Programming Languages. Spring 2015

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

CSCE 314 Programming Languages. Functional Parsers

CS 11 Haskell track: lecture 1

Introduction to MATLAB

MATLAB Introduction To Engineering for ECE Topics Covered: 1. Creating Script Files (.m files) 2. Using the Real Time Debugger

Getting started with Hugs on Linux

Functional Programming in Haskell Part I : Basics

SML A F unctional Functional Language Language Lecture 19

CS422 - Programming Language Design

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

CS115 - Module 9 - filter, map, and friends

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

Thoughts on Assignment 4 Haskell: Flow of Control

Basic concepts. Chapter Toplevel loop

Introduction to MATLAB

FP Foundations, Scheme

A Brief Haskell and GHC Refresher

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Logic - CM0845 Introduction to Haskell

Introduction to lambda calculus Part 3

Functional Languages. Hwansoo Han

MATLAB - Lecture # 4

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Python Programming Exercises 1

Java Bytecode (binary file)

Transcription:

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 for teaching and prototyping purposes; Hugs is available on the web from: www.haskell.org/hugs 1

Starting Hugs On a Unix system, Hugs can be started from the % prompt by simply typing hugs: % hugs Hugs 98: Based on the Haskell 98 standard Copyright (c) 1994-2005 --- World Wide Web: http://haskell.org/hugs Report bugs to: hugs-bugs@haskell.org > 2

The Hugs > prompt means that the Hugs system is ready to evaluate an expression. For example: > 2+3*4 14 > (2+3)*4 20 > sqrt (3^2 + 4^2) 5.0 3

The Standard Prelude The library file Prelude.hs provides a large number of standard 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 prelude, 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 Hugs. 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 Hugs with the new script: % hugs test.hs Now both Prelude.hs and test.hs are loaded, and functions from both scripts can be used: > quadruple 10 40 > take (double 2) [1,2,3,4,5,6] [1,2,3,4] 14

Leaving Hugs 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

Hugs 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

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 17

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 18

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 19

Useful Hugs Commands Command Meaning :load name load script name :reload reload current script :edit name edit script name :edit edit current script :type expr show type of expr :? show all commands :quit quit Hugs 20

Exercises (1) (2) Try out slides 2-8 and 14-17 using Hugs. Fix the syntax errors in the program below, and test your solution using Hugs. 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