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
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
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
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
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.
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.
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
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
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
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
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
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
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
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
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
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
16 matches
Mail list logo