gusl: (Default)
[personal profile] gusl
flet saves us from repeating often-used arguments:

(defun funky (x)
  (flet ((mult (y) (* x y)))
    (mult 4)))

(funky 5)
=> 20

A more realistic example, in which we want functions to use implicit arguments: (Coming soon...)

It would be nice to have something to automatically bring a function in and out of an flet, i.e. the scope of another function.
* flet2defun would make explicit the implicit arguments (i.e. variables that are global relative to the flet) by adding these variables to the lambda list.
* defun2flet would do the opposite: it would make the lambda list shorter by eliminating arguments already defined in the new scope. The latter can be accomplished by currying.

This implicit/explicit distinction can be observed in C++: class methods can use class properties as well as arguments passed. Class properties are usually seen as not being "inherent" to the function, or at least it's not what one should focus on when trying to understanding what the function does.

(no subject)

Date: 2006-06-17 08:44 pm (UTC)
tiedyedave: (Default)
From: [personal profile] tiedyedave
flet2defun is often called lambda lifting, though it's usually performed as a compilation operation rather than a runtime operation.

The situation in C++ is actually a formal equivalent: objects are just closures anyway. The class's "properties", "fields", or whatever are just captured variables, specifically the captured arguments of a constructor call.

(no subject)

Date: 2006-06-17 11:14 pm (UTC)
From: [identity profile]
Cool. I wish this were a meta-programming operation, though. The IDE would recognize these functions for what they are and automatically change styles, as you asked it to.

(no subject)

Date: 2006-06-18 08:27 pm (UTC)
From: [identity profile]
Testing functions defined inside an flet can be really annoying. A lambda lifter would help eliminate this drudgery.

syntactic sugar

Date: 2006-06-18 08:04 am (UTC)
From: [identity profile]
the various let forms are just syntactic sugar for lambda expressions, e.g. the plain let:

(let ( (foo bar) ) expression) = ( (lambda (foo) expression) bar )

Multiple variables are bound "in parallel"; then there's another let form which binds sequentially, i.e. it is equivalent to nested lambdas. flet is (from the viewpoint of the lambda calculus) a perversion which was caused by an historical error which some early lisps made by having a separate binding space for functions vs. other values. In Scheme no such form exists because function names are no different from other symbols and the first position of a form is evaluated the same as all positions.

All of these let forms can be defined in Scheme using hygienic macros, i.e. they are syntactic sugar which need not be known to the compiler at all. Such extensions are shallow yet can clarify intent.

Second order operations like those you ask for are the heart of functional programming. The soul is metaprogramming. I find that Common Lisp obscures the real power of lisp so that it takes Common Lisp programmers a long time to really "get it", and some never do. Scheme has its problems, too, but it is much cleaner and, with guaranteed end-call optimization and call/cc I can create new abstractions and have them just as general and efficient as the built-in features of the language - and that is more important than providing large numbers of pre-built features.

At the time that Common Lisp was accepted as an ISO standard, it was the largest standard document of any computer language. At the same time, the Scheme standard (also an ISO standard) was the shortest, yet it includes a complete denotational semantics of the language as an appendix.

Oh well, you get it that I like Scheme!



gusl: (Default)

December 2016

18 192021222324

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags