4

I need to create a function. Within this I need the following to happen:

List 1: '(a 5 6)
List 2: '(c 8 10)
List 3: '(d 4 9)

Above are the lists. I need to ignore the 1st column of each list (this being a, c and d.) and then put the 2nd column in a vector. Then do the same for the 3rd column but a separate vector. Once this is done I'll carry out some small arithmetic between the two and write the results of each into a third vector.

I have very little Clojure experience and come from a Java background. I've tried to use let

By doing so I've only been able to create a var which stores the 2nd and 3rd item in a single list only. (e.g. List 1's 5 & 6.) However I need the vector to be [5 8 4].

1
  • This sounds like matrix arithmetic. Depending on what else you want to do with the data, core.matrix might be useful. Commented Oct 27, 2015 at 4:06

1 Answer 1

4
(defn answer [& [list-1 list-2 list-3 :as lists]]
   (->> lists                    ; ((a 5 6) (c 8 10) (d 4 9))
        (map rest)               ; ((5 6) (8 10) (4 9))
        (apply map vector)       ; ([5 8 4] [6 10 9])
        (apply small-arithmetic) ; (small-arithmetic [5 8 4] [6 10 9])
  ))

assuming small-arithmetic is a function taking the desired two vectors and returning the third vector.

Sign up to request clarification or add additional context in comments.

9 Comments

Can you just run me through your answer please? So lists 1, 2 and 3 are all within a list. So a list of lists. Would they be put in the function as a parameter like that? If it's a list of lists would this change your answer at all..
I have declared a variadic function to collect the three argument lists in lists. (answer list-1 list-2 list-3) is the intended invocation. An equivalent declaration would be (defn answer [list-1 list-2 list-3] (->> [list-1 list-2 list-3] ...;(as above)
If the input is a list of lists you chould just (defn answer [lists] ...) and invoke it e. g. (answer [list-1 list-2 list-3])
I'll give it a run through, I'm fairly confident I'm going to struggle with its implementation though so I'll keep it open for a while!
How would I do the function for the small-arithmetic?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.