Question

In: Computer Science

In Clojure, how do I enter a vector of vectors and print the specified index in...

In Clojure, how do I enter a vector of vectors and print the specified index in each vector? i.e. I enter [1 2 3 4] [5 6 7 8] and my function gives me [1 5] when I call (index vector 0)?

Solutions

Expert Solution

check out the solution and do COMMENT if any doubts.

-----------------------------------------------------------------

(ns clojure.examples.program1
(:gen-class))

(defn index [v n]
(def cnt (count v)) ; count the number of vectors present in 'v' and store it in 'cnt'
(loop [i 0 mat []] ; loop begins from i=0 and an empty vector called 'mat'
(if (< i cnt) ; check if i < cnt
(recur (inc i) (conj mat (get-in v [i n]))) ; increment i and get the specified indexed element 'n' of vector 'v' and concat to vector 'mat' using 'conj'
(println mat)))) ; print the final vector as resultant   

; the below is the way to enter vector of vectors
(def vect [[1 2 3 4] [5 6 7 8]])
; pass the defined vector of vectors 'vect' and index '0' to function 'index'
; call #1
(index vect 0)
; call #2
(index vect 2)

--------------------------------------------------------------------------------------------------------

---

OUTPUT ::


Related Solutions

What role do disease vectors play in the spread of vector-borne diseases? Explain with some vector...
What role do disease vectors play in the spread of vector-borne diseases? Explain with some vector examples and their hosts; using mosquitos as an example, describe major mosquitos-borne diseases and their control and prevention.
In c++ how do I search a vector for a name and then determine if the...
In c++ how do I search a vector for a name and then determine if the name is found?
What is an interrupt vector table. How do I create an interrupt vector table in Arm...
What is an interrupt vector table. How do I create an interrupt vector table in Arm Architecture . Please include as much information as you can, including pictures, examples etc. Thank you very much.
How do I find the angle between two three-dimensional vectors?
How do I find the angle between two three-dimensional vectors?
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Write a program that will print the whole numbers from a user-specified minimum to a user-specified...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified maximum. Display the total amount of numbers printed.
Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
Determine if each of the following sets of vectors U is a subspace of the specified...
Determine if each of the following sets of vectors U is a subspace of the specified vector space, and if so, describe the set geometrically: (a) U ⊆ R2, where U = {〈x1,x2〉 : x1 = 0} (b) U ⊆ R2, where U = {〈x1,x2〉 : x1x2 = 0} (c) U⊆R3,whereU={〈x1,x2,x3〉:〈1,2,3〉·〈x1,x2,x3〉=0} (d) U ⊆ R3, where U = {〈x1,x2,x3〉 : 〈1,2,2〉 · 〈x1,x2,x3〉 = 0 and 〈1, 3, 0〉 · 〈x1, x2, x3〉 = 0}
How do you define identity? I need a long answer, well specified the concept.
How do you define identity? I need a long answer, well specified the concept.
How do you define identity? I need a long answer, well specified the concept with examples.
How do you define identity? I need a long answer, well specified the concept with examples.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT