Re: why is this failing on a list
Hi Roelof, I start the 4Clojure exercises and I did a little project so I can keep a record of my solutions. It's really simple but also I've found it really helpful to see where my errors are. You can fork it from here... if you want. https://github.com/erlis/4Clojure You only have there the first 5 or 6 ones, the only thing you have to do is continue adding exercises and your answers. This will help you also to become familiar with your dev environment. I'm using Emacs but you can use whatever you want. Regards, Erlis On Sun, May 4, 2014 at 11:06 AM, James Reeves wrote: > Remember that indexes work from zero. So if you have a collection of 3 > elements: > > (nth ["a" "b" "c"] 2) => "c" > (nth ["a" "b" "c"] 1) => "b" > (nth ["a" "b" "c"] 0) => "a" > > I'd encourage you to open a REPL and try the solution you have to see what > you get if you get stuck. Often some experimentation will show you what > you're doing wrong. > > - James > > > On 4 May 2014 15:58, Roelof Wobben wrote: > >> oke, >> >> Then I think I have to work with a if then : >> >> The second test is already a vector but the thirth not >> >> (= (__ ["a" "b" "c"]) "b") >> >> (= (__ [[1 2] [3 4]]) [1 2]) >> >> I tried already the nth but I was also failing on the first. I think >> because of count because you cannot know how many values you have. >> >> (fn secondlast [v] >> (nth v (-(count v)1))) >> >> >> Roelof >> >> Op zondag 4 mei 2014 16:49:45 UTC+2 schreef Lee: >> >>> >>> >>> On May 4, 2014, at 10:42 AM, Roelof Wobben wrote: >>> >>> > For 4clojure I have to find the second to last item. >>> > >>> > So I did this: >>> > >>> > (fn secondlast [v] >>> > (get v (-(count v)1))) >>> > >>> > Now it's only failing at this test : (= (__ (list 1 2 3 4 5)) 4) >>> > >>> > Can anyone tell me where I did take the wrong way. >>> >>> >>> The "get" function with integer keys works for vectors but not for >>> lists: >>> >>> => (get [7 8 9] 1) >>> 8 >>> => (get '(7 8 9) 1) >>> nil >>> >>> So one option would be to call "vec" on the list before calling get: >>> >>> => (get (vec '(7 8 9)) 1) >>> 8 >>> >>> Another option would be to use "nth" instead of "get". >>> >>> -Lee >>> >>> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. >> 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 clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. > 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 clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: why is this failing on a list
Remember that indexes work from zero. So if you have a collection of 3 elements: (nth ["a" "b" "c"] 2) => "c" (nth ["a" "b" "c"] 1) => "b" (nth ["a" "b" "c"] 0) => "a" I'd encourage you to open a REPL and try the solution you have to see what you get if you get stuck. Often some experimentation will show you what you're doing wrong. - James On 4 May 2014 15:58, Roelof Wobben wrote: > oke, > > Then I think I have to work with a if then : > > The second test is already a vector but the thirth not > > (= (__ ["a" "b" "c"]) "b") > > (= (__ [[1 2] [3 4]]) [1 2]) > > I tried already the nth but I was also failing on the first. I think > because of count because you cannot know how many values you have. > > (fn secondlast [v] > (nth v (-(count v)1))) > > > Roelof > > Op zondag 4 mei 2014 16:49:45 UTC+2 schreef Lee: > >> >> >> On May 4, 2014, at 10:42 AM, Roelof Wobben wrote: >> >> > For 4clojure I have to find the second to last item. >> > >> > So I did this: >> > >> > (fn secondlast [v] >> > (get v (-(count v)1))) >> > >> > Now it's only failing at this test : (= (__ (list 1 2 3 4 5)) 4) >> > >> > Can anyone tell me where I did take the wrong way. >> >> >> The "get" function with integer keys works for vectors but not for lists: >> >> => (get [7 8 9] 1) >> 8 >> => (get '(7 8 9) 1) >> nil >> >> So one option would be to call "vec" on the list before calling get: >> >> => (get (vec '(7 8 9)) 1) >> 8 >> >> Another option would be to use "nth" instead of "get". >> >> -Lee >> >> -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. > 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 clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: why is this failing on a list
oke, Then I think I have to work with a if then : The second test is already a vector but the thirth not (= (__ ["a" "b" "c"]) "b") (= (__ [[1 2] [3 4]]) [1 2]) I tried already the nth but I was also failing on the first. I think because of count because you cannot know how many values you have. (fn secondlast [v] (nth v (-(count v)1))) Roelof Op zondag 4 mei 2014 16:49:45 UTC+2 schreef Lee: > > > On May 4, 2014, at 10:42 AM, Roelof Wobben > > wrote: > > > For 4clojure I have to find the second to last item. > > > > So I did this: > > > > (fn secondlast [v] > > (get v (-(count v)1))) > > > > Now it's only failing at this test : (= (__ (list 1 2 3 4 5)) 4) > > > > Can anyone tell me where I did take the wrong way. > > > The "get" function with integer keys works for vectors but not for lists: > > => (get [7 8 9] 1) > 8 > => (get '(7 8 9) 1) > nil > > So one option would be to call "vec" on the list before calling get: > > => (get (vec '(7 8 9)) 1) > 8 > > Another option would be to use "nth" instead of "get". > > -Lee > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: why is this failing on a list
Roelof, The whole point of something like 4clojure is for you to try to understand how things work and learn how to fix the errors that happen along the way. Open a repl and see what the error tells you. Also, you should have the clojure cheat sheet open to help you find what each function does. If everything fails, then you can come here, tell what you did, paste the error. If you do all that, you will have learned something. You are not helping yourself by asking here on the first problem you find. Best, Marcelo On Sunday, May 4, 2014 11:42:07 AM UTC-3, Roelof Wobben wrote: > > Hello, > > For 4clojure I have to find the second to last item. > > So I did this: > > (fn secondlast [v] > (get v (-(count v)1))) > > Now it's only failing at this test : (= (__ (list 1 2 3 4 5)) 4) > > Can anyone tell me where I did take the wrong way. > > Roelof > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: why is this failing on a list
Also, don't forget that vectors are zero indexed, so (- (count v) 1) will give you the last element, not the second last. Cheers, James On Sunday, May 4, 2014 4:49:45 PM UTC+2, Lee wrote: > > > > On May 4, 2014, at 10:42 AM, Roelof Wobben > > wrote: > > > For 4clojure I have to find the second to last item. > > > > So I did this: > > > > (fn secondlast [v] > > (get v (-(count v)1))) > > > > Now it's only failing at this test : (= (__ (list 1 2 3 4 5)) 4) > > > > Can anyone tell me where I did take the wrong way. > > > The "get" function with integer keys works for vectors but not for lists: > > => (get [7 8 9] 1) > 8 > => (get '(7 8 9) 1) > nil > > So one option would be to call "vec" on the list before calling get: > > => (get (vec '(7 8 9)) 1) > 8 > > Another option would be to use "nth" instead of "get". > > -Lee > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: why is this failing on a list
On May 4, 2014, at 10:42 AM, Roelof Wobben wrote: > For 4clojure I have to find the second to last item. > > So I did this: > > (fn secondlast [v] > (get v (-(count v)1))) > > Now it's only failing at this test : (= (__ (list 1 2 3 4 5)) 4) > > Can anyone tell me where I did take the wrong way. The "get" function with integer keys works for vectors but not for lists: => (get [7 8 9] 1) 8 => (get '(7 8 9) 1) nil So one option would be to call "vec" on the list before calling get: => (get (vec '(7 8 9)) 1) 8 Another option would be to use "nth" instead of "get". -Lee -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
why is this failing on a list
Hello, For 4clojure I have to find the second to last item. So I did this: (fn secondlast [v] (get v (-(count v)1))) Now it's only failing at this test : (= (__ (list 1 2 3 4 5)) 4) Can anyone tell me where I did take the wrong way. Roelof -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.