Exercise 3: First class vs first order

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

Exercise 3: First class vs first order

Beitrag von Osterlaus »

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
BASIC-Programmierer
Beiträge: 119
Registriert: 19. Apr 2012 18:12

Re: Exercise 3: First class vs first order

Beitrag von Seldon »

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

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

Re: Exercise 3: First class vs first order

Beitrag von f_jakob »

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
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

Re: Exercise 3: First class vs first order

Beitrag von Osterlaus »

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
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

Re: Exercise 3: First class vs first order

Beitrag von Osterlaus »

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
Mausschubser
Beiträge: 50
Registriert: 27. Okt 2009 14:05

Re: Exercise 3: First class vs first order

Beitrag von f_jakob »

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
Computerversteher
Beiträge: 381
Registriert: 18. Okt 2007 11:36

Re: Exercise 3: First class vs first order

Beitrag von Wambolo »

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
BSc Spammer
Beiträge: 1263
Registriert: 23. Aug 2007 12:46
Wohnort: DA

Re: Exercise 3: First class vs first order

Beitrag von Osterlaus »

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

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

Re: Exercise 3: First class vs first order

Beitrag von Seldon »

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()
      case Cons(head, tail) => Cons(interp(App(mapper, head)),
                               interp(Map(tail, mapper)))
      case _ => sys.error("Invalid list expression for function Map")
    }
Application (adding 1 to each list element):

Code: Alles auswählen

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

Antworten

Zurück zu „Archiv“