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
#promt the user to enter student name for i in range(0, 3): print ("Enter student's first...
#promt the user to enter student name for i in range(0, 3): print ("Enter student's first and last name") #variable for name name = input() #prompt the user to enter number of book purchased print ("Enter the number of book the student purchased this month") #varibale for number of book book_number = input() book_number = int (book_number) #point = int (point) if (book_number <=0): points = 0 elif (book_number <=3): points = 5 elif (book_number <=6): points = 10 elif...
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++...
In C++, create two vectors, one a vector of ints and one a vector of strings....
In C++, create two vectors, one a vector of ints and one a vector of strings. The user will fill each one with data, and then your program will display the values. Copy the template file /home/cs165new/check10a.cpp to your working directory. For this assignment, in order to focus our practice on the syntax of the vectors, you can put all of your code in main. Create and display a vector of ints as follows: Create a vector of ints. Prompt...
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}
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT