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

Pages: 1-

Why this no work?

Name: Anonymous 2011-04-21 11:41

;___;



(defmacro get-pos (board x y)
  `(nth ,y (nth ,x ,board)))

(defun cell-times (lst &optional (acc 0))
  (if lst
      (cond ((eq (car lst) nil)
         (cell-times (cdr lst) acc))
        ((numberp (car lst))
         (cell-times (cdr lst) acc))
        (t (cell-times (cdr lst) (1+ acc))))
      acc))
 
(defun get-if-possible (board x y) ;X och Y felet ligger här!?
  (if (not
       (and (>= x (length board))
        (>= y (length (nth 0 board)))))
      (if
       (not (< x 0))
       (if (not (< y 0))
       (get-pos board x y)))))

(defun get-neighbours (board x y)
  (let ((output '() ))
    (push (get-if-possible board (1+ x) y) output)
    (push (get-if-possible board (1- x) y) output)

    (push (get-if-possible board x (1+ y)) output)
    (push (get-if-possible board x (1- y)) output)

    (push (get-if-possible board (1+ x) (1+ y)) output)
    (push (get-if-possible board (1- x) (1- y)) output)

    (push (get-if-possible board (1+ x) (1- y)) output)
    (push (get-if-possible board (1- x) (1+ y)) output)
    output))

(defun next-gen (board) ;Den ändrar board, men hur ><?!
  (let ((output board))
  (loop for x-num from 0 to (1- (length output))
     for x in output do
       (loop for y in x
        for y-num from 0 to (1- (length x)) do
        (let ((neigh-val (cell-times (get-neighbours output x-num y-num))))
          (cond
        ((< neigh-val 2) (setf (get-pos output x-num y-num) neigh-val))
        ((= neigh-val 3) (setf (get-pos output x-num y-num) 'x))
        ((> neigh-val 3) (setf (get-pos output x-num y-num) neigh-val))))))
  output))

Name: Anonymous 2011-04-21 11:46

(defun get-if-possible (board x y) ;X och Y felet ligger här!?

här här här u dumb ligger

Name: Anonymous 2011-04-21 11:56

You should convert tabs to spaces before posting here.

Leaving that aside, what exactly are you trying to accomplish? (I have yet to fully read your code, but it does appear valid, at least syntactically and as far as the compiler is concerned).

Name: Anonymous 2011-04-21 12:01

>>2
Nope, forgot removing the comment.

>>3
It's Conway's game of life (a very cluttered version of it), or at least it is supposed to be.
The next-gen function is the most interesting one I guess as I get weird output which doesn't fit into the sims I've tested out on the web.

Name: Anonymous 2011-04-21 12:11

>>4
What is the input format supposed to look like?
You're setting numbers to the "array" (you should use an array instead of a list, but whatever):

CL-USER> (let ((gen '((O O O) (O X O) (O O O)))) (loop repeat 10 do (print (setf gen (next-gen gen)))))

((X 5 O) (4 6 X) (1 O O))
((0 5 1) (1 X X) (1 X X))
((1 5 1) (1 X X) (1 X X))
...


The problem here is that:

        ((< neigh-val 2) (setf (get-pos output x-num y-num) [i]neigh-val[/i]))
        ((= neigh-val 3) (setf (get-pos output x-num y-num) 'x))
        ((> neigh-val 3) (setf (get-pos output x-num y-num) [i]neigh-val[/i]))))))

those 2 are numeric, while the rest are supposed to be symbolic?

Name: Anonymous 2011-04-21 12:16

You probably don't want to be updating the board in-place. Make a new board for output, and use the input board to count neighbours.

Name: Anonymous 2011-04-21 12:17

>>5
Basically, a board looks like this:

'( (0 0 0 0 0)
   (0 0 0 0 0))
If a cell is alive I replace it with the symbol X.
And yes, I should use arrays... But for some reason I decided not to.

Name: Anonymous 2011-04-21 13:15


(defun next-gen (board)
  (let ((output (copy-list board)))
  (loop for x-num from 0 to (1- (length board))
     for x in board do
       (loop for y in x
            for y-num from 0 to (1- (length x)) do
            (let ((neigh-val (cell-times (get-neighbours board x-num y-num))))
              (cond
                ((< neigh-val 2) (setf (get-pos output x-num y-num) neigh-val))
                ((= neigh-val 3) (setf (get-pos output x-num y-num) 'x))
                ((> neigh-val 3) (setf (get-pos output x-num y-num) neigh-val))))))
  output))


How come that this manipulates not only the output but also the board variable that I feed the function with?

Name: Anonymous 2011-04-21 13:28

>>8
copy-list probably isn't doing a deep copy. Try (map copy-list board) or equivalent.

Name: Anonymous 2011-04-21 13:34

>>9
Yes! That worked, nice (I used mapcar).
Though now I'm kind of scared as I have no idea what serves as a "deep copy" and just a reference to another object.

Name: Anonymous 2011-04-21 14:04

lol I was doing the same thing yesterday because I saw some interesting thread on Reddit. May I ask if your motivation is the same as mine?

btw, here's the code in HASKAL

import Data.Array

neighbs :: (Int, Int) -> Array (Int, Int) Bool -> Int
neighbs (i,j) arr
  | (iOK i) && (jOK j)  = count i j 1 1 1 1
  | i==mMax && (jOK j)  = count i j 1 0 1 1
  | i==mMin && (jOK j)  = count i j 0 1 1 1
  | (iOK i) && j==nMax  = count i j 1 1 1 0
  | (iOK i) && j==nMin  = count i j 1 1 0 1
  | (i,j)==(mMin, nMin) = count i j 0 1 0 1
  | (i,j)==(mMin, nMax) = count i j 0 1 1 0
  | (i,j)==(mMax, nMin) = count i j 1 0 0 1
  | (i,j)==(mMax, nMax) = count i j 1 0 1 0
  | otherwise           = 0
  where  ((mMin, nMin), (mMax, nMax)) = bounds arr
         iOK i = (i > mMin) && (i < mMax)
     jOK j = (j > nMin) && (j < nMax)
     count i j di ui dj uj = length.filter id $ [arr!(k,l) | k<-[i-di..i+ui], l<-[j-dj..j+uj], (k,l)/=(i,j)]

updateGrid :: Array (Int, Int) Bool -> Array (Int, Int) Bool
updateGrid arr = arr // [((i,j), updateCell i j) | i<-[mMin..mMax], j<-[nMin..nMax]]
  where ((mMin, nMin), (mMax, nMax)) = bounds arr
        updateCell i j
      | arr!(i,j) && (aliveN < 2)        = False
      | arr!(i,j) && (aliveN > 3)        = False
      | arr!(i,j)                        = True
      | (not$arr!(i,j)) && (aliveN == 3) = True
      | otherwise                        = False
      where
        aliveN = neighbs (i,j) arr

grid2String :: Array (Int, Int) Bool -> String
grid2String arr = strGrid mMin nMin arr
  where ((mMin, nMin), (mMax, nMax)) = bounds arr
        strGrid i j arr
      | j>nMax    = []
      | i>mMax    = '\n':strGrid mMin (j+1) arr
      | otherwise = showCell (arr!(i,j)) : (strGrid (i+1) j arr)
    showCell b = if b then '*' else ' '

readString :: String -> [(Int, Bool)]
readString s = zip [0,1..] (map readCell s)
  where readCell ' ' = False
        readCell '*' = True

readLinesGrid :: [String] -> [((Int, Int), Bool)]
readLinesGrid ss = concat $ map (\(j, xs) ->
                          map (\(i, b) -> ((i,j),b)) xs) $ zip [0,1..] (map readString ss)

readGrid :: String -> Array (Int, Int) Bool
readGrid str =
  let gridList = readLinesGrid $ lines str
      minBound = (0,0)
      (maxBound, _) = last gridList
  in  array (minBound, maxBound) gridList

nCycles :: Int -> Array (Int, Int) Bool -> Array (Int, Int) Bool
nCycles n arr = doNCycles 1 arr
  where doNCycles i arr
          | i>n = arr
      | otherwise = updateGrid $ doNCycles (i+1) arr

diff :: Array (Int, Int) Bool -> Array (Int, Int) Bool -> Int
diff arr1 arr2
  | mMin==mMin2 && nMin==nMin2 = sum [if (arr1!(k,l)) == (arr2!(k,l)) then 0 else 1 | k<-[mMin..mMax], l<-[nMin..nMax]]
  | otherwise                  = undefined
  where ((mMin, nMin), (mMax, nMax))     = bounds arr1
        ((mMin2, nMin2), (mMax2, nMax2)) = bounds arr2


has some utility shit to read the initial board from text file too

Name: >>11 2011-04-21 14:08

stupid [code] tags fucked my indentation

Name: Anonymous 2011-04-21 14:29

>>12

Stop saying they're stupid.

Name: Anonymous 2011-04-21 14:31

>>13
okay

Name: Anonymous 2011-04-21 16:55

I dont even f'ing know, but, you could
give me a link to pic: "Have you read your SICP today?"-with-the-crazy-trouser-snake-guy !

Name: Anonymous 2011-04-21 18:31

>>11
I find that to be completely unreadable.
I have some more functions to print the board and stuff like that, it all clocks in at about 83 LoC.
I also do not browse Reddit.

Name: Anonymous 2011-04-22 5:16

>>16
I also do not browse Reddit.
None of us does at the moment, man. You're not alone. Together we will survive.

Name: Anonymous 2011-04-22 5:22

My kitty cat is CRAZY!!! :D

Name: Anonymous 2011-04-22 5:24

"Smokin' POT? Ya damn KIDS!" ... :D

Name: Anonymous 2011-04-22 5:25

Playin ur rock music and smokin ur marijuana!

Name: Anonymous 2011-04-22 5:28

:D :D :D
LOL

Name: Anonymous 2011-04-22 5:57

>>18-21

Who the fuck are you and why are you here?

Name: Anonymous 2011-04-22 6:52

gaido no suneiku

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