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

To find neighbours

Name: Anonymous 2011-04-12 10:28

Lets suppose that we have an array which looks like this:


#(#(O O O O)
  #(O O X O)
  #(O O O O))


How would I go about to find the neighbours of 'X
without using loads of if-else ?

Name: Anonymous 2011-04-12 10:31

Satori: #FAILED

Name: Anonymous 2011-04-12 10:37

Let (i,j) be the position of X in the matrix.

#(#([i-1,j-1] [i-1,j] [i-1,j+1])
  #([i,j-1]   [i,j]   [i,j+1])
  #([i+1,j-1] [i+1,j] [i+1,j+1]))


You still need to check for (i,j) = (0,0)|(MAX,MAX)|(0,_)|(MAX,_)|(_,0)|(_,MAX)

Name: Anonymous 2011-04-12 10:38

>>2
You can't actually fail with Satori, simply be very late in achieving it.


I know I probably shouldn't ask you about this (as you are oh so busy with discussing real programming) but you are one of the few that probably could give me a proper answer about this.

Name: Anonymous 2011-04-12 10:41

>>4
You could define a (get-if-possible a x y) function and use an if inside that.

Name: Anonymous 2011-04-12 10:43

>>3
I had already figured that out, is there any better way to write a function that does this than doing loads of check-ups manually?

Something like:


