Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Loop all pairs

Name: Anonymous 2012-02-05 12:30

Let's say I have this array:

{a, b, c, d, e}

How can I look through all the possible pairs?

a, b
a, c
a, d
a, e
b, c
b, d
b, e
c, d
c, e
d, e

Fictional language syntax please.

Name: Anonymous 2012-02-06 3:35

All n combinations from a list, in 6 lines of scheme:

(define (combinations n lis output)
  (cond ((= n 0)
         (output '()))
        ((not (null? lis))
         (begin (combinations (- n 1) (cdr lis) (lambda (x) (output (cons (car lis) x))))
                (combinations n (cdr lis) output)))))

(combinations 3 '(a b c d e f g) (lambda (x) (display x) (newline)))



:w !scm
#<unspecified>
(a b c)
(a b d)
(a b e)
(a b f)
(a b g)
(a c d)
(a c e)
(a c f)
(a c g)
(a d e)
(a d f)
(a d g)
(a e f)
(a e g)
(a f g)
(b c d)
(b c e)
(b c f)
(b c g)
(b d e)
(b d f)
(b d g)
(b e f)
(b e g)
(b f g)
(c d e)
(c d f)
(c d g)
(c e f)
(c e g)
(c f g)
(d e f)
(d e g)
(d f g)
(e f g)
#<unspecified>


by the way, did you know that you can pipe the content of a file being editted in vim to a command using:

:w !command

It's rather nifty when writing scripts.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List