I don't see a reason for f(x) sugar, but if I had to do some complex formulas I'd consider looking into a macro to enable infix math. On Dec 27, 2011 2:44 PM, "daly" <d...@axiom-developer.org> wrote:
> On Mon, 2011-12-26 at 10:09 -0800, Louis Yu Lu wrote: > > Recently, I found freedom of coding playing with Clojure with over 20 > > years’ experience on other program languages, Previously, I had > > several trials of learning Lisp, but never got traction. > > > > However there are two syntax notations always bother me, it feels > > unnatural with my experience on other programming languages and math: > > > > 1) Prefix operator for math formula. Jeffrey Bester’s math library > > https://github.com/jbester/cljext/blob/master/cljext/math.clj has > > brought in the infix notation and solved the problem; > > 2) The leading item of a list follows the parenthesis when it serves > > as a function name, (f x) instead of f(x). I am fully aware of many > > discussions on alternative syntax for Clojure and Lisp in the past, > > one stream is here > http://groups.google.com/group/clojure/browse_thread/thread/319a1c77ed718ba/3e4be7484b7cbe38 > > My proposition is enhance Clojure to accept both (f x) and f(x), the > > leading item can appear either after ‘(‘ as usual with classic Lisp > > notion: > > (println "Hello," "world!") > > or with conventional function call notation: > > println("Hello," "world!") > > The f(x) notation is just a syntax sugar, the only restriction is no > > space between the leading item and '('. If one or more spaces in > > between, the item will be treated as a separate symbol. Either notion > > or a mix of them can be used in the same program. Here are some > > examples: > > > > ; classic Lisp syntax notation > > (defn pig-latin [word] > > (let [first-letter (first word)] > > (if (.contains "aeiou" (str first-letter)) > > (str word "ay") > > (str (subs word 1) first-letter "ay")))) > > (println (pig-latin "red")) > > (println (pig-latin "orange")) > > > > ; f(x) syntax notation > > defn(pig-latin [word] > > let( [first-letter first(word)] > > if(.contains("aeiou", str(first-letter)) > > str(word, "ay") > > str(subs(word, 1), first-letter, "ay")))) > > println(pig-latin("red")) > > println(pig-latin("orange")) > > > > ; classic Lisp syntax notation > > (defn fib [n] > > (if (< n 2) > > 1 > > (+ (fib (dec n) ) (fib (- n 2))))) > > (fib 36) > > > > ; mix of two notations > > defn(fib [n] > > if((< n 2) > > 1 > > (+ fib(dec(n)) fib((- n 2))) > > ) > > ) > > fib(36) > > > > I have made minor changes (dozen lines) in one file (LispReader.java) > > from Clojure source repository to implement this syntax sugar(code > > posted at https://github.com/louisyulu/clojure-fx). It works with well- > > formed classic Clojure code and the proposed notation. > > Running the test suite from Clojure distribution results two error: > > One in the file compilation.clj line 90: > > (recur y ^Long(rem x y)))))] > > The problem is ^Long(… becomes function call, the fix is adding a > > space in between > > (recur y ^Long (rem x y)))))] > > > > The second is in the file sequences.clj line 1131: > > (is (=(partition-by #{\a \e \i \o \u} "abcdefghijklm") > > The problem is (=(… , ‘=’ is associated with following ‘(‘, the > > intention was the previous ‘(‘, the fix is also adding a space in > > between > > (is (= (partition-by #{\a \e \i \o \u} "abcdefghijklm") > > > > These two cases are caused by the not well-formed Clojure code > > (strictly speaking, each item in the list should be separated by a > > space). > > > > The proposed syntax sugar apparently pleases my eyes and fingers from > > conventional languages. With some experiments, I found the code is > > more readable for me to use f(x) notation for function call, and (op > > x) for operator. > > > > I like to hear from the community what kind of dark corner the > > proposed syntax sugar may run into. > > > > Thanks, > > Louis > > > > This is an idea that happens quite frequently. Axiom, a lisp-based > computer algebra system, originally had a "syntactic sugar" almost > exactly like the one proposed. I uploaded an ancient design > document for this kind of language syntax, called "boot". See > http://daly.axiom-developer.org/boot.tgz > > This turned into a huge point of discussion. The Axiom decision > to remove this "boot" language eventually resulted in a fork of > the Axiom project. > > Based on long experience I would strongly recommend NOT going > down this path. > > Tim Daly > > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en