(defun check-neigh (field x y)
  (let ((output '() ))
    (push (nth (1- y) (nth x field)))
    ...
    output))


just feels so unnecessary and long.

Name: Anonymous 2011-04-12 10:50

>>6
It's not that long, just a
(or ((disjoin zero? (curry = i-max)) i)
    ((disjoin zero? (curry = j-max)) j))

Name: Anonymous 2011-04-12 10:56

>>7
disjoin ?
curry (in this context)?

What in general?

Name: Anonymous 2011-04-12 10:58

Name: Anonymous 2011-04-12 11:00

Oh wait... All I should need to do is flatten the whole list of lists into one list and then run something like a labels in-between (- pos-symbol 4) and (+ pos-symbol 4) and then remove the symbol.
Yeah... That should do it.
Hm, only problem is finding the symbol again, which I could do in a separate function.

This is probably not as fast as the whole PUSH-ONE-LOAD-OF-VALUES function but the code is far more interesting.

Name: Anonymous 2011-04-12 11:01

If anyone has any idea on how to find the symbols new position in the flattened list, it would be much appreciated.
I have a little idea of my own but it's kinda boring.

Name: Anonymous 2011-04-12 11:03

>>8
((disjoin f g) . args) = (or (apply f args) (apply g args))
curry
is curry.

All I should need to do is flatten the whole list of lists into one list
Then you lose the structure of the list.

Name: Anonymous 2011-04-12 11:09

>>11
(2,3) = 2*length+3

It's not so difficult, really. Read SICP.

Name: Anonymous 2011-04-12 11:17

>>12
I wouldn't do it to the actual list, merely a copy of the list, and I don't know what I was thinking about when I said that.

Name: Anonymous 2011-04-12 11:20

>>14
You still lose its structure, and it will work only with a fixed-length list of lists/you'll end writing more boilerplate than before.

Name: Anonymous 2011-04-12 11:48

>>15
Yeah, I realised that, see:
and I don't know what I was thinking about when I said that.

Name: Anonymous 2011-04-12 22:53

>>1
Use bit masks

Name: Anonymous 2011-04-12 22:55

>>17
One also could also run a bit window against data and count matchin bits (modern CPU support bitcount function). A pretty general technique, you can use to pattern match any data.

Name: Anonymous 2011-04-12 22:57

Name: Anonymous 2011-04-12 23:00

>>19
You came from /g/?

Name: Anonymous 2011-04-13 7:04

Should be simple enough, but I suggest you define the input and output more exactly, it's unclear to me the exact format of the output that you want.

Name: Anonymous 2011-04-13 7:56

CHECK MY DUBS <---- ^___^

Name: Anonymous 2011-04-13 8:10

#lang racket
 
  ;; Conway's Game of Life
 
  (define (lives? currently-alive neighbors)
    (if currently-alive
        (case neighbors [(2 3) #t] [else #f])
        (= neighbors 3)))
 
  (define (how-many-living cells)
    (length (filter (lambda (cell) cell) cells)))
 
  (define (my-vector-ref vector n)
    (vector-ref vector (let ([i (vector-length vector)])
                         (cond
                           [(< n 0) (+ i n)]
                           [(= i n) 0]
                           [else n]))))
 
  (define (next-generation game)
    (let ([y (vector-length game)]
          [x (vector-length (vector-ref game 0))])
      (list->vector
       (let loop ([j (- y 1)]
                  [new-game '()])
         (if (< j 0) new-game
             (loop (- j 1)
                   (cons (list->vector
                          (let loop ([i (- x 1)]
                                     [row '()])
                            (if (< i 0) row
                                (loop (- i 1)
                                      (cons (lives?
                                             (vector-ref (vector-ref game j) i)
                                             (how-many-living
                                              `(,(my-vector-ref (my-vector-ref game (- j 1)) (- i 1))
                                                ,(my-vector-ref (my-vector-ref game (- j 1)) i)
                                                ,(my-vector-ref (my-vector-ref game (- j 1)) (+ i 1))
                                                ,(my-vector-ref (my-vector-ref game j) (- i 1))
                                                ,(my-vector-ref (my-vector-ref game j) (+ i 1))
                                                ,(my-vector-ref (my-vector-ref game (+ j 1)) (- i 1))
                                                ,(my-vector-ref (my-vector-ref game (+ j 1)) i)
                                                ,(my-vector-ref (my-vector-ref game (+ j 1)) (+ i 1)))))
                                            row)))))
                         new-game)))))))
 
 
  (define (play game generations)
    (if (<= generations 0)
        (displayln "Fuck you, ``faggot''!!!" (current-error-port))
        (let loop ([g 1]
                   [game (next-generation game)])
          (displayln game)
          (unless (= g generations)
            (loop (+ g 1) (next-generation game))))))
 
  ;;
  ;; Now let's try 'er out:
  ;;
 
  (play #(#(#t #f #t #f #t)
          #(#f #t #f #f #f)
          #(#f #t #f #t #t)
          #(#f #t #t #f #f)
          #(#t #f #f #f #f))
        25)
 
  ;;
  ;; Would output:
  ;;

#(#(#t #f #f #f #t) #(#f #t #f #f #f) #(#f #t #f #t #f) #(#f #t #t #t #t) #(#t #f #t #t #t))
#(#(#f #f #t #f #f) #(#f #t #t #f #t) #(#f #t #f #t #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #t #t #t #f) #(#f #t #f #f #t) #(#f #t #f #t #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#t #t #t #t #f) #(#f #t #f #f #t) #(#f #f #t #t #t) #(#f #f #f #f #f) #(#f #f #t #f #f))
#(#(#t #f #f #t #t) #(#f #f #f #f #f) #(#t #f #t #t #t) #(#f #f #t #f #f) #(#f #f #t #t #f))
#(#(#f #f #t #t #t) #(#f #t #t #f #f) #(#f #t #t #t #t) #(#f #f #f #f #f) #(#f #t #t #f #f))
#(#(#t #f #f #f #f) #(#f #f #f #f #f) #(#t #t #f #t #f) #(#t #f #f #f #f) #(#f #t #t #f #f))
#(#(#f #t #f #f #f) #(#t #t #f #f #t) #(#t #t #f #f #t) #(#t #f #f #f #t) #(#t #t #f #f #f))
#(#(#f #f #t #f #t) #(#f #f #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #f) #(#f #t #f #f #t))
#(#(#f #t #t #f #t) #(#f #f #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #f) #(#t #f #f #t #f))
#(#(#f #t #t #f #t) #(#t #t #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #t) #(#t #t #t #t #t))
#(#(#f #f #f #f #f) #(#f #f #f #f #t) #(#f #t #t #t #f) #(#f #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #f #t #t #f) #(#t #t #t #t #f) #(#f #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #f #f #t #t) #(#t #f #f #t #t) #(#t #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#t #f #f #t #f) #(#f #t #t #t #f) #(#t #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #t #f #t #t) #(#f #f #f #t #f) #(#t #t #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #f #f) #(#f #f #t #t #t) #(#f #t #f #t #f) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #f #t #f) #(#f #f #t #t #t) #(#f #f #f #t #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #t #t #t) #(#f #f #t #f #f) #(#f #f #t #f #t) #(#f #f #f #f #f) #(#f #f #f #f #f))
#(#(#f #f #t #t #f) #(#f #t #t #f #t) #(#f #f #f #t #f) #(#f #f #f #f #f) #(#f #f #f #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
#(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))

  ;;
  ;; Observation: Entropy change survives for 21 generations in the particular test game above.
  ;; The 21st generation's state, namely
  ;; #(#(#f #t #f #f #t) #(#f #t #f #f #t) #(#f #f #t #t #f) #(#f #f #f #f #f) #(#f #f #t #t #f))
  ;; does not change.
  ;;

Name: 23 2011-04-13 8:30

>>1-san, see my-vector-ref (and the rest of the code) for your answer.

Note about this part:

  (how-many-living
    `(,(my-vector-ref (my-vector-ref game (- j 1)) (- i 1))
      ,(my-vector-ref (my-vector-ref game (- j 1)) i)
      ,(my-vector-ref (my-vector-ref game (- j 1)) (+ i 1))
      ,(my-vector-ref (my-vector-ref game j) (- i 1))
      ,(my-vector-ref (my-vector-ref game j) (+ i 1))
      ,(my-vector-ref (my-vector-ref game (+ j 1)) (- i 1))
      ,(my-vector-ref (my-vector-ref game (+ j 1)) i)
      ,(my-vector-ref (my-vector-ref game (+ j 1)) (+ i 1))))


I could have written it iteratively (and that would work in my game in particular), but I kept it expanded to demonstrate the idea the way you need it because otherwise it probably wouldn't work for you.

In other words, I could have made it iterate through a 2-dimensional array from (-1,-1) to (+1,+1) and add the numbers to i and j in a single my-vector-ref expression, except return a #f when both numbers equal 0 (the current cell). (And that would work in my game because an additional #f in the list fed to how-many-living will not change its output.)

Name: Anonymous 2011-04-13 8:31

Forgot to link http://en.wikipedia.org/wiki/Conway's_Game_of_Life

Name: >>23 2011-04-13 8:35

By the way, sorry for using Scheme when you're obviously using Common Lisp. I'm not really fond of the latter (yet).

Name: Anonymous 2011-04-13 9:55

>>23
Yeah, I skimmed through the code, I did my "implementation" in the same raw way that you did. Conway's Game of Life is actually what I'm implementing right now so how nice of you to show me your source!

I have one single question:
What the hell is up with the whole [game (next-generation game)] ?
I'm talking about [ and the other one, what are they doing there?

Name: Anonymous 2011-04-13 10:52

>>27
That's just scheme's alternate syntax, unless you meant something else?

Name: Anonymous 2011-04-13 18:18

>>27
In Scheme, parenthesis and rectangular brackets are exchangeable. Racketeers in particular prefer to use the latter in certain positions to enhance readability. Particularly in expressions like let where you would otherwise end up with a salad of parenthesis when nesting them.

Name: Anonymous 2011-04-13 21:44

>>29
In CL I like to use [a b c] for (list a b c) and {a b c} for (progn a b c), but Scheme doesn't allow me. Stupid Scheme.

Name: Anonymous 2011-04-13 21:49

I also use [a b -> c d -> e f] for hashes in CL, but Scheme doesn't have macros, so it couldn't discern list from hash declaration at compile time.

Name: Anonymous 2011-04-13 21:59

>>30
In Scheme we use '(a b c) for (list a b c). But what is progn?

Name: Anonymous 2011-04-13 22:02

>>32
a b c wont be evaluated.

Name: >>32 2011-04-13 22:03

Ah, disregard that. In Scheme, it's begin.

Name: Anonymous 2011-04-13 22:04

>>32
But what is progn?
Scheme forbids side effects and don't have evaluation order? A toy language, indeed.

Name: Anonymous 2011-04-13 22:05

>>34
Pascal wannabe.

Name: Anonymous 2011-04-13 22:11

>>35
Scheme does not restrict side effects, rather functional programmers avoid promiscuous unnecessary use of them.

Name: Anonymous 2011-04-13 22:38

>>37
define unnecessary

Name: Anonymous 2011-04-13 22:42

A man, who was struck by lightning, treated by ECT and executed by electric chair, may say that electricity is unnecessary.

Name: Anonymous 2011-04-13 23:14

>>38
#ifndef UNNECESSARY
#define UNNECESSARY "side_effects"
#endif

Name: Anonymous 2011-04-13 23:49

>>40
#ifndef UNNECESSARY
#define UNNECESSARY "TERRIBLE! UNNECESSARY!"
#endif

Name: Anonymous 2011-04-14 0:39

>>40-41
#ifdef UNNECESSARY
#undef UNNECESSARY
#endif


You guys suck at Zen.

Name: Anonymous 2011-04-14 6:40

>>42
I didn't want to waste a wish on a blindfold or towel.

Name: Anonymous 2012-03-14 20:00

will this work?

Name: Anonymous 2012-03-14 20:41

>>40
Your C overlords have entered the thread.

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