Re: find the last and the nth item without using last or nth

2014-05-06 Thread Roelof Wobben
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

Re: find the last and the nth item without using last or nth

2014-05-06 Thread James Reeves
You have a number of typos, syntax errors and missing brackets in your source code. It should be: (defn nth* [coll number] (loop [coll coll, acc 0] (if (= acc number) (first coll) (recur (next coll) (+ acc 1) Additionally, you're using an unquoted list to test. Use a vector

Re: find the last and the nth item without using last or nth

2014-05-06 Thread Roelof Wobben
Thanks, Roelof Op dinsdag 6 mei 2014 17:11:52 UTC+2 schreef James Reeves: You have a number of typos, syntax errors and missing brackets in your source code. It should be: (defn nth* [coll number] (loop [coll coll, acc 0] (if (= acc number) (first coll) (recur (next

find the last and the nth item without using last or nth

2014-04-30 Thread Roelof Wobben
Hello, IM busy with 4Clojure and I have now two challenges which I think can be solved the same way. On a list I first have to find the last item of a list without using last and after that I have to do the same for a nth item and im not allowed to use nth. Now I can convert the list to a

Re: find the last and the nth item without using last or nth

2014-04-30 Thread James Reeves
Your intuition that this can be done through recursion or a loop is correct (the latter being a more specialised version of the former). When dealing with recursion, it often helps to consider the base case, i.e. where the recursion stops. Typically this is when you get to zero or one elements.

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Tassilo Horn
Roelof Wobben rwob...@hotmail.com writes: Could a recurvice way or a loop do the trick. Yes. And how do I then take the right value. For nth, you need a counter that you can increment in each recursion step. For last, you return the first element of the list whose rest is the empty list.

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Roelof Wobben
Op woensdag 30 april 2014 08:21:40 UTC+2 schreef James Reeves: Your intuition that this can be done through recursion or a loop is correct (the latter being a more specialised version of the former). When dealing with recursion, it often helps to consider the base case, i.e. where the

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Roelof Wobben
Op woensdag 30 april 2014 08:22:07 UTC+2 schreef Tassilo Horn: Roelof Wobben rwo...@hotmail.com javascript: writes: Could a recurvice way or a loop do the trick. Yes. And how do I then take the right value. For nth, you need a counter that you can increment in each recursion

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Roelof Wobben
Op woensdag 30 april 2014 08:50:22 UTC+2 schreef Roelof Wobben: Op woensdag 30 april 2014 08:22:07 UTC+2 schreef Tassilo Horn: Roelof Wobben rwo...@hotmail.com writes: Could a recurvice way or a loop do the trick. Yes. And how do I then take the right value. For nth, you need

Re: find the last and the nth item without using last or nth

2014-04-30 Thread James Reeves
On 30 April 2014 07:49, Roelof Wobben rwob...@hotmail.com wrote: Op woensdag 30 april 2014 08:21:40 UTC+2 schreef James Reeves: 1. How do you find the last element of a seq with exactly one element? the last and the first element are the same Right. So to formalise it: (defn

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Tassilo Horn
Roelof Wobben rwob...@hotmail.com writes: For nth, you need a counter that you can increment in each recursion step. For last, you return the first element of the list whose rest is the empty list. Thanks, and if I use cons (item, list) then item is the value I want. I only have to print

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Roelof Wobben
Op woensdag 30 april 2014 09:58:26 UTC+2 schreef James Reeves: On 30 April 2014 07:49, Roelof Wobben rwo...@hotmail.com javascript:wrote: Op woensdag 30 april 2014 08:21:40 UTC+2 schreef James Reeves: 1. How do you find the last element of a seq with exactly one element? the last

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Roelof Wobben
Op woensdag 30 april 2014 09:58:26 UTC+2 schreef James Reeves: On 30 April 2014 07:49, Roelof Wobben rwo...@hotmail.com javascript:wrote: Op woensdag 30 april 2014 08:21:40 UTC+2 schreef James Reeves: 1. How do you find the last element of a seq with exactly one element? the last

Re: find the last and the nth item without using last or nth

2014-04-30 Thread James Reeves
On 30 April 2014 10:41, Roelof Wobben rwob...@hotmail.com wrote: Op woensdag 30 april 2014 09:58:26 UTC+2 schreef James Reeves: Unlike vectors, seqs are simple structures and don't know their own length. You can count seqs, but this involves iterating through every element in turn. If

Re: find the last and the nth item without using last or nth

2014-04-30 Thread Roelof Wobben
Op woensdag 30 april 2014 12:14:39 UTC+2 schreef James Reeves: On 30 April 2014 10:41, Roelof Wobben rwo...@hotmail.com javascript:wrote: Op woensdag 30 april 2014 09:58:26 UTC+2 schreef James Reeves: Unlike vectors, seqs are simple structures and don't know their own length. You

Re: find the last and the nth item without using last or nth

2014-04-30 Thread James Reeves
On 30 April 2014 11:41, Roelof Wobben rwob...@hotmail.com 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