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-16 19:48

EXPERT C SOLUTIONS!
Works for 0 >= n >= 7 . Could be extended for a larger range of n


#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int (**F)(int);

int F00(int i) { return 0 + i; }
int F01(int i) { return 1 + i; }
int F02(int i) { return 2 + i; }
int F03(int i) { return 3 + i; }
int F04(int i) { return 4 + i; }
int F05(int i) { return 5 + i; }
int F06(int i) { return 6 + i; }
int F07(int i) { return 7 + i; }

int main(int argc, char **argv) {
    unsigned long n,i;
    printf("n:%lu\n",n);
    F = malloc(8*sizeof(void*));
    F[0] = F00;
    F[1] = F01;
    F[2] = F02;
    F[3] = F03;
    F[4] = F04;
    F[5] = F05;
    F[6] = F06;
    F[7] = F07;
    n = argv[1] ? strtoul(argv[1],0,10) : 0;
    i = argv[2] ? strtoul(argv[2],0,10) : 0;
    int (*f)(int) = F[n];
    printf("answer:%d\n",f(i));
    return 0;
}

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