From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Lee Newsgroups: gmane.emacs.help Subject: Re: How to improve the readability of (any) LISP or any highlevel functional language to the level of FORTH ? Date: Thu, 6 Jan 2011 14:59:21 -0800 (PST) Organization: http://groups.google.com Message-ID: <370ce4b6-b0ed-4c84-b008-099226a6734b@j32g2000prh.googlegroups.com> References: <80ceeca0-1d32-47d1-ba96-feb4d9729c3a@v17g2000yqv.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1294381834 11121 80.91.229.12 (7 Jan 2011 06:30:34 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 7 Jan 2011 06:30:34 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jan 07 07:30:29 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Pb5qG-0000gE-Uw for geh-help-gnu-emacs@m.gmane.org; Fri, 07 Jan 2011 07:30:29 +0100 Original-Received: from localhost ([127.0.0.1]:40766 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pb5qG-00029O-6b for geh-help-gnu-emacs@m.gmane.org; Fri, 07 Jan 2011 01:30:28 -0500 Original-Path: usenet.stanford.edu!postnews.google.com!j32g2000prh.googlegroups.com!not-for-mail Original-Newsgroups: comp.lang.functional, comp.lang.lisp, gnu.emacs.help, comp.lang.forth, comp.lang.prolog Original-Lines: 273 Original-NNTP-Posting-Host: 76.126.112.84 Original-X-Trace: posting.google.com 1294354762 26390 127.0.0.1 (6 Jan 2011 22:59:22 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Thu, 6 Jan 2011 22:59:22 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j32g2000prh.googlegroups.com; posting-host=76.126.112.84; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10, gzip(gfe) Original-Xref: usenet.stanford.edu comp.lang.functional:69134 comp.lang.lisp:297362 gnu.emacs.help:184078 comp.lang.forth:160410 comp.lang.prolog:44054 X-Mailman-Approved-At: Fri, 07 Jan 2011 01:29:36 -0500 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:78280 Archived-At: On Jan 5, 6:58=C2=A0am, Xah Lee wrote: > On Dec 31 2010, 11:04=C2=A0pm, girosenth wrote: > > > How to improve the readability of (any) LISP or any highlevel > > functional language to the level of FORTH ? some basics that might be helpful. =E3=80=88What's Function, What's Operator?=E3=80=89 http://xahlee.org/math/function_and_operators.html -------------------------------------------------- What's Function, What's Operator? Xah Lee, 2010-12-14, 2011-01-06 Typically, we understand what =E2=80=9Cfunction=E2=80=9D and =E2=80=9Copera= tor=E2=80=9D mean, but programer may have a hard time explaining them, and mathematician may never thought about it. Here, we clarify a bit on the meaning of the word =E2=80=9Cfunction=E2=80=9D and =E2=80=9Coperator=E2=80=9D, their conte= xt, their relation. -------------------------------- Function is a Mathematical Concept Function you probably understand. A function takes a input, and output a value for a given input. The inputs are called =E2=80=9Cparameters=E2=80= =9D. A specific set of input is called =E2=80=9Carguments=E2=80=9D. The number of = parameters of a function is called the function's =E2=80=9Carity=E2=80=9D. So, for exa= mple, the function =E2=80=9Csin=E2=80=9D has arity 1. A constant, such as 35, =CF=80,= can be considered as functions of no parameter, so they are functions of arity 0. A function of 2 parameters, such as f(x,y) :=3D x+y has arity 2. Function is a mathematical concept. Viewed in another way, it's a map from one space (aka set) to another. -------------------------------- Operator is About Notation A operator, is less of a mathematical concept, but more of a concept of notation. Or, in computer language contexts, a element of syntax. A =E2=80=9Coperator=E2=80=9D is a symbol (or symbols) that are written to ind= icate operations. For example, we write 1+2, the =E2=80=9C+=E2=80=9D is a operato= r, and the =E2=80=9C1=E2=80=9D and =E2=80=9C2=E2=80=9D are its =E2=80=9Coperands=E2=80= =9D. Mathematically, operator is a function that acts on its operands. The operands are the arguments of the function. -------------------------------- Binary Operators Typically, operators takes 2 arguments, the left and right of the symbol. e.g. 2=C3=973, 3/4, 2^3, union {3,4}=E2=88=AA{2,4,1}, etc. -------------------------------- Unary Operators But there are also 1-argument operators. For example the minus sign -3, and the logical not =C2=AC sign, the factorial 3!, square root =E2=88= =9A3. -------------------------------- Multi-symbol Operators Operators can also involve other forms with more symbols. For example, the absolute sign |-3|, floor =E2=8C=8A3.8=E2=8C=8B uses a bracket, summati= on =E2=88=91 takes 4 arguments, a expression, a variable, and start and end values. The anti-derivative (aka indefinite integral) =E2=88=AB takes 2 arguments, a expression and a symbol. In traditional notation, the integration operator involves 2 symbols, =E2=88=AB and =E2=85=86. For example, we write= =E2=88=AB sin(x) =E2=85=86x . -------------------------------- Implicit Operators Operator can be a bit complicated. For example -3 can be interpreted in several ways. We can think of the minus sign as unary operator acting on the argument 3. So, mathematically, it is a function of 1 arity that returns the addictive inverse of the element 3. Or, we can interprete it as one single entity, a element of Reals denoted -3. When we write 3-2, the ways to interprete it gets a bit more complex. One way to think of it as a notation shorthand for 3 + (-2). The -2 part can be thought of as before. Another way is to think of - as a binary operator on 3 and 2, but this seemingly simple interpretation is a bit complex. Because, what is math definition of the minus binary function? I'm not sure how it can be defined in terms of algebra without ultimately thinking of it as additon of 2 elements, one being a addictive inverse. The other way is to think of it as a real line, moving the first argument to the left by a distance of the second argument. Of course ultimately these are equivalent, but i can't think of a simple, direct, interpretation that can serve as a math foundation. Also, this is directly related to how does one interprete division, such as 3/2. The multiplication operator also gets complicated. For example, when we write 3 x, it usually means 3*x. The space acts as implicit multiplication sign. But when we write 3 +2, the space there has no significance. When we write 3x, even there is no space nor any operator, but we mean 3*x. As a computer language syntax based on traditional notation, the parsing rule is not trivial. Operator Stickiness There's also the concept of =E2=80=9Coperator stickiness=E2=80=9D (aka =E2= =80=9Coperator precendence=E2=80=9D) at work that makes expressions with operators more concise. When we write3=E2=96=B34=E2=96=B25, how do you know it's (3=E2=96= =B34)=E2=96=B25 or 3=E2=96=B3(4=E2=96=B25)? The concept of operator stickiness is needed to resolve that. Otherwise, you'll need to always write 3+(4*5) instead of the simpler 3+4*5. But this again, also introduced more complexity. When you have a expression of tens of different operators, it becomes a problem of remembering the stickiness grammar for each operator. (in practice, when you are not sure about the procedence, you usually just use explicit priority indicator by parens. This often happens in programing with logic operators (e.g. and &&, or ||, not !.) Forgetting Operator Precedence is a common error in programing. In written math for human communication, it is prone to miscommunication.) -------------------------------- Operator is tied to Notation Because the concept of =E2=80=9Coperator=E2=80=9D more has to do with synta= x and notation, and when considering traditional math notation of writing in =E2=80=9C2-dimensions=E2=80=9D, also the fact that traditional math notatio= n has a lot ambiguities, it gets a bit complicated and not as imprecise as we like. (See: The Problems of Traditional Math Notation) Mathematically, operator and function are the same thing. Math function in traditional notation has the form e.g. sin(x), f(x,y), where the things inside the paren are its parameter/arguments, and function name is placed to the left. Operators are useful because writing everything out in full function notation gets very cumbersome and hard to read. For example, we write 3+4*5 instead of plus(3,times(4,5)) or +(3,*(4,5)). Here's a example of traditional notation using operators: -b+=E2=88=9A(b^2-4 a c)/(2 a) If you don't allow space as implicit multiplication sign, then you have to write: -b+=E2=88=9A((b^2)-4*a*c)/(2*a) If you don't allow the the concept of operator precedence, then you have to write: (-b)+(=E2=88=9A((b^2)-((4*a)*c))/(2*a)) If you prefer the structural clarity of the traditional function notation f(x), you have to write: /(+(-(b),=E2=88=9A(+(^(b,2),-(*(4,a,c))))),*(2,a)) If you prefer words than symbols, as traditionally practiced when writing out functions, you have to write: divide(add(minus(b),sqrt(add(power(b, 2),minus(times(4,a,c))))),times(2,a)) The notation using operators is much concise, readable, but at the cost of relatively complex lexical grammar. The full functional notation is precise, grammatically simple, but difficult to read. In math context, it's best to think of functions instead of operator, and sometimes also use a uniform function notation, where all arguments are explicitly indicated in one uniform way. Here's what Wikipedia has to say about operators: Operation (mathematics). Quote: An operation =CF=89 is a function of the form =CF=89 : X1 =C3=97 =E2=80= =A6 =C3=97 Xk =E2=86=92 Y. The sets Xj are called the domains of the operation, the set Y is called the codomain of the operation, and the fixed non-negative integer k (the number of arguments) is called the type or arity of the operation. Note that it doesn't really speaks of =E2=80=9Coperators=E2=80=9D, but spea= ks of =E2=80=9Coperations=E2=80=9D, and flatly defines operation as a function. -------------------------------- Traditional Function Notation sin(x) Isn't Perfect Note that, even the functional notation such as sin(x), isn't perfect. Problem of Functions Returning Functions Normally, with full function notation, you'd expect that execution order of operations clearly corresponds to the nesting structure. For example, in our example before: divide(add(minus(b),sqrt(add(power(b, 2),minus(times(4,a,c))))),times(2,a)) The inner-most parts are evaluated first. But there's a problem when a function returns a function. For example, the derivative takes a function and returns a function. We might write: derivative(f) Now, if we want to evaluate the result at 3, then we might write: derivative(f)(3) You can see that the notation no longer nests. The operator precedence issue is back. Now, you need to have a slightly more complex notion of precedence to work out the notation. One solution to this is the lisp language's syntax. In lisp syntax, everything is written inside a paren. The first element is the function name, the rest is its arguments. So, sin(x) would be written as (sin,x). (comma is used for separator) Our derivative example would then be: ((derivative,f),3) In this way, the syntax remains a pure nested form, and provides the utmost precision. Our formula example in fully nested syntax be: (/,(+,(-,b),(=E2=88=9A,(+,(^,b,2),(-,(*,4,a,c))))),(*,2,a)) We could change the comma separator to space. So, it would look like this: (/ (+ (- b) (=E2=88=9A (+ (^ b 2) (- (* 4 a c))))) (* 2 a)) (Note: actual lisp language's syntax is not 100% regular. Many of its syntax does not have the form (a b c ...). See: Fundamental Problems of Lisp.) -------------------------------- Syntax Design for Computer Languages Mixing Operator Syntax with Full Function Notation Syntax In most computer language, they allow both the operator and full function syntax. For example, you can write (sin(x))^2+3. (almost all languages do this; e.g. C, C++, C#, Java, Pascal, Perl, Python, Ruby, Bash, PowerShell, Haskell, OCaml. The only exception is lisps.) (Though, almost all computer languages does not have a regular syntax, in fact, non of any major computer lang has a syntax specification. The closest one that has a somewhat regular and simple syntax grammar is Mathematica. See: The Concepts and Confusions of Prefix, Infix, Postfix and Fully Nested Notations =E2=97=87 Math Notations, Computer Languages, and the =E2=80=9CForm=E2=80=9D in Formalism.) Mixed form is normal, and most flexible. Because, not all functions have a associated operator symbol. And, writing everything in nested brackets is not readible and hard to write too. The question is, is it possible to design a syntax, that is fully regular with a very simple lexical grammar, and easy to read and write? Xah =E2=88=91 http://xahlee.org/ =E2=98=84