This question already has answers here:
Matrix transposition in clojure
(2 answers)
Closed 6 years ago.
I have a transpose function that works for fixed arities
(defn transpose [a b c]
(map list a b c))
(transpose '(1 2 3) '(4 5 6) '(7 8 9))
((1 4 7) (2 5 8) (3 6 9))
(apply transpose (list '(1 2 3) '(4 5 6) '(7 8 9)))
((1 4 7) (2 5 8) (3 6 9))
But how can I generalise this for n arguments so I could call it as follows:
(transpose '(1 2) '(4 5))
(transpose '(1 2 3) '(4 5 6) '(7 8 9))
...
I feel as thought I need something like the following
(defn transpose [& xs]
(apply (map list) xs))
But I can't get it to work.
You are so close:
(defn transpose [& xs]
(apply map list xs))
Related
I would like to write a clojure function that has the following behaviour :
(take 4 (floyd))
=> '((1) (2 3) (4 5 6) (7 8 9 10))
(take 3 (floyd))
=> '((1) (2 3) (4 5 6))
(take 1 (floyd))
=> '((1)))
I tried using partition and partition-all to validate these tests however i couldn't get the right solution. If you have any idea of how to do it, i would really appreciate a little help. I started using clojure a few weeks ago and still have some issues.
Thanks
Here's another option:
(defn floyd []
(map (fn [lo n] (range lo (+ lo n 1)))
(reductions + 1 (iterate inc 1))
(range)))
(take 5 (floyd))
;=> ((1) (2 3) (4 5 6) (7 8 9 10) (11 12 13 14 15))
This was arrived at based on the observation that you want a series of increasing ranges (the (range) argument to map is used to produce a sequence of increasingly longer ranges), each one starting from almost the triangular number sequence:
(take 5 (reductions + 0 (iterate inc 1)))
;=> (0 1 3 6 10)
If we start that sequence from 1 instead, we get the starting numbers in your desired sequence:
(take 5 (reductions + 1 (iterate inc 1)))
;=> (1 2 4 7 11)
If the + 1 inside the mapped function bothers you, you could do this instead:
(defn floyd []
(map (fn [lo n] (range lo (+ lo n)))
(reductions + 1 (iterate inc 1))
(iterate inc 1)))
it is not possible to solve it with partition / partition-all, since they split your sequence into predefined size chunks.
What you can do, is to employ recursive lazy function for that:
user> (defn floyd []
(letfn [(f [n rng]
(cons (take n rng)
(lazy-seq (f (inc n) (drop n rng)))))]
(f 1 (iterate inc 1))))
#'user/floyd
user> (take 1 (floyd))
;;=> ((1))
user> (take 2 (floyd))
;;=> ((1) (2 3))
user> (take 3 (floyd))
;;=> ((1) (2 3) (4 5 6))
user> (take 4 (floyd))
;;=> ((1) (2 3) (4 5 6) (7 8 9 10))
another variant can use similar approach, but only track chunk-start/chunk-size:
user> (defn floyd []
(letfn [(f [n start]
(cons (range start (+ start n))
(lazy-seq (f (inc n) (+ start n)))))]
(f 1 1)))
another approach is to use clojure's collection operating functions:
user> (defn floyd-2 []
(->> [1 1]
(iterate (fn [[start n]]
[(+ n start) (inc n)]))
(map (fn [[start n]] (range start (+ start n))))))
#'user/floyd-2
user> (take 4 (floyd-2))
;;=> ((1) (2 3) (4 5 6) (7 8 9 10))
user> (take 5 (floyd-2))
;;=> ((1) (2 3) (4 5 6) (7 8 9 10) (11 12 13 14 15))
user> (take 1 (floyd-2))
;;=> ((1))
How about this:
(defn floyd []
(map (fn[n]
(let [start (/ (* n (inc n)) 2)]
(range (inc start) (+ start n 2))))
(iterate inc 0)))
(take 4 (floyd))
How can I transform '((1 2 3) (4 5 6)) into '(1 2 3) '(4 5 6)?
Concretely, if I have the following collection:
(def coll '((1 2 3) (4 5 6))
And I want to pass both '(1 2 3) and '(4 5 6) as arguments to a method, how can coll be transformed into two lists?
For example, in
(map list ?? coll)
what implementation of ?? would lead to
'((1 4) (2 5) (3 6))
Which is the result of (map list '(1 2 3) '(4 5 6)).
What I tried is:
(flatten coll) does not work since it leads to one list (1 2 3 4 5 6).
Unquote splicing as in ~#coll leads to
Attempting to call unbound fn: #'clojure.core/unquote-splicing.
And mapping with quote as in (map #(quote %) coll) leads to something like (p1__10109# p1__10109#).
The operation is often called transpose. In clojure, you can do it thus:
(apply map list coll) ;((1 4) (2 5) (3 6))
For demonstration purpose,
I have list with has a list:
> (setf x (list '(1 2 1) '(4 5 4)))
((1 2 1) (4 5 4))
> (length x)
2
I want to add a new list '(2 3 2) to it. The append function:
> (append '(2 3 2) x)
(2 3 2 (1 2 1) (4 5 4))
> (length (append '(2 3 2) x))
5
isn't really doing what I want.
What I want is to add '(2 3 2) like this:
((8 7 8) (1 2 1) (4 5 4))
so that the length is 3.
So far, I haven't seen any example or ways to do what I want. Is there a built-in function or effective way of doing this ?
APPEND is not a destructive function, which is what you are asking for. What APPEND does is allocate a new list, which it then returns.
What you can do to achieve your goals is:
(setf x (append '((...)) x)) ;;appends the quoted list to x
There is also the function NCONC, which adjusts pointers destructively.
For your meditations, I present example work:
CL-USER> (defparameter *x* nil)
*X*
CL-USER> (setf *x* '((1 2 3) (4 5 6)))
((1 2 3) (4 5 6))
CL-USER> (append *x* '(10 11 12))
((1 2 3) (4 5 6) 10 11 12)
CL-USER> (append *x* '((10 11 12)))
((1 2 3) (4 5 6) (10 11 12))
CL-USER> (setf *x* (append *x* '((10 11 12))))
((1 2 3) (4 5 6) (10 11 12))
CL-USER> *x*
((1 2 3) (4 5 6) (10 11 12))
CL-USER>
APPEND appends lists. If you have a list of two sublists ((1 2 1) (4 5 4)) and you want to append another list of one sublist ((2 3 2)) in front of it.
CL-USER 99 > (append '((2 3 2)) '((1 2 1) (4 5 4)))
((2 3 2) (1 2 1) (4 5 4))
or use this, if you want to add one item in front of the list:
CL-USER 98 > (cons '(2 3 2) '((1 2 1) (4 5 4)))
((2 3 2) (1 2 1) (4 5 4))
I completed exercise 43 on 4clojure the other day and checked some of the other solutions. One in particular has confused me.
The challenge asks you to write a function which satisfies all of these:
(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6)))
(= (__ (range 9) 3) '((0 3 6) (1 4 7) (2 5 8)))
(= (__ (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9)))
My solution was this:
(fn [l n]
(map #(map second %) (vals (group-by #(mod (first %) n)
(map vector (iterate inc 0) l)))))
User himself had this solution:
#(apply map list (partition %2 %1))
and I couldn't work out how it worked.
Let's work through the first problem:
(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6)))
well the (#(partition %2 %1) [1 2 3 4 5 6] 2) would give us ((1 2) (3 4) (5 6)) now how does apply map list on that produce (1 3 5) (2 4 6)
apply is using the ((1 2) (3 4) (5 6)) as a variable length list of additional arguments. Then iteration of the map is applying the list function to all three of these additional lists.
As a result it expands as follows:
(apply map list '((1 2) (3 4) (5 6)))
=> (map list '(1 2) '(3 4) '(5 6))
=> (list 1 3 5) and (list 2 4 6)
In Clojure, I want to combine two lists to give a list of pairs,
> (zip '(1 2 3) '(4 5 6))
((1 4) (2 5) (3 6))
In Haskell or Ruby the function is called zip. Implementing it is not difficult, but I wanted to make sure I wasn't missing a function in Core or Contrib.
There is a zip namespace in Core, but it is described as providing access to the Zipper functional technique, which does not appear to be what I am after.
Is there an equivalent function for combining 2 or more lists, in this way, in Core?
If there is not, is it because there is an idiomatic approach that renders the function unneeded?
(map vector '(1 2 3) '(4 5 6))
does what you want:
=> ([1 4] [2 5] [3 6])
Haskell needs a collection of zipWith (zipWith3, zipWith4, ...) functions, because they all need to be of a specific type; in particular, the number of input lists they accept needs to be fixed. (The zip, zip2, zip3, ... family can be regarded as a specialisation of the zipWith family for the common use case of tupling).
In contrast, Clojure and other Lisps have good support for variable arity functions; map is one of them and can be used for "tupling" in a manner similar to Haskell's
zipWith (\x y -> (x, y))
The idiomatic way to build a "tuple" in Clojure is to construct a short vector, as displayed above.
(Just for completeness, note that Haskell with some basic extensions does allow variable arity functions; using them requires a good understanding of the language, though, and the vanilla Haskell 98 probably doesn't support them at all, thus fixed arity functions are preferrable for the standard library.)
(partition 2 (interleave '(1 2 3) '(4 5 6)))
=> ((1 4) (2 5) (3 6))
or more generally
(defn zip [& colls]
(partition (count colls) (apply interleave colls)))
(zip '( 1 2 3) '(4 5 6)) ;=> ((1 4) (2 5) (3 6))
(zip '( 1 2 3) '(4 5 6) '(2 4 8)) ;=> ((1 4 2) (2 5 4) (3 6 8))
(map vector [1 2 3] [4 5 6])
to give you exactly what you wanted, mapping list across the two lists will give you a list of lists like in your example. I think that many Clojurians would tend to use vectors for this though it will work with anything. and the inputs do not need to be the same type. map creates seqs from them and then maps the seqs so any seq'able input will work fine.
(map list '(1 2 3) '(4 5 6))
(map list [1 2 3] '(4 5 6))
(map hash-map '(1 2 3) '(4 5 6))
(map hash-set '(1 2 3) '(4 5 6))
The built-in way would simply be the function 'interleave':
(interleave [1 2 3 4] [5 6 7 8]) => [1 5 2 6 3 7 4 8]
There is a function called zipmap, that may have the similar effect,
(zipmap (1 2 3)(4 5 6))
The ouput is as fllows:
{3 6, 2 5, 1 4}
#(apply map list %) transposes a matrix just like the Python zip* function. As a macro definition:
user=> (defmacro py-zip [lst] `(apply map list ~lst))
#'user/py-zip
user=> (py-zip '((1 2 3 4) (9 9 9 9) (5 6 7 8)))
((1 9 5) (2 9 6) (3 9 7) (4 9 8))
user=> (py-zip '((1 9 5) (2 9 6) (3 9 7) (4 9 8)))
((1 2 3 4) (9 9 9 9) (5 6 7 8))