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.

Slides:



Advertisements
Similar presentations
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Operators, Functions and Modules1 Pattern Matching & Recursion.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Functions Why we use functions C library functions Creating our own functions.
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)
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Overview of the Haskell 98 Programming Language
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.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Introduction to Objective Caml. General comments ML is a purely functional language--there are (almost) no side effects There are two basic dialects of.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
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.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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)
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Conditional Expressions
Midterm recap Total was 80 points Distribution range
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Functions and patterns
A lightening tour in 45 minutes
Haskell.
CSE 3302 Programming Languages
Chapter 2 - Introduction to C Programming
PROGRAMMING IN HASKELL
Type & Typeclass Syntax in function
Chapter 2 - Introduction to C Programming
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Fundamentals of Functional Programming
Functions and patterns
Chapter 2 - Introduction to C Programming
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

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 GHCi is the REPL Just enter ghci at the command line HUGS is also a popular version As far as the language is concerned, there are no differences between the two that concern us. 3

Using Haskell You can do arithmetic at the prompt: Main> You can call functions at the prompt: Main> sqrt The GHCi documentation says that functions must be loaded from a file: Main> :l "test.hs" Reading file "test.hs": But you can define them in GHCi with let let double x = 2 * x 4

Lexical issues Haskell is case-sensitive Variables begin with a lowercase letter Type names begin with an uppercase letter Indentation matters (braces and semicolons can also be used, but it’s not common) There are two types of comments: -- (two hyphens) to end of line {- multi-line {- these may be nested -} -} 5

Semantics The best way to think of a Haskell program is as a single mathematical expression In Haskell you do not have a sequence of “statements”, each of which makes some changes in the state of the program Instead you evaluate an expression, which can call functions Haskell is a functional programming language 6

Functional Programming (FP) In FP, Functions are first-class objects. That is, they are values, just like other objects are values, and can be treated as such Functions can be assigned to variables, passed as parameters to higher-order functions, returned as results of functions There is some way to write function literals Functions should only transform their inputs into their outputs A function should have no side effects It should not do any input/output It should not change any state (any external data) Given the same inputs, a function should produce the same outputs, every time-- it is deterministic If a function is side-effect free and deterministic, it has referential transparency—all calls to the function could be replaced in the program text by the result of the function But we need random numbers, date and time, input and output, etc. 7

Types Haskell is strongly typed… …but type declarations are seldom needed, because Haskell does type inferencing Primitive types: Int, Float, Char, Bool Lists: [2, 3, 5, 7, 11] All list elements must be the same type Tuples: (1, 5, True, 'a') Tuple elements may be different types 8

Bool Operators Bool values are True and False Notice how these are capitalized “And” is infix && “Or” is infix || “Not” is prefix not Functions have types “Not” is type Bool -> Boo l “And” and “Or” are type Bool -> Bool -> Bool 9

Arithmetic on Integers + - * / ^ are infix operators Add, subtract, and multiply are type (Num a) => a -> a -> a Divide is type (Fractional a) => a -> a -> a Exponentiation is type (Num a, Integral b) => a -> b -> a even and odd are prefix operators They have type (Integral a) => a -> Bool div, quot, gcd, lcm are also prefix They have type (Integral a) => a -> a -> a 10

Floating-Point Arithmetic + - * / ^ are infix operators, with the types specified previously sin, cos, tan, log, exp, sqrt, log, log10 These are prefix operators, with type (Floating a) => a -> a pi Type Float truncate Type (RealFrac a, Integral b) => a -> b 11

Operations on Chars These operations require import Data.Char ord is Char -> Int chr is Int -> Char isPrint, isSpace, isAscii, isControl, isUpper, isLower, isAlpha, isDigit, isAlphaNum are all Char-> Bool A string is just a list of Char, that is, [Char] "abc" == ['a', 'b', 'c'] 12

