(module fnc (lib "swindle.ss" "swindle") (provide (all-defined)) ;(provide isin star invert flip converge cur cure) "fnc.ss -- various functional things" (define (isin l) "1-curry of member. Probably should be named in?" (lambda (x) (member x l))) (define (iseq x) (lambda (y) (eq? x y))) (define (isequal x) (lambda (y) (equal? x y))) (define (pipe . fs) (apply compose (reverse fs))) (define (fn-name f) (let ((name (object-name f))) (if name name 'anonymous))) (define (uncurry fn) "This name is from Python. In Haskell it was uncurry" (lambda (args) (apply fn args))) (define star uncurry) (define (flip fn) (lambda args (apply fn (reverse args)))) (define (converge f init &key [test equal?] [count #f]) "fn*a->a--run f on x until x stabilises" (let converge ((x init) (new (f init)) (i 0)) (if (or (and count (= i count)) (test new x)) x (converge new (f new) (if count (add1 i) 0))))) #|These currying macros are like cute in that they really only return a single-nested lambda: the next call to the curried function must 'use up' the closure, so you can't (directly) create a mapping whose output is a list of closures like you can in Haskell. However, the interface is more like Haskell's in that it's less powerful, since you can only curry the rightmost argument. I think the name is better but something like !! or ^ might still be better. Also, the macro *looks* much more efficient, because every call to the closure produced by cure has to append its arguments before calling the function. I have NOT tested this. On the other hand, 'args' is evaluated once and once only for the function. You COULD wrap the macro in a let to do the same thing. I guess. |# (define-syntax curl (syntax-rules () ((_ f arg args ...) (lambda moreargs (apply f arg args ... moreargs))) ((_ f) f))) (define (cur f . args) (lambda moreargs (apply f (append args moreargs)))))