I tried this problem again.
So I did this:
(defn nth* [coll, number]
(let [acc 0]
(loop [coll coll acc]
(if == acc number))
(first coll)
(recur (next coll) (+acc 1 ))
but when I do :
(nth* (1,2,3,4,5)2)) I see a cannot cast error message.
Is this because Clojure cannot figure out what the coll and what the number
is ?
Roelof
Op woensdag 30 april 2014 12:53:01 UTC+2 schreef James Reeves:
> On 30 April 2014 11:41, Roelof Wobben <[email protected] <javascript:>>wrote:
>>
>>
>> Op woensdag 30 april 2014 12:14:39 UTC+2 schreef James Reeves:
>>>
>>>
>>> Consider how you might add a counter to the loop. You'll want to
>>> increment the counter, then stop when it reaches the desired number.
>>>
>>
>>
>> So without checking it so out of my head I would do this
>>
>> (defn last* [coll, number]
>> (loop [coll coll]
>> (if == counter number))
>> (first coll)
>> (recur (next coll) (+counter 1 ))
>>
>
> Essentially yes. You need to initiate the counter to zero, and you still
> need a condition to make sure you don't go past the end of the seq (in case
> n is more than the length of the list), but you've basically got it.
>
> - James
>
--
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.