Where should I use defrecord in clojure?

I consider structs to be effectively deprecated so I don’t use them at all. When I have a fixed set of well-known keys used in many map instances, I usually create a record. The big benefits are: Performance Generated class has a type that I can switch on in multimethods or other situations With additional … Read more

How to compare two functions for equivalence, as in (λx.2*x) == (λx.x+x)?

It’s pretty well-known that general function equality is undecidable in general, so you’ll have to pick a subset of the problem that you’re interested in. You might consider some of these partial solutions: Presburger arithmetic is a decidable fragment of first-order logic + arithmetic. The universe package offers function equality tests for total functions with … Read more

How to Iterate over Map Keys and Values in Clojure?

That’s expected behavior. (doseq [x … y …]) will iterate over every item in y for every item in x. Instead, you should iterate over the map itself once. (seq some-map) will return a list of two-item vectors, one for each key/value pair in the map. (Really they’re clojure.lang.MapEntry, but behave like 2-item vectors.) user> … Read more

What is the difference between ; and ;; in Clojure code comments?

There is no difference as far as the interpreter is concerned. Think of ; ;; ;;; and ;;;; as different heading levels. Here is my personal use convention: ;;;; Top-of-file level comments, such as a description of the whole file/module/namespace ;;; Documentation for major code sections (i.e. groups of functions) within the file. ;; Documentation … Read more

Clojure on Android [closed]

Yes, here is main project I am aware of: https://github.com/remvee/clojurehelloandroid And here is a little tutorial http://riddell.us/ClojureAndAndroidWithEmacsOnUbuntu.html though I would not be surprised if this tutorial is outdated, as it was over a year ago when I played with the code following this tutorial, and remvee’s code has since been updated. EDIT: see the update … Read more

How to list the functions of a namespace?

I normally call (keys (ns-publics ‘foo)) to list Vars exported by the namespace foo; e.g. for clojure.contrib.monads this returns (defmonad censor m-when-not m+write+m maybe-m maybe-t …) (the … stands for quite a lot more). More generally, there’s a bunch of functions whose names start in ns- which list Vars by namespace, with certain additional criteria … Read more

How to read mentally Lisp/Clojure code

I think concat is a bad example to try to understand. It’s a core function and it’s more low-level than code you would normally write yourself, because it strives to be efficient. Another thing to keep in mind is that Clojure code is extremely dense compared to Java code. A little Clojure code does a … Read more

Building a Clojure app with a command-line interface?

Here is an example of using its with-command-line macro. The following code specifies a trivial class with a main method that does nothing but print out the values of its command line arguments. (ns cmd-line-demo (:gen-class) (:use clojure.contrib.command-line)) (defn -main [& args] (with-command-line args “Command line demo” [[foo “This is the description for foo” 1] … Read more

How many primitives does it take to build a LISP machine? Ten, seven or five?

Basic Predicates/F-functions McCarthy’s Elementary S-functions and Predicates were: atom Which was necessary because car and cdr are defined for lists only, which means you cannot count on any sort of answer to indicate what was happening if you gave car an atom. eq For testing equality between atoms. car For returning the first half (address) … Read more