Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some lisps put the function name outside of the argument list in a function definition?

common lisp and clojure do

(defn function-name (arg1 arg2) (body))

racket/scheme does

(defn (function-name arg1 arg2) (body))

The latter makes more sense to me because the syntax for function definition and invocation is similar. What's the reasoning for the former?

like image 677
Boris Avatar asked Jan 21 '17 00:01

Boris


1 Answers

TL;DR It's not quite as simple as you put it. The syntax is slightly more different and every time someone create a language they get to decide new syntax, usually to make it more concise that previous languages or the language has a design that requires it. Of course what is "better" is a matter of opinion.

In Scheme (and the descendant Racket) you have one namespace for both variables and functions. Thus you use define for everything.

(define variable 10)
(define function (lambda (arg1 arg2) body ...))

Now define has a short cut for that last one, which is:

(define (function arg1 arg2) body ...)

So the rule is if the first part is a pair? it is supposed to be expanded to the longer form with lambda. That it resembles an application is just a conicident I guess. This extra feature is just to save some keystrokes and is often omitted in books like The little Schemer since its confusing to have two ways and the learner and they might think defining a binding for a function is more special than defining 10, which is ridiculous. The rest argument usually confuses. eg. these are the same:

(define (test . x) x)
(define test (lambda x x))

In Common Lisp you have two namespaces and thus defun is only used for global scope and there are function equivalents of variable forms that makes functions. in defun you have a list first argument in Common Lisp too, but it does something completely different:

(defun (setf name) (new-value)
  body ...)

This provides a way to get the same symmetry as other accessors in CL. eg. if (car x) gets the car value from a cons (setf (car x) 10) will alter it. In my example name can be used in the same manner as car. It's quite handy.

Clojure does it with def and fn and uses an array for parameters:

(def function (fn [arg1 agr2] body ...))

defn is just a shorthand version just like the define that starts with a pair. How it didn't end up more similar to Scheme is perhaps either the fact that the parameter data is an array or that they kept syntax as close to the original as possible. If I remember correctly you can have a function name as second argument with fn as well making fn and defn look almost identical.

like image 141
Sylwester Avatar answered Oct 26 '22 21:10

Sylwester