Haskell-style curry function in Scheme

#lang racket

(define (>curry f . n-opt)
  (let loop ((final-args '()) 
             (n (if (empty? n-opt) 2 (car n-opt))))
    (if (zero? n)
        (apply f final-args)
        (lambda nth-args
          (loop (append final-args nth-args) (sub1 n))))))

(>curry list 0)                      ;=> '() ;; should this case really be…
((>curry list 1) 'a)                 ;=> '(a)
(((>curry list) 'a 'b 'c) 'd 'e 'f)  ;=> '(a b c d e f)
((((>curry list 3) 'a 1) 'b 2) 'c 3) ;=> '(a 1 b 2 c 3)

(define (curry< f . n-opt)
  (let loop ((final-args '()) 
             (n (if (empty? n-opt) 2 (car n-opt))))
    (if (zero? n)
        (apply f final-args)
        (lambda nth-args
          (loop (append nth-args final-args) (sub1 n))))))

(curry< list 0)                      ;=> '() ;; …part of the interface, though?
((curry< list 1) 'a)                 ;=> '(a)
(((curry< list) 'a 'b 'c) 'd 'e 'f)  ;=> '(d e f a b c)
((((curry< list 3) 'a 1) 'b 2) 'c 3) ;=> '(c 3 b 2 a 1)
 
  1. uros posted this
Blog comments powered by Disqus