How to convert this simple (and inefficient) fibonacci function to # format

120 views
Skip to first unread message

Angus

unread,
Nov 13, 2013, 12:41:46 PM11/13/13
to clo...@googlegroups.com
I know this fibonacci function is not optimal but I want to learn one step at a time.  I want to be able to apply this approach to 4clojure which disallows a lot of things including defn. 

This is my implementation so far: 

(defn fib [x] 
   (cond 
      (= x 0) 0 
      (= x 1) 1 
      :else 
         (+ (fib (- x 1)) (fib (- x 2)))))


So I thought about something like this:

(#(fn fib [x] 
   (cond 
      (= x 0) 0 
      (= x 1) 1 
      :else 
         (+ (fib (- x 1)) (fib (- x 2)))))
              8)

But trying that in a REPL I get error:

clojure.lang.ArityException: Wrong number of args (1) passed to: sandbox28956$eval28971$fn

So how can I call this as a one line REPL?

I though using apply with a one element list might work, but no.

(apply #(fn fib [x](cond (= x 0) 0 (= x 1) 1  :else  (+ (fib (- x 1)) (fib (- x 2))))) '(8))


Ben Wolfson

unread,
Nov 13, 2013, 12:46:21 PM11/13/13
to clo...@googlegroups.com
#(fn fib [x] ... ) creates a zero-arity function which, when called, will return a one-arity function. Just get rid of the #.


--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which may be sweet, aromatic, fermented or spirit-based. ... Family and social life also offer numerous other occasions to consume drinks for pleasure." [Larousse, "Drink" entry]

Jim - FooBar();

unread,
Nov 13, 2013, 12:46:57 PM11/13/13
to clo...@googlegroups.com
you don't need 'fn' when you're using #(...) - that is the whole point.
To not have to declare a function and its args explicitly.

this particular example you cannot write using #() syntax though because
it is recursing at 2 points and you cannot use 'recur'.

Jim

Angus

unread,
Nov 13, 2013, 1:47:51 PM11/13/13
to clo...@googlegroups.com
Ah, as in:

> ((fn fib [x](cond (= x 0) 0 (= x 1) 1  :else  (+ (fib (- x 1)) (fib (- x 2))))) 8))
21

Many thanks.

angus...@gmail.com

unread,
Nov 19, 2013, 10:22:13 AM11/19/13
to clo...@googlegroups.com
Did this get returned from my htc

Sent from my HTC
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/5xV74YC-9C4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages