159'331 Programming Languages - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

159'331 Programming Languages

Description:

Put the above in file fact.hs and run the command HUGS fact.hs ... We can invoke fact 3 = 6 (type fact 3 at the HUGS prompt) 159.331. Prog Lang & Alg ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 13
Provided by: KenHa98
Category:

less

Transcript and Presenter's Notes

Title: 159'331 Programming Languages


1
159.331 Programming Languages Algorithms
  • Lecture 20 - Functional Programming Languages -
    Part 4
  • More Example Languages Lambda Calculus

2
Example Languages - ML SML
  • Meta-Language
  • Robin Milner et al. 1990
  • Static Scoped (like Scheme)
  • ML is strongly typed however
  • Ha s type inferencing so type declaratiosn not
    always (often) used
  • No type coercions - types must match properly

3
  • ML uses infix so it looks a lot more
    imperative than Lisp and Scheme
  • fun fact(n int) int if n 0 then 1
  • else n fact( n - 1)
  • ML has exception handling and module facility
    for implementing ADTs
  • ML uses square brackets for lists - like
    Miranda
  • hd and tl instead of CAR and CDR
  • CONS is instead of a single as in
    Miranda
  • Various features absorbed from Hope combined
    to form Standard ML (SML)
  • We can see a lot of Mirandas and Haskells
    origins in ML and SML.

4
Example Languages - Haskell
  • Similar to ML syntax
  • Haskell is statically scoped and strongly typed
    and has same type inferencing method as ML
  • but Haskell uses lazy evaluation (non-strict
    semantics)
  • and Haskell has list comprehensions
  • (Useful book is Haskell - the Craft of
    Functional Programming, Simon Thompson, Pub
    Addison Wesley, ISBN 0-201-34275-8)

5
Haskell - factorial function
  • fact Int -gt Int
  • fact 0 1
  • fact n n fact (n-1)
  • Put the above in file fact.hs and run the
    command HUGS fact.hs
  • Set of two pattern matching equations
  • (No reserved word to introduce the function
    definition ie fun in ML )
  • We can invoke fact 3 gt 6 (type fact 3
    at the HUGS prompt)

6
Fibonacci Numbers in Haskell
  • fib 0 1
  • fib 1 1
  • fib ( n 2 ) fib ( n 1 ) fib n
  • The type checker will figure out the missing
    first line fib Int -gt Int
  • map fib 0..10 gt 1,1,2,3,5,8,13,21,34,55,
    89
  • map fib 0.. is a bad idea because?
  • Guards can specify which definition to use,
    eg
  • fact n
  • n 0 1
  • n gt 0 n fact( n -1)
  • So that fact 0 is OK but fact -1 gives an
    error

7
  • We can have an otherwise
  • sub n
  • n lt 10 0
  • n gt 100 2
  • otherwise 1
  • hd, tl, etc as in Miranda
  • member b False
  • member ( ax) b ( a b ) member x b
  • And member 1..10 6 gt True
  • member 1.. 0 will take a while

8
Summary
  • Functional programs consist of functions that map
    input values from one domain onto output values
    of another (co)domain, much as in mathematics.
    Pure functional languages lack imperative
    features such as assignment and repetitive
    statements. Also unlike most imperative
    languages, functions in functional languages
    cannot have side-effects.
  • Pure functional languages are referentially
    transparent, which means that the result of a
    function application does not depend on when the
    function is called but only on the arguments of
    the call.
  • Referential transparency can make programs easier
    to read, transform, parallelize and prove correct.

9
  • Problems with the functional paradigm are
    expressing I/O and obtaining an efficient
    implementation. Efficient updating of data
    structures poses an important implementation
    problem.
  • The most important concepts in functional
    languages are recursive functions, lists,
    polymorphism, higher-order functions, currying
    and equations.
  • Recursion can be used to express repetition.
  • Lists are the main data structures in functional
    languages. Many operations on lists are
    pre-defined or built-in.
  • The management of memory for data structures is
    done automatically. Programmers are not aware of
    the existence of machine addresses (there are no
    pointers). Deallocation of memory that is no
    longer used is also done automatically, using
    garbage collection.

10
  • Modern functional languages support polymorphic
    functions, which can take arguments of different
    types. Many (functional) languages use strong
    typing, but the types are often inferred by a
    type checker and need not be given by the
    programmer.
  • A higher-order function takes a function as
    argument. Higher-order functions contribute
    significantly to the flexibility of functional
    languages.
  • Applicative order reduction first evaluates the
    arguments of a function and then applies the
    function. Normal order reduction applies the
    function to the unevaluated arguments, and only
    evaluates the arguments when needed.

11
  • Normal order reduction is the basis for lazy
    evaluation. Lazy evaluation makes it possible to
    deal with infinite data structures. It is
    conceptually more elegant than eager evaluation,
    but it is also harder to implement efficiently.
  • Modern functional languages allow functions to be
    defined as a set of equations. They use pattern
    matching to select the appropriate equation.
  • Impure functional languages such as Lisp support
    some properties of functional languages, but are
    not referentially transparent.

12
Further Reading
  • See Bal Grune Chapter 4
  • See Sebesta Chapter 15
  • See also Hudaks ACM article on Conception,
    Evolution and Application of Functional
    Programming Languages
  • Next - Lambda Calculus (not assessed)
  • And then Logic Programming Languages
Write a Comment
User Comments (0)
About PowerShow.com