Functional Programming in ML and Scheme: A Comparison, Assignments of Programming Languages

An overview of functional programming in ml and scheme, including their functional programming styles, history, features, syntax comparison, typing, and recursive data types. It also includes examples of list manipulation, pattern matching, and parametric polymorphism.

Typology: Assignments

Pre 2010

Uploaded on 09/17/2009

koofers-user-oz9
koofers-user-oz9 🇺🇸

5

(1)

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSC 4101: Programming Languages 1
1
Functional Programming
Functional Programming
Chapter 13
2
Functional Programming Style
Functional Programming Style
zWrite many small functions (2-liners)
zEach loop corresponds to 1 function
zNo assignment, only function calls
zWrite base cases of recursion
case for empty list, ma ybe for atoms
maybe case for singleton list
zWrite recursive cases
case(s) for nonempty list
3
Example: Nesting of Parentheses
Example: Nesting of Parentheses
zBase case empty list: 1
zBase case atoms: 0
zRecursive case: max (1+car, cdr)
zFinished function
(define (nest x)
(cond ((null? x) 1)
((not (pair? x)) 0)
(else (max (+ 1 (nest (car x)))
(nest (cdr x))))))
pf3
pf4
pf5

Partial preview of the text

Download Functional Programming in ML and Scheme: A Comparison and more Assignments Programming Languages in PDF only on Docsity!

1

Functional ProgrammingFunctional Programming

Chapter 13

2

Functional Programming StyleFunctional Programming Style

z Write many small functions (2-liners)

z Each loop corresponds to 1 function

z No assignment, only function calls

z Write base cases of recursion

– case for empty list, maybe for atoms

– maybe case for singleton list

z Write recursive cases

– case(s) for nonempty list

3

Example: Nesting of ParenthesesExample: Nesting of Parentheses

z Base case empty list: 1

z Base case atoms: 0

z Recursive case: max (1+car, cdr)

z Finished function

(define (nest x)

(cond ((null? x) 1)

((not (pair? x)) 0)

(else (max (+ 1 (nest (car x)))

(nest (cdr x))))))

4

Example: Integer EquationExample: Integer Equation

z Given: lengths l1, l2, l3, len

z Can len be constructed from pieces

of lengths l1, l2, and l3?

(define (test len l1 l2 l3)

(if (<= len 0) (= len 0)

(or (test (- len l1) l1 l2 l3)

(test (- len l2) l1 l2 l3)

(test (- len l3) l1 l2 l3))))

5

ML vs. SchemeML vs. Scheme

z Scheme

– primitive syntax

– dynamically typed

– Lists as built-in data type

z ML

– fancy syntax

– statically typed, type inference

– recursive data types

6

History of MLHistory of ML

z Developed at Edinburgh (early ’80s)

as Meta-Language for a program

verification system

z Now a general purpose language

z Standard ML (1991)

from ftp://ftp.research.bell-labs.com/dist/smlnj/

z Development of ML 2000

z CAML from INRIA, Moby from Lucent

10

ListsLists

z Empty list

nil

z Cons

z List syntax

1 :: 2 :: 3 :: nil

[1, 2, 3]

z Lists are homogenous

11

Recursive Data TypesRecursive Data Types

z Enumeration types

datatype Color = red | blue | green

z Integer trees

datatype Tree = Leaf of int

| Node of Tree * Tree

12

Pattern MatchingPattern Matching

  • fun foo red = 0 | foo blue = 1 | foo green = 2; val foo = fn : Color -> int
  • fun max (i, j: int) = if i > j then i else j; val max = fn : int * int -> int
  • fun height (Leaf _) = 0 | height (Node (l, r)) = 1 + max (height l, height r); val height = fn : Tree -> int

13

Parametric PolymorphismParametric Polymorphism

  • fun id x = x;

val id = fn : ’a -> ’a

  • datatype ’a Tree = Leaf of ’a

| Node of ’a Tree * ’a Tree;

  • fun height (Leaf _) = 0

| height (Node (l, r)) =

1 + max (height l, height r);

val height = fn : ’a Tree -> int

14

More ExamplesMore Examples

  • fun length nil = 0 | length (_::t) = 1 + length t; val length : fn ’a list -> int
  • length [1, 2, 3]; val it = 3 : int
  • height (Node (Leaf 1, Node (Leaf 2, Leaf 3))) val it = 2 : int
  • id 42; val it = 42 : int
  • id [1, 2, 3]; val it = [1,2,3] : int list

15

TuplesTuples

  • (1, 2);

val it = (1,2) : int * int

  • fun add (x : int, y) = x + y;

val add : fn int * int -> int

z Tuples have at least two elements

z Extra parentheses don’t count

z All functions have one argument!