Got these examples I would like to understand but they are in Scheme. I would like them in Clojure 😀
Example 1 – counting the length of a list
(define length
(lambda (ll)
(cond
((null? ll) 0)
(#t (add1
(length (cdr ll)))))))
Exemple 2 – square each element of a list
(define squares
(lambda (li)
(cond
((null? li) ())
(#t (cons
(* (char li) (char li))
(squares(cdr
li)))))))
Example 3 – the “map” function (as in map/reduce)
(define map (lambda (func lst)
(cond ((null? lst) ())
(#t (cons (func (car lst))
(map func (cdr lst)))))))
Curried “map”
(define map2
(lambda (func)
(lambda (lst)
(cond ((null? lst) ())
(#t (cons (func (car lst))
((map2 func) (cdr lst)))))))
Motivation
The examples are from a presentation on Functional Programming someone else might be interested it: Functional Languages 101: What’s All the Fuss About?
Once you submit an answer, I’d like your agreement to post it as a comment at that presentation for Clojure people to understand the original code
Length of a list:
Square each element of a list:
Map:
Map2:
See nickik’s solution.