--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
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 clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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.You may wish to consider the following questions:1. How do you find the last element of a seq with exactly one element?
2. How do you find out whether a seq has one element, or more than one element?
3. How do you find out the last element if a seq with exactly two elements?
4. How do you find out the last element of a seq that may have one or two elements?
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
2. How do you find out whether a seq has one element, or more than one element?
Look at the length of the seq
3. How do you find out the last element if a seq with exactly two elements?
take the first element away and you have a seq of one element .
4. How do you find out the last element of a seq that may have one or two elements?
Look at the length. If it has one element , see question 1
if it has 2 elements, see question 3.
On 30 April 2014 07:49, Roelof Wobben <rwo...@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 sameRight. So to formalise it:(defn last* [coll](first coll))I'm using "last*" as the name so that it doesn't conflict with the function "last" in clojure.core.2. How do you find out whether a seq has one element, or more than one element?
Look at the length of the seqUnlike 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 all you want to do is find out whether there is more than one element, there's a much simpler and more efficient way.
In code, this would be:
(defn last* [coll](if (more-than-one-element? coll)(first (rest coll))(first coll)))The "more-than-one-element?" to be replaced with the answer to question 2, of course.Can you think of a way of making this function recursive to work with any number of elements?
(last* (rest coll))
- James
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 all you want to do is find out whether there is more than one element, there's a much simpler and more efficient way.
Yep, if there is only 1 item is a list (rest coll) is nil.
Can you think of a way of making this function recursive to work with any number of elements?I think I can but I have to try some things but untested it looks like this(defn last* [coll](if (nil? (rest coll)(first coll)))(last* (rest coll))
Now I wonder how I can make it work if I want to find lets say the 5th value of a array of any number of elementsThen rest is never nil. So at first glance if I do not know the length it cannnot be done recursive.I have to do it in a for loop.
On 30 April 2014 10:41, Roelof Wobben <rwo...@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 all you want to do is find out whether there is more than one element, there's a much simpler and more efficient way.Yep, if there is only 1 item is a list (rest coll) is nil.Almost. Clojure distinguishes between nil and the empty list. The function you want is "next":(rest [1]) => ()(next [1]) => nilWhy the distinction between next and rest? It has to do with laziness, and if you're interested I can go into more detail. Otherwise just remember that rest returns an empty list, and next returns nil.
(defn last* [coll](loop [coll coll](if (nil? (next coll))(first coll)(recur (next coll))))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.
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 ))