Programming Languages

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
By Neng-Fa Zhou Functional Programming 4 Theoretical foundation –Church’s -calculus expressions and evaluation rules 4 Characteristics –Single assignment.
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
A Lightning Tour of Haskell Lecture 1, Designing and Using Combinators John Hughes.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Advanced Programming Andrew Black and Tim Sheard Lecture 2 Intro to Haskell.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
AAAARCH Research Group A grammar for Policies in a generic AAA Environment.
Definitions. name :: Type answer :: Int name = expression answer = Definitions associate a name with a value of a certain type is of type greater.
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.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Overview of the Haskell 98 Programming Language
0 INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Dr. Philip Cannata 1 Programming Languages Haskell.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics.
Guards1. 2 Let’s test this function. Main> maxi Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases.
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.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
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)
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Lecture 14: Advanced Topic: Functional Programming
Functional Programming
Conditional Expressions
Midterm recap Total was 80 points Distribution range
Principles of programming languages 12: Functional programming
dr Robert Kowalczyk WMiI UŁ
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
Haskell Chapter 2.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
Haskell programming language.
Functional Programming
Haskell.
CSE 3302 Programming Languages
Main Points of Haskell Functional programming Laziness
Haskell Strings and Tuples
Clojure to Haskell (It’s mostly syntax).
Functional Programming
Programming Languages
Extended Static Checking for Haskell (ESC/Haskell)
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
Type & Typeclass Syntax in function
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
PROGRAMMING IN HASKELL
Types and Type Checking (What is it good for?)
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Functional Programming
Functions and patterns
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Programming Languages Haskell Part 1

A programming language is a language with a well-defined syntax (lexicon and grammar), type system, and semantics that can be used to implement a set of algorithms. Haskell

Haskell: /lusr/bin/hugs, should be in your default $PATH or for windows, download and install winhugs at http://cvs.haskell.org/Hugs/pages/downloading.htm $ hugs __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Bugs: http://hackage.haskell.org/trac/hugs || || Version: 20051031 _________________________________________ Haskell 98 mode: Restart with command line option -98 to enable extensions Type :? for help Hugs> :load 09H1 Main> To load the file “09H1.hs” from the directory in which you started hugs

Haskell Expression Number Expressions Boolean Expressions Hugs> 2 * 4 ^2 32 Hugs> 8^2 64 Hugs> (2 * 4) ^ 2 Hugs> 2 + 4 ^ 2 18 Hugs> 2 ^ 200 1606938044258990275541962092341162602522202993782792835301376 Hugs> True && False False Hugs> True || False True Hugs> 3 < 5 Hugs> 'c' < 'p' Hugs> 'c' > 'p' Hugs> "tree" < "rock" Hugs> "tree" > "rock" Number Expressions Boolean Expressions Character Expressions String Expressions

Haskell Basic Data Structures Hugs> ("dog", "cat", 5) ("dog","cat",5) Hugs> ["dog", "cat", 5] ERROR - Cannot infer instance *** Instance : Num [Char] *** Expression : ["dog","cat",5] Hugs> ["dog", "cat", "5"] ["dog","cat","5"] Hugs> 1 : [] [1] Hugs> 1 : [2,3,4] [1,2,3,4] [expression | generator] Hugs> [2 * x ^ 2 | x <- [1, 2, 3 ,4]] [2,8,18,32] Hugs> [x * y | (x, y) <- [(1, 2), (3, 4), (5, 6)]] [2,12,30] Tuples Lists List Construction List Comprehension Haskell Basic Data Structures

Haskell Basic Data Structures continued List Comprehension Cross product “<“ and “>” Relation Factors of 72 Hugs> [(x, y) | x <- [1, 3 .. 6], y <- ['a', 'b', 'c', 'd']] [(1,'a'),(1,'b'),(1,'c'),(1,'d'),(3,'a'),(3,'b'),(3,'c'),(3,'d'),(5,'a'),(5,'b'),(5,'c'),(5,'d')] Hugs> [(x, y) | x <- [1..4], y <- [1..4]] [(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4),(3,1),(3,2),(3,3),(3,4),(4,1),(4,2),(4,3),(4,4)] Hugs> [(x, y) | x <- [0..4], y <- [0..4], x < y] [(0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)] Hugs> [(x, y) | x <- [0..4], y <- [0..4], x > y] [(1,0),(2,0),(2,1),(3,0),(3,1),(3,2),(4,0),(4,1),(4,2),(4,3)] Hugs> [x | x <- [1..100], y <- [1..100], 72 == x * y] [1,2,3,4,6,8,9,12,18,24,36,72]

Haskell Basic Data Structures - continued List Comprehension Database Query Main> [empno | (empno, _, _, _, _, _, _) <- emp] [7839,7698,7782,7566,7788,7902,7369,7499,7521,7654,7844,7876,7900,7934] Main> [empno | (empno, _, _, _, _, sal, _) <- emp, sal > 4000] [7839] Main> [empno | (empno, _, _, _, _, sal, _) <- emp, sal > 2000] [7839,7698,7782,7566,7788,7902]

Haskell Functions Hugs> :type product product :: Num a => [a] -> a Hugs> product [ 2, 3, 4 ] 24 Hugs> product [ x | x <- [1..10]] 3628800 Hugs> :type (+) (+) :: Num a => a -> a -> a Hugs> 3 + 5 8 Hugs> (+) 3 5 Hugs> (\ x -> x ^ 2 + 4) 5 29 product Function (+) Operator – an Operator is a 2-ary Function lambdas

Haskell Operators

Haskell Pattern Matching Hugs> :type not not :: Bool -> Bool Hugs> not True False Hugs> :type head head :: [a] -> a Hugs> head [5, 4, 3, 2, 1] 5 Hugs> :type map map :: (a -> b) -> [a] -> [b] Hugs> map sqrt [ 1, 4, 9, 10 ] [1.0,2.0,3.0,3.16227766016838] Hugs> map (\ x -> x + 2) [ 1, 2, 3, 4] [3,4,5,6] not :: Bool  Bool not False = True not True = False head :: [a] -> a head (x:xs) = x map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs

= = = = = Haskell Pattern Matching product [2,3,4] 2 * product [3,4] Hugs> product [ 2, 3, 4 ] 24 product :: [Int]  Int product [] = 1 product (x:xs) = x * product xs product [2,3,4] 2 * product [3,4] = 2 * (3 * product [4]) = 2 * (3 * (4 * product [])) = 2 * (3 * (4 * 1)) = 24 =

Haskell Conditional Expressions Let Expressions (i.e., local variables) Hugs> if True then 6 else 8 6 Hugs> if False then 6 else 8 8 Hugs> 2 + let x = sqrt 9 in (x + 1) * (x - 1) 10.0 not :: Bool  Bool not False = True not True = False Let assignment assignment . in (expression)

Lazy Evaluation: v = 1/0 testLazy x = 2 + 10 testLazy1 x = 2 / x *DBQ> v Infinity *DBQ> testLazy 22 12 *DBQ> testLazy v *DBQ> testLazy1 22 9.090909090909091e-2 *DBQ> testLazy1 v 0.0 *DBQ> testLazy1 1/0 1.#INF Main> (\x -> let y = x in (2 / y)) (1/0) 0.0 Main> (\x -> let y = x in (2 / y)) (0) 1.#INF