Docsity

COSC 3015 Midterm Review: Functional Programming Concepts and Haskell - Prof. James L. Cal, Exams of Computer Science

Toggle sidebar
bg1
COSC 3015: Midterm Review
Lecture given by Prof. Caldwell and scribed by Sunil Kothari
October 14, 2008
1 Review
We will have a midterm exam on Thursday. The exam will be closed book.
HW1 was a warm up and we looked at Qsort and had you analyze some
code. Something like that might not be a bad question.
HW3 was like beginning of the real functional programming.
1.1 Lambda terms - Higher Order functions
\xโˆ’> e where e is a haskell expression
f x y =e
is same as
\f x โ†’ \yโ†’e
is same as
f=\xโ†’ \yโ†’e
For example, fxy =x+y
f x =\yโ†’x+y
is same as
f=\xโ†’ \yโ†’x+y
Given,
plusc x y =x+y
>:t plusc 5
Num a => a -> a
1.2 HW3 - curry and uncurry
curry :: ((a, b)โ†’c)โ†’(aโ†’(bโ†’c))
curry f x y =f(x, y)
1
pf3
pf4
pf5

Partial preview of the text

Download COSC 3015 Midterm Review: Functional Programming Concepts and Haskell - Prof. James L. Cal and more Exams Computer Science in PDF only on Docsity!

COSC 3015: Midterm Review

Lecture given by Prof. Caldwell and scribed by Sunil Kothari

October 14, 2008

1 Review

We will have a midterm exam on Thursday. The exam will be closed book. HW1 was a warm up and we looked at Qsort and had you analyze some code. Something like that might not be a bad question. HW3 was like beginning of the real functional programming.

1.1 Lambda terms - Higher Order functions

\xโˆ’ > e where e is a haskell expression f x y = e

is same as \f x โ†’ \y โ†’ e

is same as f = \x โ†’ \y โ†’ e

For example, f xy = x + y f x = \y โ†’ x + y

is same as f = \x โ†’ \y โ†’ x + y Given, plusc x y = x + y

:t plusc 5 Num a => a -> a

1.2 HW3 - curry and uncurry

curry :: ((a, b) โ†’ c) โ†’ (a โ†’ (b โ†’ c)) curry f x y = f (x, y)

uncurry :: (a โ†’ b โ†’ c) โ†’ ((a, b) โ†’ c) uncurry f (x, y) = f x y

1.3 HW4- Extensionality

Extensionality - equality between functions. There is no general algorithm to decide when two functions are equal. Itโ€™s equivalent to halting problem but you can look at individual programs and see if it halts. So same is the case with equality on functions. If f, g : a โ†’ b then

f = g def = โˆ€x : a.f x = gx

Function composition f โ—ฆ g = f (g x)

So what is the type of function composition (โ—ฆ) :: (b โ†’ c) โ†’ (a โ†’ b) โ†’ a โ†’ c

In the HW, we had to prove that curry plus = plusc

curryplus :: Int โ†’ (Int โ†’ Int) plusc :: Int โ†’ (Int โ†’ Int)

Q: How do we show that the two are equal? A: Use extensionality.

Proof. Choose arb. x โˆˆ Int and show curry plus x = plusc x

Choose an arb. y โˆˆ Int and show curry plus x y = plusc x y

On the left, curry plusc x y = plus (x, y) = x + y On the right, plusc x y = plus (x, y) = x + y

Note that we had to apply extensionality twice since we have a function of two arguments. If we apply extensionality once, we are still showing two functions are equal. Note that we cannot choose arbitrarily any y but something different from x. And in the HW I asked you to write flip. f lip :: (a โ†’ b โ†’ c) โ†’ b โ†’ a โ†’ c f lip x y = f y x

1.6 HW7 - Induction (on Nat)

data N at = Zero | Succ N at (+) :: N at โ†’ N at โ†’ N at m + zero = m m + (Succ k) = Succ (m + k)

To show that some property P holds for all finite nats (i.e. not involving โŠฅ):

  • Show P is true for Zero
  • Assuming P is true for K, show P is true for (Succ k)

To show for all Nats

  • Show P (โŠฅ)

Q: What type of induction is that? A: It is really ordinarily mathematical induction but in fact is structural induc- tion - more general in the same way.

There will be probably a question on induction principle.

1.7 HW

In Haskell, lists are defined as data [a] = [] | (::) a [a]

Or alternatively, data List a = N il | Cons a (List a)

In the HW the lists are defined as : data Liste a = N il | Snoc (Liste a) a

It might be interesting to see what is induction principle for Lists formed from snoc? convert :: (Listea) โ†’ [a]

1.8 HW9 - Writing list functions

Given type and desired behavior, define the function.

1.9 HW 10 - Modeling finite functions as lists

Implement finite functions using lists and instantiate the Eq type class for this data type so that f == g returns true iff f and g are functional, and they really are equal.

For finite functions, we can do this kind of thing but not for infinite functions. If we use equality on Lists, then the finite function equality would be slightly different. F inF un[(1, 2)(2, 1)] == F inF un[(2, 1), (1, 2)]

1.10 HW 11

Lists are defined as: data List a = N il | Cons a (List a)

Structural Induction on lists (P (N il) โˆง โˆ€x : a, โˆ€m : List a. P (m) โ‡’ P (Cons a m)) โ‡’ โˆ€l : List a. P (l)

This can also be written as:

  1. P (N il).
  2. Assuming P (m) show P (x :: m) for arb. x and m.

A binary tree is defined as: data Btree a = Leaf a | F ork (Btree a) (Btree a)

1.11 HW 12

data Btree a = Leaf a | F ork (Btree a) (Btree a)

(โˆ€x : a, P (Leaf x) โˆง โˆ€t 1 , t2 : (Btree a), P (t1) โˆง P (t2) โ‡’ P (F ork t 1 t2)) โ‡’ โˆ€t : (Btree a), P (t)

1.12 HW

data (Ord a) => Stree a = N ull|F ork (Stree a) a (Stree a)

So what does the induction principle look like? (P (N ull) โˆง โˆ€x : a, โˆ€t 1 , t2 : (Stree a), P (t1) โˆง P (t2) โ‡’ P (F ork t 1 x t2)) โ‡’ โˆ€t : Stree a, P (t).

So what about map? map f N ull = N ull map f (f ork t 1 x t2) = F ork (map f t1) (f x) (map f t2)