[plt-scheme] Why do layman programmers care about Currying?

From: Richard Cleis (rcleis at mac.com)
Date: Thu Jan 1 00:28:41 EST 2009

On Dec 31, 2008, at 4:18 PM, Matthias Felleisen wrote:

>
> You could imagine a world that looks like this:
>
> #lang scheme
>
> (define (fcore a b c)
>  (+ a b c))
>
> (define (f #:a (a #f) #:b (b #f) #:c (c #f))
>  (cond
>    [(and a b c) (fcore a b c)]
>    [(and a b) (lambda (c) (fcore a b c))]
>    [(and a c) (lambda (b) (fcore a b c))]
>    [(and b c) (lambda (a) (fcore a b c))]
>    [a (lambda (b c) (fcore a b c))]
>    [b (lambda (a c) (fcore a b c))]
>    [c (lambda (a b) (fcore a b c))]))
>
> ((f #:a 0) 1 2)
> ((f #:a 0 #:c 2) 1)
> ..
>
> John Lamping explored this form of abstraction in his dissertation  
> and I always thought there was something neat about it.
>
> -- Matthias

In the spirit of the opening post, I offer a laymen application of  
this exact form.  To point telescopes at ufo's, fcore is replaced by a  
geometric calculation based on functions that are represented by  
strings, here.  Currying is useful for reasons such as:

1) the earth wobbles slowly with heavy-duty computations, so it only  
needs to be calculated occasionally.
2) most of the calculations don't need to be repeated for multiple  
telescopes
3) most of the calculations don't need to be repeated for multiple ufo's

I have never used keywords for this sort of thing; instead, I make  
function-producers with verbose names.  The keyword abstraction  
suggestion is worth considering, though, since it concentrates  
complication inside of a single producer.  Roughly: simple keywords  
and one function replace no-keywords and functions which need to be  
corralled by comments or a module, or user won't know what is available.

#lang scheme

(define  (view-info  ufo telescope earth)
   (string-append "Geometric calcs of: " ufo ", " telescope ", " earth))

(define  (produce-ufo-viewer #:ufo (ufo #f) #:telescope (telescope #f)  
#:earth (earth #f))
  (cond
    [(and ufo telescope earth) (lambda  ()    (view-info  ufo  
telescope earth))]
    [(and ufo telescope)       (lambda  (e)   (view-info  ufo  
telescope e))]
    [(and ufo earth)           (lambda  (t)   (view-info  ufo  
t         earth))]
    [(and telescope earth)     (lambda  (u)   (view-info  u    
telescope earth))]
    [ufo                       (lambda  (t e) (view-info  ufo  
t         e))]
    [telescope                 (lambda  (u e) (view-info  u    
telescope e))]
    [earth                     (lambda  (u t) (view-info  u    
t         earth))]))

((produce-ufo-viewer #:ufo "green ufo at 12:01" #:telescope "big  
telescope" #:earth "earth at 12:00"))
((produce-ufo-viewer #:telescope "big telescope" #:earth "earth at  
12:00") "green ufo at 12:01")

Welcome to DrScheme, version 4.1 [3m].
Language: Module; memory limit: 128 megabytes.
"Geometric calcs of: green ufo at 12:01, big telescope, earth at 12:00"
"Geometric calcs of: green ufo at 12:01, big telescope, earth at 12:00"
 >
rac


Posted on the users mailing list.