On 6 May 2014 13:09, Gary Trakhman <[email protected]> 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 keep the previous value
stored until the end of the loop is reached:
(loop [prev nil, coll (seq coll)]
(if (next coll)
(recur (first coll) (next coll)
prev))
- James
>
>
> On Tuesday, May 6, 2014, Roelof Wobben <[email protected]> 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)
>> (recur (next coll) counter (+ number 1))))))
>>
>> and without the loop there will be : (first(drop(reverse v))
>>
>> Roelof
>>
>>
>> Op dinsdag 6 mei 2014 13:36:53 UTC+2 schreef Roelof Wobben:
>>>
>>> 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 number counter
>>> 1) [1,2,3.4.5) 0 3
>>> 2) [2.3,4,5] 1 3
>>> 3) [3,4,5] 2 3
>>> 4) [ 4,5] 3 3
>>>
>>> so the (first coll) is executed which is 4.
>>>
>>> Am I at the right track now.
>>>
>>>
>>>
>>> Roelof
>>>
>>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to [email protected]
>>
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected]
>>
>> 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 [email protected].
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
>
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
>
> 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 [email protected].
>
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.