Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic mathematical calculations in Clojure vs. F#

I have come across the following F# sample and found it intriguing.

http://www.codeproject.com/KB/net-languages/SymbolicCalcInFS.aspx

Does Clojure have language/library facilities for doing something like this with ease? It is ok to force Polish notation for formulas, if that makes things easier.

Thanks, and let me know if there are questions.

like image 952
Hamish Grubijan Avatar asked Dec 02 '22 04:12

Hamish Grubijan


2 Answers

Lisp has long history in symbolic computing. See the AI case study book by Peter Norvig. Lisp provides a lot of great language features to abstract the common operations on the symbols. Sometimes you can write really concise code (more concise/short than F#).

Static languages like F# have strong type systems and convenient pattern matching on the data types. The compiler could find errors that are caught by the type system, e.g. lack of considering one special case. Thinking about types with your data could also reduce the chances for runtime error. The type inference in F# also makes F# code very concise.

like image 91
Yin Zhu Avatar answered Dec 04 '22 14:12

Yin Zhu


I don't know much about Clojure, but here are, at least, some pointers.

The key feature that makes the F# code nice is pattern matching on algebraic data types. An algebraic data type is for example the declaration of the Expression type (that is used to represent mathematical expressions) and pattern matching is the match construct that is used to check for various known cases when implementing simplification or differentiation.

I don't think that Clojure has any built-in support for pattern matching, but it can be implemented as a library. One library that looks quite interesting is the patter-match module (in Clojars). Here is an example that uses it to implement algebraic evaluator (which is quite close to the F# article).

Another thing that appears in the F# article is active patterns (which allow you to declare and reuse patterns). I don't think that there is a Clojure library for that, but given the flexibility of the language, it should be possible to implement them too (however, they are not really that necessary in the F# article)

like image 38
Tomas Petricek Avatar answered Dec 04 '22 14:12

Tomas Petricek