PROGRAMMING IN HASKELL

Slides:



Advertisements
Similar presentations
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
Advertisements

0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Definitions. name :: Type answer :: Int name = expression answer = Definitions associate a name with a value of a certain type is of type greater.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
0 PROGRAMMING IN HASKELL Chapter 5 – Introduction, The Hugs System, Types and Classes.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
1 A Guide to SQL Chapter 2. 2 Introduction Mid-1970s: SQL developed under the name SEQUEL at IBM by San Jose research facilities to be the data manipulation.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee.
0 INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Haskell Basics CSCE 314 Spring CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
Haskell. GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Lecture 14: Advanced Topic: Functional Programming
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
Functional Programming
Conditional Expressions
Midterm recap Total was 80 points Distribution range
User-Written Functions
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Haskell Chapter 5, Part III.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell.
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Installation and exercises
PROGRAMMING IN HASKELL
Clojure to Haskell (It’s mostly syntax).
PROGRAMMING IN HASKELL
A First Book of ANSI C Fourth Edition
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

PROGRAMMING IN HASKELL Chapter 2 - First Steps

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

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.

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

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

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]

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

Calculate the product of a list of numbers: 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]

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.

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

Means (f a) + b, rather than f (a + b). 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).

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

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.

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)

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]

div is enclosed in back quotes, not forward; 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.

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

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

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

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

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 {a = b + c where {b = 1; c = 2} d = a * 2} means implicit grouping explicit grouping

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]

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