Haskell Basics CSCE 314 Spring 2016. CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

Programming in Visual Basic
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
Introduction to C Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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)
Introduction to Python
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.
Introduction to Computational Linguistics Programming I.
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.
Input, Output, and Processing
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)
Linux Operations and Administration
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Overview of the Haskell 98 Programming Language
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
0 INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham.
0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
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 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
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
Fundamentals of Programming I Overview of Programming
Conditional Expressions
Midterm recap Total was 80 points Distribution range
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Types CSCE 314 Spring 2016.
Chapter 1: Introduction to computers and C++ Programming
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell.
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Installation and exercises
A First Book of ANSI C Fourth Edition
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
PROGRAMMING IN HASKELL
Topics Designing a Program Input, Processing, and Output
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Fundamentals of Functional Programming
PROGRAMMING IN HASKELL
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Haskell Basics CSCE 314 Spring 2016

CSCE 314 – Programming Studio Using GHC and GHCi Log in to unix.cse.tamu.edu (or some other server) From a shell window, the compiler is invoked as > ghc myfile.hs > ghci (or as > ghc --interactive ) For multi-file programs, use --make option GHCi operates on an eval-print-loop: > sqrt (3^2 + 4^2) 5.0 > User types in a Haskell expression The interpreter evaluates it and prints out the result Waits for the next expression

Spring 2016 CSCE 314 – Programming Studio Other Haskell Options Download Haskell compiler on your computer Haskell Platform link on course webpage Windows, Mac OS X, linux Will install GHC on your computer

Spring 2016 CSCE 314 – Programming Studio Editing Haskell Files Use any editor to create a Haskell file Use the.hs extension to designate that it’s a Haskell file Install IDE Leksah Efficient edit-compile-run cycle, e.g., using Emacs with haskell- mode ( ) helps indenting, debugging, jumping to an error, etc.

Spring 2016 CSCE 314 – Programming Studio Using GHCi Useful basic GHCi commands: :? Help! Show all commands :load test Open file test.hs or test.lhs :reload Reload the previously loaded file :main a1 a2 Invoke main with command line args a1 a2 :! Execute a shell command :edit name Edit script name :edit Edit current script :type expr Show type of expr :quit Quit GHCi Commands can be abbreviated. E.g., :r is :reload At startup, the definitions of the “Standard Prelude” are loaded

Spring 2016 CSCE 314 – Programming Studio Basic statements Basic arithmetic and comparisons are supported + Addition - Subtraction * Multiplication / Division (for fractions/floats) ^ Power div Integer Division function == Equality /= Inequality >, <, etc. Other comparisons

Spring 2016 CSCE 314 – Programming Studio 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. -- Lists are written with square brackets, and elements separated by commas [1,2,3,4,5]

Spring 2016 CSCE 314 – Programming Studio List Operations -- 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

Spring 2016 CSCE 314 – Programming Studio -- 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] -- 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]

Spring 2016 CSCE 314 – Programming Studio -- 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] Calculate the product of a list of numbers: > product [1,2,3,4,5] 120

Spring 2016 CSCE 314 – Programming Studio Functions The key part of Haskell programming! Functions are created in Haskell scripts (.hs files) Cannot be defined at the command prompt in an interactive window Scripts can get loaded in :l :r to reload Scripts can be command line argument when invoked ghci Windows Once loaded, the functions defined in the script are in scope (available to use)

Spring 2016 CSCE 314 – Programming Studio Functions (1) Function and parameter names must start with a lower case letter, e.g., myFun1, arg_x, personName, etc. (By convention, list arguments usually have an s suffix on their name, e.g., xs, ns, nss, etc.) Functions are defined as equations: square x = x * x add x y = x + y Once defined, apply the function to arguments: > square 7 > add In C, these calls would be square(7); and add(2,3);

Spring 2016 CSCE 314 – Programming Studio Functions (2) Parentheses are often needed in Haskell too > add (square 2) (add 2 3) 9 Function application has the highest precedence (after parentheses) square means (square 2) + 3 not square (2+3) Function call associates to the left and is by pattern matching (first one to match is used)

