September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 1 Functional Programming Symbolic AI is based.

Slides:



Advertisements
Similar presentations
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Advertisements

Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
BB1756: Software Development Functional Programming in Haskell 2010/2011 Dan Russell WWW:
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
Introduction to a Programming Environment
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,
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.
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)
CS1101: Programming Methodology Aaron Tan.
Haskell Chapter 1, Part II. List Comprehension  List comprehensions are a way to filter, transform and combine lists  Similar to mathematical set comprehensions.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
CS 330 Programming Languages 09 / 04 / 2008 Instructor: Michael Eckmann.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
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)
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Artificial Intelligence Programming in Prolog Lecture 1: An Introduction 23/09/04.
Chapter 9: Functional Programming in a Typed Language.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
Functional Programming Language OCaml Tutorial 科大 - 耶鲁联合研究中心
CS5205Introduction1 CS5205: Foundation in Programming Languages Lecture 0 : Overview Lecturer : Chin Wei Ngan Office : COM2.
1-1 An Introduction to Functional Programming Sept
Course Instructor: Hira Farman Course : BY:HIRA FARMAN.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.
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)
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.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Functional Programming Lecture 1 - Introduction Professor Muffy Calder.
Lecture 14: Advanced Topic: Functional Programming
Polymorphic Functions
Functional Programming
CST 1101 Problem Solving Using Computers
Haskell: Syntax in Functions
Midterm recap Total was 80 points Distribution range
dr Robert Kowalczyk WMiI UŁ
Haskell Chapter 2.
PROGRAMMING IN HASKELL
Be A programmer in Steps
CS 326 Programming Languages, Concepts and Implementation
PROGRAMMING IN HASKELL
Functional Programming
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Developing Applications
Programming fundamentals 2 Chapter 2:Function
First work in AI 1943 The name “Artificial Intelligence” coined 1956
CSE341: Programming Languages Section 1
CSE341: Programming Languages Section 1
Pattern Matching Pattern matching allows us to do things like this:
Artificial Intelligence Lecture 2: The Haskell Programming Language
CSE 3302 Programming Languages
Functional Programming
Functions and patterns
Functional Programming and Haskell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE341: Programming Languages Section 1
Functional Programming and Haskell
Presentation transcript:

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 1 Functional Programming Symbolic AI is based on a representation of the world in terms of symbols and rules. Here, the AI comes in the form of algorithms that manipulate the symbols in order to solve problems or derive new knowledge. In order to explore such algorithms and experiment with them, we will use a functional programming language. Functional programming languages are well-suited for implementing complex algorithms in a concise and readable way and offer great flexibility in modifying, reusing, and testing code.

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 2 The Haskell Language Traditionally, AI researchers and educators used the language LISP. LISP is a powerful language that is still being used; for example, emacs is programmed in LISP. However, in my opinion, LISP programs are not very readable or at least require some time to get used to. Therefore, we will use Haskell, which has a rather straightforward and clear syntax. It is similar to many other functional languages such as F#, ML, Miranda, or OCaml.

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 3 The Haskell Language You can download the Haskell Platform and find tons of information about the language here: In particular, please study Chapters 1 to 6 of the free online Haskell Tutorial with the beautiful title: “Learn you a Haskell for Great Good!” In the next lecture, we will review the Haskell basics, discuss your questions and look at some more advanced Haskell topics.

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 4 Today’s Haskell Session in Class Please take some time to study Haskell; it’s not only important for this course, but knowing principles of functional programming will also make you a better programmer in imperative languages such as C++ or Java. The next page shows the Haskell programs we developed in class today, followed by the complete protocol of our Haskell session. Please me anytime with any questions you may have.

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 5 The Programs We Wrote Today fact 0 = 1 fact n = n*fact (n - 1) mymap f [] = [] mymap f (x:xs) = (f x):(mymap f xs) quicksort [] = [] quicksort (x:xs) = (quicksort smaller) ++ [x] ++ (quicksort greater) where smaller = filter (< x) xs greater = filter (>= x) xs

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 6 Today’s Haskell Session (1) *Main> 5 5 it :: Integer *Main> it 5 it :: Integer *Main> it :: Integer *Main> 5/ it :: Double *Main> 2^ it :: Integer

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 7 Today’s Haskell Session (2) *Main> 2^ … a few hours later… … it :: Integer *Main> 'a' 'a' it :: Char *Main> 'b' 'b' it :: Char *Main> 3 == 5 False it :: Bool

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 8 Today’s Haskell Session (3) *Main> 5 == 5 True it :: Bool *Main> let double x = 2*x double :: Num a => a -> a *Main> double 4 8 it :: Integer *Main> double it :: Double *Main> let mult x y = x*y mult :: Num a => a -> a -> a

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 9 Today’s Haskell Session (4) *Main> mult it :: Integer *Main> mult it :: Double *Main> let timesthree = mult 3 timesthree :: Integer -> Integer *Main> timesthree 4 12 it :: Integer *Main> (5, 3) (5,3) it :: (Integer, Integer)

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 10 Today’s Haskell Session (5) *Main> fst it 5 it :: Integer *Main> snd (5, 3) 3 it :: Integer *Main> :t fst fst :: (a, b) -> a *Main> [] [] it :: [a] *Main> [3] [3] it :: [Integer]

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 11 Today’s Haskell Session (6) *Main> 3:[5] [3,5] it :: [Integer] *Main> [1..10] [1,2,3,4,5,6,7,8,9,10] it :: [Integer] *Main> ['a'..'z'] "abcdefghijklmnopqrstuvwxyz" it :: [Char] *Main> even 5 False it :: Bool *Main> mod it :: Integer

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 12 Today’s Haskell Session (7) *Main> [x | x [x | x <- [1..10], even x][2,4,6,8,10] it :: [Integer] *Main> [(x,y) | x [(x,y) | x <- [1..3], y <- ['a'..'c']][(1,'a'),(1,'b'),(1,'c'),(2,'a'),(2,'b'),(2,'c'),(3,'a'),(3,'b'),(3,'c')] it :: [(Integer, Char)] *Main> mult it :: Double *Main> let prime n = n > 1 && [x | x let prime n = n > 1 && [x | x <- [2..(div n 2)], mod n x = 0] == [] :77:58: parse error on input `=' :77:58: parse error on input `=' *Main> let prime n = n > 1 && [x | x let prime n = n > 1 && [x | x <- [2..(div n 2)], mod n x == 0] == [] prime :: Integral t => t -> Bool

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 13 Today’s Haskell Session (8) *Main> prime 2 True it :: Bool *Main> prime 3 True it :: Bool *Main> prime 4 False it :: Bool *Main> filter even [1..100] [2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52, 54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100] it :: [Integer]

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 14 Today’s Haskell Session (9) *Main> :t filter filter :: (a -> Bool) -> [a] -> [a] *Main> filter prime [1..100] [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] it :: [Integer] *Main> filter prime [ ] [2,3,5,7,11,13,17,19,23,29,31,37,41,43… and so on and on… … 9871,9883,9887,9901,9907,9923,9929,9931,9941,9949,9967,9973] it :: [Integer] *Main> mult it :: Integer *Main> map prime [3, 4, 5] [True,False,True] it :: [Bool]

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 15 Today’s Haskell Session (10) *Main> :t map map :: (a -> b) -> [a] -> [b] *Main> map (mult 3) [1..10] [3,6,9,12,15,18,21,24,27,30] it :: [Integer] *Main> :t (mult 3) (mult 3) :: Num a => a -> a *Main> it :: Integer *Main> :92:1: parse error on input `+' :92:1: parse error on input `+'

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 16 Today’s Haskell Session (11) *Main> (+) it :: Integer *Main> map (* 3) [1..10] [3,6,9,12,15,18,21,24,27,30] it :: [Integer] *Main> "hello" ++ " world!" "hello world!" it :: [Char] *Main> [1..10] ++ [30..40] [1,2,3,4,5,6,7,8,9,10,30,31,32,33,34,35,36,37,38,39,40] it :: [Integer] *Main> :reload [1 of 1] Compiling Main ( test.hs, interpreted ) Ok, modules loaded: Main.

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 17 Today’s Haskell Session (12) *Main> fact 0 1 it :: Integer *Main> fact 1 1 it :: Integer *Main> map fact [1..10] [1,2,6,24,120,720,5040,40320,362880, ] it :: [Integer] *Main> :reload [1 of 1] Compiling Main ( test.hs, interpreted ) Ok, modules loaded: Main. *Main> mymap even [1..100] [False,True,False,True,False,True,… and so on…, False, True] it :: [Bool]

September 19, 2012Introduction to Artificial Intelligence Lecture 5: Functional Programming with Haskell 18 Today’s Haskell Session (13) *Main> :reload [1 of 1] Compiling Main ( test.hs, interpreted ) Ok, modules loaded: Main. *Main> quicksort "hello world!" " !dehllloorw" it :: [Char] *Main> :t quicksort quicksort :: Ord a => [a] -> [a]