CSC 4101: Programming Languages - Scheme and Functional Programming, Assignments of Programming Languages

An introduction to the csc 4101: programming languages course focusing on scheme and functional programming. Topics covered include mathematical functions, lambda calculus, scheme syntax, atomic data types, variable definitions, functions, anonymous functions, lists, list functions, and more. Students will learn about functional programming concepts such as first-class functions, higher-order functions, and curried functions.

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-afi
koofers-user-afi 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSC 4101: Programming Languages 1
1
Scheme
Scheme
Textbook, Sections 13.1 – 13.3, 13.7
2
Functional Programming
Functional Programming
zBased on mathematical functions
zTake argument, return value
zOnly function call, no assignment
zFunctions are first-class values
zE.g., functions can return functions
zRequires garbage collection
3
Lambda Calculus
Lambda Calculus
zMathematical functional language
zLanguage constructs
Variables: x
Primitive functions: *
Function abstraction:
λ
(x) x*x or
λ
x.x*x
Function call: f (x) or f x
zExample
(
λ
(x) x*x*x) (2) yields 8
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CSC 4101: Programming Languages - Scheme and Functional Programming and more Assignments Programming Languages in PDF only on Docsity!

1

SchemeScheme

Textbook, Sections 13.1 – 13.3, 13.

2

Functional ProgrammingFunctional Programming

z Based on mathematical functions

z Take argument, return value

z Only function call, no assignment

z Functions are first-class values

z E.g., functions can return functions

z Requires garbage collection

3

Lambda CalculusLambda Calculus

z Mathematical functional language

z Language constructs

  • Variables: x
  • *Primitive functions: **
  • Function abstraction: λ ( x ) _xx_* or λ x****. _xx_*
  • Function call: f ( x ) or f x

z Example

( λ ( x ) _xxx_** ) (2) yields 8

4

SchemeScheme

z Dialect of LISP (from MIT, ca. 1975)

z Lists as built-in data type

z Same syntax for lists and programs

z Functions are (first-class) data

z Dynamic, strong typing

z Interaction ( read-eval-print ) loop

5

Scheme SyntaxScheme Syntax

z S-expressions

s-expression ::= literals | symbols | ( s-expression... )

z Special forms for specific language

constructs

6

Atomic Data TypesAtomic Data Types

z Numbers

z Booleans

#t, #f

z Characters, Strings

#\a, #\newline, "Hello World!"

z Symbols

f, x, +, *, foo, bar, null?, set!

10

ListsLists

z Quotes

(quote x), ’x

z Empty list

’() , often assigned to the variable nil

z Nonempty list

’(1 2 3), ’(a b 3), ’(1 (2 3) 4)

11

List RepresentationList Representation

The list ’(1 2) is stored as

cons /
1 cons /
2 ()

12

List RepresentationList Representation

The list ’(1 (2 3) 4) is stored as cons /
1 cons /
cons cons / \ /
2 cons 4 () /
3 ()

13

List FunctionsList Functions

z Predefined functions

car, cdr, cons

z Examples

(car ’(1 2)) returns 1 (cdr ’(1 2)) returns (2) (cons 1 ’(2)) returns (1 2) (cons 1 2) returns (1. 2)

14

SS--ExpressionsExpressions

List Syntax S-Expression Syntax

Not a list (1. 2)

15

More ExamplesMore Examples

Expression Value

(car (cdr ’(1 2 3))) 2 (cadr ’(1 2 3))) 2 (cons 1 ’()) (1) (cons 1 2) (1. 2) (cons ’() ’()) (()) (car ’(())) ()

19

HigherHigher--Order Functions (cont’d)Order Functions (cont’d)

;; Curried version of twice (define (twice f) (lambda (x) (f (f x))))

(define quadruple (twice double)) (quadruple 2) ; returns 8 ((twice double) 2) ; returns 8 ((twice quadruple) 2) ; returns 32 ((twice (twice double)) 2) ; returns 32

20

Curried Function in C SyntaxCurried Function in C Syntax

(int ()(int)) twice (int (f)(int)) { int foo (int x) { return f(f(x)); }

return foo; }

(twice(double))(2);

21

ConditionalsConditionals

z If-then-else

(if (= x y) 1 2) ; in C: (x == y)? 1 : 2

z Cond

(cond ((= x y) 1) ((< x y) 2) (else 3))

z Anything non- #f is considered true

(if 1 2 3) ; returns 2

22

EqualityEquality

Expression Value

(eq? x x) #t (eq? ’x ’x) #t (eq? ’() ’()) #t (eq? 2 2) undefined (eq? (list 1) ’(1)) #f

(eqv? 2 2) #t (equal? (list 1) ’(1)) #t

23

RecursionRecursion

(define (fac n) (if (= n 0) 1 (* n (fac (- n 1)))))

(fac 5) ; returns 120 (fac 1000) ; that’s 2568 digits

24

Map FunctionMap Function

(define (mymap f l) (if (null? l) ’() (cons (f (car l)) (mymap f (cdr l)))))

(mymap double ’(1 2 3 4 5)) ; returns (2 4 6 8 10) (mymap (lambda (x) (+ x 1)) ’(1 2)) ; returns (2 3)

28

PartitioningPartitioning

(define (gt p ls) (filter (lambda (x) (> p x)) ls))

(define (le p ls) (filter (lambda (x) (<= p x)) ls))

29

Filtering a ListFiltering a List

(define (filter pred ls) (cond ((null? ls) ’()) ((pred (car ls)) (cons (car ls) (filter pred (cdr ls)))) (else (filter pred (cdr ls)))))

30

Assignment (but don’t use it)Assignment (but don’t use it)

Expression Value of x

(define x 3) 3 (set! x 5) 5

(define x (list 1 2)) (1 2) (set-car! x 3) (3 2) (set-cdr! x 4) (3. 4)