## Exercise 3: First class vs first order

Osterlaus
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

### Exercise 3: First class vs first order

Hi there,
I'm a bit confused about this task as I don't really know what to do... Is some tiny stuff where just map() is used with a lambda function already an example for first-class?

Seldon
BASIC-Programmierer
Beiträge: 119
Registriert: 19. Apr 2012 18:12

### Re: Exercise 3: First class vs first order

I thought so and used something similar as my first example.

f_jakob
Mausschubser
Beiträge: 50
Registriert: 27. Okt 2009 14:05

### Re: Exercise 3: First class vs first order

Is some tiny stuff […] already an example […]?
You do not need to provide fancy examples. Some of the niceness of first class and higher order functions is in fact, that you can write much less code.

Osterlaus
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

### Re: Exercise 3: First class vs first order

Yeeeeah, but that makes the following task a bit complicated. How should we implement something like a higher order function as map or filter in FAE if we cannot pass lists to a function? Or is this already a part of "extend FAE if you like"?

Osterlaus
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

### Re: Exercise 3: First class vs first order

Nobody with ideas on this? It's a bit bad planned that exercises are on friday and office hours on monday, otherwise I'd gone there to ask...

f_jakob
Mausschubser
Beiträge: 50
Registriert: 27. Okt 2009 14:05

### Re: Exercise 3: First class vs first order

You could of course extend FAE to support lists and then implement two common higher order functions on lists – but that is not necessary.

Think about advantages of first class functions compared to first order functions (what can you do with the former, what is not possible with the latter?) and provide two examples for that.

Wambolo
Computerversteher
Beiträge: 381
Registriert: 18. Okt 2007 11:36

### Re: Exercise 3: First class vs first order

Implement examples that make use of first-class functions and could not be written nicely with first-order functions only.
Who defines wether a solution is written nicely or not

Osterlaus
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

### Re: Exercise 3: First class vs first order

As this task is done now, would someone mind to share example solutions for implementations of first class functions in FAE?

Seldon
BASIC-Programmierer
Beiträge: 119
Registriert: 19. Apr 2012 18:12

### Re: Exercise 3: First class vs first order

1. Example of functions as values (the function returns a function, an adder):

Code: Alles auswählen

Fun('x, Fun('a, Add('a, 'x)))
2. Example of using map as a higher order function (required FAE extension):

Code: Alles auswählen

sealed abstract class NullOrCons extends FAE
case class Null() extends NullOrCons
case class Cons(head: FAE, tail: NullOrCons) extends NullOrCons

// in lack of recursion, Map had to be a language construct
case class Map(list: NullOrCons, mapper: FAE) extends FAE

// Map interpreted as follows:
case Map(list, mapper) => interp(list) match {
case Null() => Null()

Map(Cons(1, Cons(2, Cons(3, Null()))), Fun('a, Add('a, 1))) // => Cons(2, Cons(3, Cons(4, Null())))