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:38

Java can do something like this with nested classes but the effort is not really worth it in most cases.

public class MyThing
{
   abstract class OtherThing
   {
      abstract int add(int x);
   }
   protected int i;

   public MyThing(int a) { i = a; }

   public int add(final int n)
   {
      OtherThing thing = new OtherThing() {
         public int add(int x)
         {
            return n + x;
         }
      };
      return thing.add(i);
   }

   public static void main(String[] args) { }
}


What the hell did I just write?

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