1 / 14

Functional Programming Lecture 1 - Introduction

Functional Programming Lecture 1 - Introduction. Professor Muffy Calder. About the Course. the Haskell functional language Lectures + Laboratories + Problem Sessions 2 Assignments Web site Lecture notes Assignments and problem sheets Resources Textbook

gail-mullen
Télécharger la présentation

Functional Programming Lecture 1 - Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functional ProgrammingLecture 1 - Introduction Professor Muffy Calder

  2. About the Course • the Haskell functional language • Lectures + Laboratories + Problem Sessions • 2 Assignments • Web site • Lecture notes • Assignments and problem sheets • Resources • Textbook Functional Programming in Haskell by Simon Thompson

  3. Programming Languages • imperative: Ada, C, Pascal, … • object: Java, C++, Smalltalk … • logic: Prolog, ... • functional: Haskell, ML, Lisp, Scheme, … • Haskell named after Haskell B. Curry • Not Schoenfinkel • Haskell is lazy • ML is strict

  4. What is Functional Programming? • Based on mathematical functions • Example • A function to compute whether or not an integer is in a list of integers inlist :: Int -> [Int] -> Bool inlist x [] = False inlist x (y:ys) = | (x == y) = True | otherwise = inlist x ys inlist 3 [5,3,6] inlist 2 [5,3,6] • Key concepts • Types • Recursion

  5. Why use Functional Languages? • Concise and powerful style • Rapid prototyping • Strong error checking by intepreter/compiler • Reduced debugging time • Reliable, correct software • Formal reasoning • Why teach it in second year? • Good discipline + more abstract way of thinking

  6. Haskell • Standard functional language • developed by international committee • 3 people from GU on that committee • Used as the basis for numerous projects, in numerous countries • Has technical characteristics that support software engineering and formal methods

  7. Hugs • An interactive interpreter for Haskell98 • Hugs98 for windows • Very quick ‘compilation’ and error checking • Available on nearly all computers • Free software • See http://haskell.org/hugs

  8. Overview of Functional Programming 2 slogans • everything is a function • every function has a type FP consists of • defining types • defining functions • applying functions and calculating the resulting expression typed inputs typed output f

  9. Types, Constants and Expressions • type - set of possible values Int • constant - a particular value in a type 27:: Int • expression - can be evaluated 2+3*x • notation: 2+2 => 4 “2+2 evaluates to 4” The type of + is Int->Int-Int, i.e. _+_ :: Int->Int->Int

  10. Int and Integer • Int - 32-bit integers • Integer - genuine integers • no limit on size (except memory) • all operations give correct result • programmer doesn’t have to worry about how much memory to allocate You will usually use Int.

  11. Float and Double • Single and double precision floating point numbers • Does not give exact real arithmetic - there can be roundoff errors • It is unlikely that you will use these types in this course.

  12. Char • ASCII characters • constant - surround with single quote ‘x‘ :: Char • can compare characters `c` < ‘Z’ • case conversion • toUpper ‘ w ‘ => ‘ W ‘ • toLower ‘Q ‘ => ‘ q ‘ • toUpper :: Char -> Char

  13. Bool • Boolean values, used to control conditionals • only two constants: False :: Bool and True :: Bool • b && c (conjunction) • b || c (disjunction) • not b (negation) Example: ( 3 <= x) && (x <= 10) :: Bool

  14. Function application • Write the name of the function followed by the arguments • Sometimes use terminology rator and rand • Don’tput parentheses around the argument sqrt 9.0 fcn 2 8 13 3.4 + (sqrt x) * 100 2 * (sqrt (pi*r*r)) + y E.g. In Maths f(x) In FP (f x)

More Related