Can Clojure functions be anonymous, curried and allow composition?

309 views
Skip to first unread message

dokondr

unread,
Nov 24, 2008, 5:54:15 PM11/24/08
to Clojure
How can I write the following examples in Clojure that in Haskell will
be:

-- 1) Curried function:
Prelude> let f = (+) 1
Prelude> f 1
2

-- 2) Anonymous function:
Prelude> let f2 = \x -> x * 2
Prelude> f2 2
4

-- 3) Function composition:
Prelude> (f2 . f) 3
8
Prelude>

Jeff Rose

unread,
Nov 24, 2008, 6:03:58 PM11/24/08
to clo...@googlegroups.com
dokondr wrote:
> How can I write the following examples in Clojure that in Haskell will
> be:
>
> -- 1) Curried function:
> Prelude> let f = (+) 1
> Prelude> f 1
> 2

(def f (partial + 1)) ; => #'user/f
(f 1) ; => 2

>
> -- 2) Anonymous function:
> Prelude> let f2 = \x -> x * 2
> Prelude> f2 2
> 4

(def f2 (fn [x] (* x 2))) ; => #'user/f2
(f2 2) ; => 4

>
> -- 3) Function composition:
> Prelude> (f2 . f) 3
> 8
> Prelude>

((comp f2 f) 3) ; => 8


voila.

Jeff

Jarkko Oranen

unread,
Nov 24, 2008, 6:06:38 PM11/24/08
to Clojure
1) (def fn1 (partial + 1))
2) (def fn2 #(* % 2)) or (fn [x] (* x 2)))
3) ((comp fn2 fn1) 3)

--
Jarkko

Stuart Halloway

unread,
Nov 24, 2008, 6:07:25 PM11/24/08
to clo...@googlegroups.com
>>
>> -- 2) Anonymous function:
>> Prelude> let f2 = \x -> x * 2
>> Prelude> f2 2
>> 4
>
> (def f2 (fn [x] (* x 2))) ; => #'user/f2
> (f2 2) ; => 4

Or even

(def f2 #(* % 2))

Stuart

Matt Moriarity

unread,
Nov 24, 2008, 6:10:03 PM11/24/08
to Clojure
1 is actually an example of partial application of functions more than
it is currying. Haskell's currying makes partial application far more
natural though. In Clojure you can use the (partial ...) macro to do
this:

user=> (def f (partial + 1))
user=> (f 1)
2

2 is done using the (fn ...) special form. It should be noted that
(defn ...) to define named functions is actually just a macro that
translates basically to (def name (fn ...))

user=> (def f2 (fn [x] (* x 2)))
user=> (f2 2)
4

3 is done with the (comp ...) macro:

user=> ((comp f2 f) 3)
8

Hope this helped!

Matt

dokondr

unread,
Nov 24, 2008, 6:14:19 PM11/24/08
to Clojure

On Nov 25, 2:06 am, Jarkko Oranen <Chous...@gmail.com> wrote:
...
> > -- 3) Function composition:
> > Prelude> (f2 . f) 3
> > 8
> > Prelude>
>
> 1) (def fn1 (partial + 1))
> 2) (def fn2 #(* % 2)) or (fn [x] (* x 2)))
> 3) ((comp fn2 fn1) 3)
>
> --
> Jarkko

And what is 'comp'?

Matt Moriarity

unread,
Nov 24, 2008, 6:18:08 PM11/24/08
to Clojure
comp composes functions just like the dot operator

Randall R Schulz

unread,
Nov 24, 2008, 6:46:28 PM11/24/08
to clo...@googlegroups.com

When in doubt:

- Clojure native API
<http://clojure.org/api>

- comp
<http://clojure.org/api#toc122>


Randall Schulz

J. McConnell

unread,
Nov 25, 2008, 5:02:38 PM11/25/08
to clo...@googlegroups.com

Or:

user=> (doc comp)
-------------------------
clojure.core/comp
([& fs])
Takes a set of functions and returns a fn that is the composition
of those fns. The returned fn takes a variable number of args,
applies the rightmost of fns to the args, the next
fn (right-to-left) to the result, etc.

- J.

Reply all
Reply to author
Forward
0 new messages