Skip to main content
make code clearer to the newcomer to Clojure
Source Link
Thumbnail
  • 626
  • 3
  • 7

(Your lock values are all distinct. We could instead consider the locks distinct.)

We add a nil value to each lock, representing the lock not being included. Using your example, we get ...

input = [A,B,C,nil] [1,2,3,4,5,nil] [G,H,nil]

... to choose from. There are ...

$$4 \times 6 \times 3 = 72 $$

... ways to do this.

In general, from sets $Locks$, there are ...(

$$\prod_{l \in Locks} (\vert l \vert + 1)$$

... ways.


So far, so mathematical. Now let's generate the possibilities. I'll have to use a programming language. My choice is Clojure, a Lisp. We should be able to at least verify the above result.

(def locks '[[A,B,C] [1,2,3,4,5] [G,H]])

(defn combos [ls]
  (let [c (count ls)]
    (if (zeroempty? cls)
      [()]
      (let [[l & ls] ls
            sub-list (combos ls)]
        (concat
          sub-list
          (for [x l, y sub-list] (conj ycons x) y))))))

Now

(count (combos locks))
=> 72

... as expected.

There are better ways to depict the problem space, but this'll surely do.

(Your lock values are all distinct. We could instead consider the locks distinct.)

We add a nil value to each lock, representing the lock not being included. Using your example, we get ...

input = [A,B,C,nil] [1,2,3,4,5,nil] [G,H,nil]

... to choose from. There are ...

$$4 \times 6 \times 3 = 72 $$

... ways to do this.

In general, from sets $Locks$, there are ...(

$$\prod_{l \in Locks} (\vert l \vert + 1)$$

... ways.


So far, so mathematical. Now let's generate the possibilities. I'll have to use a programming language. My choice is Clojure, a Lisp. We should be able to at least verify the above result.

(def locks '[[A,B,C] [1,2,3,4,5] [G,H]])

(defn combos [ls]
  (let [c (count ls)]
    (if (zero? c)
      [()]
      (let [[l & ls] ls
            sub-list (combos ls)]
        (concat
          sub-list
          (for [x l, y sub-list] (conj y x)))))))

Now

(count (combos locks))
=> 72

... as expected.

There are better ways to depict the problem space, but this'll surely do.

(Your lock values are all distinct. We could instead consider the locks distinct.)

We add a nil value to each lock, representing the lock not being included. Using your example, we get ...

input = [A,B,C,nil] [1,2,3,4,5,nil] [G,H,nil]

... to choose from. There are ...

$$4 \times 6 \times 3 = 72 $$

... ways to do this.

In general, from sets $Locks$, there are ...(

$$\prod_{l \in Locks} (\vert l \vert + 1)$$

... ways.


So far, so mathematical. Now let's generate the possibilities. I'll have to use a programming language. My choice is Clojure, a Lisp. We should be able to at least verify the above result.

(def locks '[[A,B,C] [1,2,3,4,5] [G,H]])

(defn combos [ls]
  (if (empty? ls)
    [()]
    (let [[l & ls] ls
          sub-list (combos ls)]
      (concat
        sub-list
        (for [x l, y sub-list] (cons x y))))))

Now

(count (combos locks))
=> 72

... as expected.

There are better ways to depict the problem space, but this'll surely do.

add algorithm
Source Link
Thumbnail
  • 626
  • 3
  • 7

(Your lock values are all distinct. We cancould instead consider the locks distinct.)

We add a nil value to each lock, representing the lock not being included. Using your example, we get ...

input = [A,B,C,nil] [1,2,3,4,5,nil] [G,H,nil]

... to choose from. There are ...

$$4 \times 6 \times 3 = 72 $$

... ways to do this.

In general, from sets $Locks$, there are ...(

$$\prod_{l \in Locks} (\vert l \vert + 1)$$

... ways.


So far, so mathematical. Now let's generate the possibilities. I'll have to use a programming language. My choice is Clojure, a Lisp. We should be able to at least verify the above result.

(def locks '[[A,B,C] [1,2,3,4,5] [G,H]])

(defn combos [ls]
  (let [c (count ls)]
    (if (zero? c)
      [()]
      (let [[l & ls] ls
            sub-list (combos ls)]
        (concat
          sub-list
          (for [x l, y sub-list] (conj y x)))))))

Now

(count (combos locks))
=> 72

... as expected.

There are better ways to depict the problem space, but this'll surely do.

(Your lock values are all distinct. We can instead consider the locks distinct.)

We add a nil value to each lock, representing the lock not being included. Using your example, we get ...

input = [A,B,C,nil] [1,2,3,4,5,nil] [G,H,nil]

... to choose from. There are ...

$$4 \times 6 \times 3 = 72 $$

... ways to do this.

In general, from sets $Locks$, there are ...(

$$\prod_{l \in Locks} (\vert l \vert + 1)$$

... ways.

(Your lock values are all distinct. We could instead consider the locks distinct.)

We add a nil value to each lock, representing the lock not being included. Using your example, we get ...

input = [A,B,C,nil] [1,2,3,4,5,nil] [G,H,nil]

... to choose from. There are ...

$$4 \times 6 \times 3 = 72 $$

... ways to do this.

In general, from sets $Locks$, there are ...(

$$\prod_{l \in Locks} (\vert l \vert + 1)$$

... ways.


So far, so mathematical. Now let's generate the possibilities. I'll have to use a programming language. My choice is Clojure, a Lisp. We should be able to at least verify the above result.

(def locks '[[A,B,C] [1,2,3,4,5] [G,H]])

(defn combos [ls]
  (let [c (count ls)]
    (if (zero? c)
      [()]
      (let [[l & ls] ls
            sub-list (combos ls)]
        (concat
          sub-list
          (for [x l, y sub-list] (conj y x)))))))

Now

(count (combos locks))
=> 72

... as expected.

There are better ways to depict the problem space, but this'll surely do.

Source Link
Thumbnail
  • 626
  • 3
  • 7

(Your lock values are all distinct. We can instead consider the locks distinct.)

We add a nil value to each lock, representing the lock not being included. Using your example, we get ...

input = [A,B,C,nil] [1,2,3,4,5,nil] [G,H,nil]

... to choose from. There are ...

$$4 \times 6 \times 3 = 72 $$

... ways to do this.

In general, from sets $Locks$, there are ...(

$$\prod_{l \in Locks} (\vert l \vert + 1)$$

... ways.