Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

PROGRAMMING CHALLENGE

Name: Anonymous 2010-07-16 18:57


This one should be difficult enough for you guys,

Write a function foo that takes a number n and returns a function that takes a number i, and returns n incremented by i.


My submission, in Scheme


(define (foo num)
  (lambda (x) (+ x num)))

Name: Anonymous 2010-07-17 2:45

>>52
Why would you do that?
OOP and closures are somewhat equivalent.

A class with a (possibly private) member called i, a constructor which takes i, and an add method which takes n is enough.

Usage would be like:

Adder adder = new Adder(i);
int newValue = adder.Add(n,i);


It's not exactly what OP requested for, but the concepts are somewhat equivalent. It's easier to implement OO systems if you already have closures and macros, but it's harder to implement closures if you only have classes, but aside from that the functionality they present is somewhat equivalent.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List