🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #3994

Re: type checking problem

#3994
From: Douglas E. Quale
Date: Fri, 17 May 1991 05:51
49 lines
2756 bytes
In article <1991May16.182932.26327@kodak.kodak.com> cok@islsun.Kodak.COM (David Cok) writes:
>Here is a real-life problem which advocates of various languages/typing systems
>are invited to comment on. 
> 
>I want to write a routine to optimize a function (numerically) over one of its 
>arguments, returning the abscissa of the extremum.  I'll settle for 
>optimization over the first argument.  I'll even restrict myself to that 
>argument and the return value being doubles.  So I want a function 
>                optimize() 
>which returns a double and takes as an argument a pointer to a function which
>returns a double and takes as arguments a double *and other arguments*.  It 
>would be used like this
>
>		optimum_x = optimize(f,...)
>
>The problem is what to do with the other arguments to f.  One solution is to
>allow creation of functions on the fly.  Given, say, myf of four arguments,
>one would write
>		optimum_x = optimize(myf(#1,a,b,c))
>in which myf(#1,a,b,c) is a function of one argument obtained by supplying
>values for three of the four arguments of f.  This can be done in interpreted
>environments like Mathematica.  Can it be done in any compiled programming
>languages?  Smalltalk with blocks perhaps?
>

Languages that can create new functions at runtime can handle this without
even breaking a sweat.  It's a one-liner.  (Warning folks, these are
professional languages.  Do not try this in C.)

Smalltalk can indeed do this with blocks, but as I don't know Smalltalk I'll
demonstrate it in lisp and SML.  Someone else is sure to be able to give you
the Smalltalk equivalent.

The function optimize is written to take a unary function to optimize.

In lisp
	(optimize #'(lambda (x) (f x a b c)) ...)

In SML
	optimize(fn x => f(x, a, b, c), ...)

The function objects that are created are called closures, because they close
over the environment containing the values of a, b and c.
In general this process is called currying, and is standard tool of the trade
in languages based on the lambda calculus.  In languages that can't create
new functions it's a standard pain in the ass.

-- Doug Quale
quale@saavik.cs.wisc.edu
Technical Details
Message-ID: <1991May17.055123.27068@daffy.cs.wisc.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!spool.mu.edu!uwm.edu!uwvax!daffy!saavik.cs.wisc.edu!quale
References: <1991May16.182932.26327@kodak.kodak.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!spool.mu.edu!uwm.edu!uwvax!daffy!saavik.cs.wisc.edu!quale
From: quale@saavik.cs.wisc.edu (Douglas E. Quale)
Newsgroups: comp.lang.misc
Subject: Re: type checking problem
Message-ID: <1991May17.055123.27068@daffy.cs.wisc.edu>
Date: 17 May 91 05:51:23 GMT
Article-I.D.: daffy.1991May17.055123.27068
References: <1991May16.182932.26327@kodak.kodak.com>
Sender: news@daffy.cs.wisc.edu (The News)
Organization: University of Wisconsin -- Madison
Lines: 49