A: 

define is def in clojure, lambda is fn, function arguments are written as vectors [], not lists (), null? is empty, car is first, cdr is rest and the default case for cond is specified with :else, not #t.

So for your first example we get:

(def length
  (fn [ll]
    (cond
      (empty? ll) 0
      :else (+ 1 (length (rest ll))))))

This can be written a little more succinctly using defn instead of def and fn, but the same is true for the scheme version, so I chose the way that is the closest to the original.

The other examples can be translated the same way.

sepp2k
what about "func" ?
Belun
@Belun: That's just a name. `lambda (func lst)` is a lambda taking two arguments named `func` and `lst`. The clojure equivalent is `fn [func lst]`.
sepp2k
@sepp2k> here's a challenge for you: can you create and use a macro for example 1 and 2 ? :P
Belun
@Belun: A macro doing what?
sepp2k
its not an idiomatic version
nickik
@nickik: Neither is the scheme version. As I said, I gave the version that is most similar to the original.
sepp2k
@sepp2k> yes, 1 macro that gets some parameters and then can build those 2 functions
Belun
+3  A: 

Length of a list:

(defn my-length [lst]
    (loop [len 0 x lst]
        (if (empty? x)
            len
            (recur (+ 1 len) (rest x)))))

user=> (my-length '(1))
1
user=> (my-length '(1 2 3 4))
4
user=> (my-length '())
0

Square each element of a list:

(defn squares [lst]
    (loop [sqrs '() x lst]
       (if (empty? x)
           (reverse sqrs)
           (recur (cons (* (first x) (first x)) sqrs) (rest x)))))

user=> (squares '(1 2 3 4))
(1 4 9 16)

Map:

(defn my-map [func lst]
    (loop [res '() x lst]
       (if (empty? x) 
           (reverse res) 
           (recur (cons (func (first x)) res) (rest x)))))

user=> (my-map (fn [x] (* x 2)) '(1 2 3))
(2 4 6)

Map2:

See nickik's solution.

Vijay Mathew
+5  A: 

I have some stoff here: http://github.com/nickik/Essentials-of-PLs-in-Clojure/blob/master/src/EssentialsOfProgrammingLanguages/core.clj

Its all scheme to clojure stuff. You can download the source of the Essentials of Programming Languages-Book to have the Scheme code.


Here are your examples:

(defn length [lst]
    (cond
      (seq ll) 0
      :else (inc (length (rest lst))))))

Note: clojure has a count function


(defn squares1 [li]
   (cond (nil? (seq li)) (empty li)
     :else (conj (squares1 (rest li)) (* (first li) (first li)))))

(defn squares2 [li]
   (map #(* % %)  li))

(defn mymap [f coll]
        (cond (nil? (seq coll)) (empty coll)
          :else (conj (mymap f (rest coll)) (f (first coll)))))

(defn map2 [f]
    (fn [lst]
        (cond (nil? (seq lst)) (empty lst)
              :else (conj ((map2 f) (rest lst)) (f (first lst))))))

Note you can not just make a 1:1 translation. The diffrence between how '() evals and so on.

Here are the most importend ones

  • (nil? (seq list)) not (null? lst) because '() is not nil in clojure
  • conj is better then cons you can make the function work with mure datastructures
  • (empty lst) is better then '() because (empty lst) keeps the type vector, list, record, structs or something else
nickik
+2  A: 

And more clojurey translation of map:

(defn map
  [f coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (cons (f (first s)) (map f (rest s))))))

Exploit closures: define map-inner like map above.

(defn map
  [f]
  (fn [coll]
    (map-inner f coll)))

In idiomatic clojure normally you exploit that the nil is logical false.

(defn length
  [coll]
  (loop [s   (seq coll)
         len 0]
    (if s
      (recur (next s) (inc len))
      len)))

As with map: you would use lazy sequences instead of eager lists.

(defn squares
  [coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (let [fst (first s)]
        (cons (* fst fst) (squares (rest s)))))))
kotarak