On 6 May 2014 13:09, Gary Trakhman wrote:
> Last + butlast?
Yeah, there are a few more concise solutions to this problem if you're
familiar with the standard library:
(last (butlast coll))
(second (reverse coll))
(peek (pop (vec coll)))
And a more efficient loop-solution would just ke
Yes, except there's no need to pass counter into the loop if it doesn't
change.
(And naming a constant value "counter" is a little weird).
- James
On 6 May 2014 13:07, Roelof Wobben wrote:
> Found the answer :
>
>
> (defn secondlast [coll]
> (let [number 0
> counter (- (count co
Last + butlast?
On Tuesday, May 6, 2014, Roelof Wobben wrote:
> Found the answer :
>
> (defn secondlast [coll]
> (let [number 0
> counter (- (count coll)2) ]
> (loop [coll coll counter counter number number]
> (if (= counter number)
> (first coll)
>
Found the answer :
(defn secondlast [coll]
(let [number 0
counter (- (count coll)2) ]
(loop [coll coll counter counter number number]
(if (= counter number)
(first coll)
(recur (next coll) counter (+ number 1))
and without the loop there will be
As far as I understand recur is going back to the loop so number is never
going back to 0
but I see the problem (-(count coll)2 is changing it value and that schould
not happen.
So I think I have to set the initial value of counter in the let.
what schould happen is this
coll
You've still got number and counter mixed up. Try going through the loop
you've made and writing down their values:
1. coll (list 1 2 3 4 5), number 0, counter 3
2. coll (list 2 3 4 5), number 0, counter 1
3. coll (list 3 4 5), number 0, counter 2
4. coll (list 4 5), number 0, counter 3
5. coll (l
Op dinsdag 6 mei 2014 12:40:24 UTC+2 schreef James Reeves:
>
> Well, == should be =, but more importantly you're mixing up number and
> counter.
>
> Step through your code one bit at a time:
>
> 1. You set number to 0
> 2. You set counter to (count coll) - 2
> 3. If number and counter are not eq
Well, == should be =, but more importantly you're mixing up number and
counter.
Step through your code one bit at a time:
1. You set number to 0
2. You set counter to (count coll) - 2
3. If number and counter are not equal...
4. Recur with the next part of the list, and set the counter to (+ numb
I changed everything to this :
(ns forclojure.core)
(defn secondlast [coll]
(let [number 0 ]
(loop [coll coll counter (- (count coll)2)]
(if (== counter number)
(first coll)
(recur (next coll) (+ number 1))
But now I get a loop which never ends.
Roelo
Op dinsdag 6 mei 2014 10:43:40 UTC+2 schreef Benjamin R. Haskell:
>
> `loop` expects a vector of binding forms (with initial values), not just a
> vector of names.
>
> (loop [coll counter]; means there is one `loop` binding named `coll`,
> with the initial value of `counter`
>
> To fix that
)2)]
> (loop [coll counter]
> (if (== counter number)
> (first coll)
> (recur (next coll) (+ counter 1 ))
>
> But as soon as I try to send it to REPL I see this message:
>
> CompilerException java.lang.IllegalArgumentException: Mismatched argument
send it to REPL I see this message:
CompilerException java.lang.IllegalArgumentException: Mismatched argument
count to recur, expected: 1 args, got: 2,
compiling:(/home/roelof/clojure/forclojure/src/forclojure/core.clj:9:11)
Roelof
--
You received this message because you are subscribed to
12 matches
Mail list logo