Polymorphic Functions == /= Equality and inequality tests are type (Eq a) => a -> a -> Bool = > These comparisons are type (Ord a) => a -> a -> Bool show will convert almost anything to a string Any operator can be used as infix or prefix (+) 2 2 is the same as `mod` 7 is the same as mod

Operations on Lists I 14

Operations on Lists II 15

Operations on Lists III 16

Operations on Tuples …and nothing else, really. 17

Lazy Evaluation No value is ever computed until it is needed Lazy evaluation allows infinite lists Arithmetic over infinite lists is supported Some operations must be avoided, for example, finding the “last” element of an infinite list 18

Finite and Infinite Lists 19

List Comprehensions I [ expression_using_x | x <- list ] read: where x is in x <- list is called a generator Example: [ x * x | x <- [1..] ] This is the list of squares of positive integers take 5 [x * x | x <- [1..]] [1,4,9,16,25] 20

List Comprehensions II [ expression_using_x_and_y | x <- list, y <- list ] take 10 [x*y | x <- [2..], y <- [2..]] [4,6,8,10,12,14,16,18,20,22] take 10 [x * y | x <- [1..], y <- [1..]] [1,2,3,4,5,6,7,8,9,10] take 5 [(x,y) | x <- [1,2], y <- "abc"] [(1,'a'),(1,'b'),(1,'c'),(2,'a'),(2,'b')] 21

List Comprehensions III [ expression_using_x | generator_for_x, test_on_x ] take 5 [x*x | x <- [1..], even x] [4,16,36,64,100] 22

List Comprehensions IV [x+y | x <- [1..5], even x, y <- [1..5], odd y] [3,5,7,5,7,9] [x+y | x <- [1..5], y <- [1..5], even x, odd y] [3,5,7,5,7,9] [x+y | y <- [1..5], x <- [1..5], even x, odd y] [3,5,5,7,7,9] 23

Simple Functions Functions are defined using = avg x y = (x + y) / 2 :type or :t tells you the type :t avg (Fractional a) => a -> a -> a 24

Anonymous Functions Anonymous functions are used often in Haskell, usually enclosed in parentheses \x y -> (x + y) / 2 the \ is pronounced “lambda” It’s just a convenient way to type the x and y are the formal parameters Functions are first-class objects and can be assigned avg = \x y -> (x + y) / 2 25

Haskell Brooks Curry Haskell Brooks Curry (September 12, 1900 – September 1, 1982) Developed Combinatorial Logic, the basis for Haskell and many other functional languages 26

Currying Currying is a technique named after the logician Haskell Curry Currying absorbs an argument into a function, returning a new function that takes one fewer argument f a b = (f a) b, where (f a) is a curried function For example, if avg = \x y -> (x + y) / 2 then (avg 6) returns a function This new function takes one argument ( y ) and returns the average of that argument with 6 Consequently, we can say that in Haskell, every function takes exactly one argument 27

Currying example “And”, &&, has the type Bool -> Bool -> Bool x && y can be written as (&&) x y If x is True, (&&)x is a function that returns the value of y If x is False, (&&)x is a function that returns False It accepts y as a parameter, but doesn’t use its value 28

Slicing negative = (< 0) Main> negative 5 False Main> negative (-3) True Main> :type negative negative :: Integer -> Bool Main> 29

Factorial I fact n = if n == 0 then 1 else n * fact (n - 1) This is an extremely conventional definition. 30

Factorial II fact n | n == 0 = 1 | otherwise = n * fact (n - 1) Each | indicates a “guard.” Notice where the equal signs are. 31

Factorial III fact n = case n of 0 -> 1 n -> n * fact (n - 1) This is essentially the same as the last definition. 32

Factorial IV You can introduce new variables with let declarations in expression fact n | n == 0 = 1 | otherwise = let m = n - 1 in n * fact m 33

Factorial V You can also introduce new variables with expression where declarations fact n | n == 0 = 1 | otherwise = n * fact m where m = n

The End 35