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 20:11

DISCLAIMER: THIS IS A TOY! I DO NOT ACTUALLY CODE LIKE THIS

Should run under ikarus with the iksqlite lib


(import (rnrs) (zeptodyne iksqlite))
(define make-counter
  (letrec* ((db (sqlite-open "/tmp/test.db"))
            (n 0)
            (make-name
             (lambda ()
               (set! n (+ n 1))
               (string-append "counter"
                              (number->string n)))))
           (sqlite-exec db "create table retarded_counter (k string, v integer)")
           (lambda (x)
             (let ((name (make-name)))
               (sqlite-exec db (format "insert into retarded_counter values (~s,~s)" name x))
               (lambda ()
                 (let ((ans (string->number
                             (vector-ref
                              (car (sqlite-exec db (format "select v from retarded_counter where k = ~s" name))) 0))))
                   (sqlite-exec db (format "update retarded_counter set v = ~s where k = ~s"
                                           (+ ans 1) name))
                   ans))))))

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