Spring 2016 CSCE 314 – Programming Studio Functions (3) Function application operator $ has the lowest precedence and is used to avoid parentheses. sum ([1..5] ++ [6..10]) -> sum $ [1..5] ++ [6..10] Combinations of most symbols are allowed as function x y = "What on earth?” Another (more reasonable) example: x +/- y = (x-y, x+y) > 10 +/- 1 (9,11)

Spring 2016 CSCE 314 – Programming Studio 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 In Haskell, function application is denoted using space, and multiplication is denoted using *. f a b + c*d As above, but in Haskell syntax

Spring 2016 CSCE 314 – Programming Studio Examples Mathematics Haskell f(x) f(x,y) 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

Spring 2016 CSCE 314 – Programming Studio Prefix vs Infix Many people are used to Infix notation: an operation comes between two arguments a op b Typical Haskell usage for functions is Prefix notation. That is, the operation comes first, then the arguments. op a b Any function with two arguments can be used as an infix operator by enclosing it in BACK quotes ` ` a `op` b Any infix operator can be used as a function (enclosed in parentheses) (*) a b

Spring 2016 CSCE 314 – Programming Studio Comments Ordinary comments: start with – Single line: ignore everything else on that line double x = x + x -- This is a function to double a number -- I am writing another comment Nested comments: {- -} Can be multiline, or nested one inside the other double x = x + x {- Here is a comment {- With another comment inside it -} -}

Spring 2016 CSCE 314 – Programming Studio Evaluating Functions (1) Think of evaluating functions as substitution and reduction add x y = x + y; square x = x * x add (square 2) (add 2 3) −− apply square add (2 * 2) (add 2 3) −− apply ∗ add 4 (add 2 3) −− apply inner add add 4 (2 + 3) −− apply + add 4 5 −− apply add 4+5 −− apply + 9

Spring 2016 CSCE 314 – Programming Studio Evaluating Functions (2) There are many possible orders to evaluate a function head (1:(reverse [2,3,4,5])) head (1:(reverse [2,3,4,5])) −− apply reverse −− apply head −−... many steps omitted here 1 head (1 : [5,4,3,2]) −− apply head 1 In a pure functional language, evaluation order does not affect the value of the computation It can, however, affect the amount of computation and whether the computation terminates or not (or fails with a run-time error) Haskell evaluates a function’s argument lazily “Call-by-need” - only apply a function if its value is needed, and “memoize” what’s already been evaluated

Spring 2016 CSCE 314 – Programming Studio My First Script double x = x + x quadruple x = double (double x) 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: % ghci test.hs In another window start up GHCi with the new script: > quadruple > take (double 2) [1,2,3,4,5,6] [1,2,3,4] Now both the standard library and the file test.hs are loaded, and functions from both can be used:

Spring 2016 CSCE 314 – Programming Studio factorial n = product [1..n] average ns = sum ns `div` length ns Leaving GHCi open, return to the editor, add the following definitions, and resave:  div is enclosed in back quotes, not forward  Lets us write using infix notation Note: > :r Reading file "test.hs" > factorial > average [1,2,3,4,5] 3 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:

Spring 2016 CSCE 314 – Programming Studio The Layout Rule  Layout of a script determines the structure of definitions  Commonly use layouts instead of braces and semicolons (which are still allowed and can be mixed with layout)  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 means a = b + c where b = 1 c = 2 d = a * 2 a = b + c where {b = 1; c = 2} d = a * 2 implicit grouping by layout explicit grouping by braces and semicolons

Spring 2016 CSCE 314 – Programming Studio Exercises N = a ’div’ length xs where a = 10 xs = [1,2,3,4,5] (1) What are the syntax errors in the program below? n = a `div` length xs where a = 10 xs = [1,2,3,4,5]

Spring 2016 CSCE 314 – Programming Studio Show how the library function last that selects the last element of a list can be defined using the functions introduced in this lecture. (2) Similarly, show how the library function init that removes all but the last element from a list can be defined in two different ways. (4) Can you think of another possible definition? (3) last xs = head ( reverse xs ) last xs = xs !! (length xs – 1) init xs = take ( length xs – 1 ) xs init xs = reverse ( tail ( reverse xs ))