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

Pages: 1-4041-

LISP: A dynamic car

Name: Anonymous 2012-01-30 20:27

I have a complex list of lists and want something to check the second element of every list of the list in the loop, any supported features like this?

Name: Anonymous 2012-01-30 20:31

Stop acting like a little girl and switch over to a real programming language like Haskell.

Name: Anonymous 2012-01-30 20:35


CL-USER> (mapcar #'second '((a b c) (1 2 3) (9 8 7)))
(B 2 8)


You'll have to specify what you meant by "check" better, if you wanted something fancier, you could compose that second (or cadr) with your "checking" function.

Some other examples:
(loop for list in lists when (your-check (second list)) do (stuff ...))
or
(find-if #'(lambda (x) (eq x 2)) '((a b c) (1 2 9) (2 3 4)) :key #'second)

Many other functions allow transforming the elements in a list with key with :key. There's obviously dozens other ways of doing this and I just mentioned the ones that first came to mind - if you specified your problem better I might have given a more complete solution.

Name: Anonymous 2012-01-30 20:37

>>2
I would by my AI professor is a LISP enthusiast and demands LISP AND THAT THAT LISP BE COMMON because apparently all LISP's are not created equal

Name: Anonymous 2012-01-30 20:39

>>2,4
Nothing wrong with a proper language like CL.

Name: Anonymous 2012-01-30 20:40

>>3
basically we have the following
(arch (parts lintel post1 post2)
    (lintel must-be-supported-by post1)
    (lintel must-be-supported-by post2)
    (lintel a-kind-of wedge)
    (post1 a-kind-of brick)
    (post1 must-not-touch post2)
    (post2 must-not-touch post1))

and must break it apart, personally I want to just use a buzzword checker and append them to the associated buzzword list

sorry if I'm not clear

Name: Anonymous 2012-01-30 20:42

>>6
oh I guess you can take out arch as I use that to store the lists

Name: Anonymous 2012-01-30 20:48

>>6
It seems like you want to be doing something fancier here than just looking at each second element in the list. Some sort of pattern matching or unification?

Name: Anonymous 2012-01-30 20:54

>>8
Please shut up and let the smart people talk.

Name: Anonymous 2012-01-30 20:56

>>8
I just want to see the second one then determine place the word before and after it under a list of that "word"

like list a-kind-of would have (lintel wedge) (post1 brick) and i need to evaluate the second member of the list to determine where i put them though

I hope i'm making sense, kind of brain dead right now

Name: Anonymous 2012-01-30 20:57

>>9
Please shut up and let the smart people talk.
let the smart people talk.
smart people
implying such things exist on /prog/

Name: Anonymous 2012-01-30 21:02

>>11
Well, you clearly don't know what you're talking about when it comes to programming.

Name: Anonymous 2012-01-30 21:04

>>12
i'm not >>8 but fair point

Name: Anonymous 2012-01-30 21:18

>>10
you could you could con an element into another list and remove it from large list and check each word you remove

Name: >>3 2012-01-30 21:31

>>9,12
If you have any actual complaints about the code, feel free to say them, but your posts are void of actual content. You're probably that kodak guy who just randomly insults people here to feel good about himself.
>>10
Sounds simple enough, here's my solution:


(defun get-arguments-of (relation relation-list) 
  (remove-if-not #'(lambda (x)
                     (destructuring-bind (first a-relation . rest) x
                       (when (eq relation a-relation)
                         (cons first rest))))
                 relation-list))


Testing:
CL-USER>(get-arguments-of 'a-kind-of '((parts lintel post1 post2)
                 (lintel must-be-supported-by post1)
                 (lintel must-be-supported-by post2)
                 (lintel a-kind-of wedge)
                 (post1 a-kind-of brick)
                 (post1 must-not-touch post2)
                 (post2 must-not-touch post1)))
((LINTEL A-KIND-OF WEDGE) (POST1 A-KIND-OF BRICK))



If you just wanted to switch the first and the second for the entire list to make processing easier, all you'd have to do is:

(defun lispify-relations (relation-list)
  (mapcar #'(lambda (x)
          (destructuring-bind (first relation . rest) x
        `(,first ,relation ,@rest)))
      relation-list))


Test:

(lispify-relations '((parts lintel post1 post2)
                 (lintel must-be-supported-by post1)
                 (lintel must-be-supported-by post2)
                 (lintel a-kind-of wedge)
                 (post1 a-kind-of brick)
                 (post1 must-not-touch post2)
                 (post2 must-not-touch post1)))


What about building a list with all the answers? I'd rather use a hashtable for that as it'd be more efficient than an alist, do let me know if you want me to write that example, it'd be trivial.

Name: >>3 2012-01-30 21:33

Oops, wrong paste:

(defun lispify-relations (relation-list)
  (mapcar #'(lambda (x)
          (destructuring-bind (first relation . rest) x
        `(,relation ,first ,@rest)))
      relation-list))

CL-USER> (lispify-relations '((parts lintel post1 post2)
                 (lintel must-be-supported-by post1)
                 (lintel must-be-supported-by post2)
                 (lintel a-kind-of wedge)
                 (post1 a-kind-of brick)
                 (post1 must-not-touch post2)
                 (post2 must-not-touch post1)))
((LINTEL PARTS POST1 POST2) (MUST-BE-SUPPORTED-BY LINTEL POST1)
 (MUST-BE-SUPPORTED-BY LINTEL POST2) (A-KIND-OF LINTEL WEDGE)
 (A-KIND-OF POST1 BRICK) (MUST-NOT-TOUCH POST1 POST2)
 (MUST-NOT-TOUCH POST2 POST1))

Name: Anonymous 2012-01-30 22:29

>>16
>>15
thanks but I don't know lisp well enough to use that

this is the code I came up with


(defun type-lint(pass &optional buzzword)
    (let ( (temp ()))
      (con temp (car arch))
      (setq arch (delete (car arch) arch))
      (loop
       (if (null arch) (return))
       (setq temp ())
       (if = (car :(car arch)) `must-be-supported-by
           (setq arch (delete (car arch) arch))
           (con must-be ((car (car arch)) (car temp)))
           (setq arch(delete (car arch)))
           )
       (if = (car :(car arch)) `a-kind-of
           (setq arch (delete (car arch) arch))
           (con a-kind ((car (car arch)) (car temp)))
           (setq arch(delete (car arch)))
           )
      (if = (car :(car arch)) `must-not-touch
           (setq arch (delete (car arch) arch))
           (con must-not ((car (car arch)) (car temp)))
           (setq arch(delete (car arch)))
           )
      
        )
    )
)


it's not general or elegant. relies heavily on knowing the data before hand. and has some lambda's needed errors, but this is about my current knowledge level

Name: Anonymous 2012-01-30 22:31

>>17
oh and in the if loop i just threw in colons to see if it made a difference, it didn't so please ignore them

Name: Anonymous 2012-01-30 22:42

>>18
That's quite imperative (the code style), also what's : and = supposed to do in your code, that part isn't valid Lisp syntax (unless there's a reader macro for : and = is bound to something or is a symbol macro).

What exactly is the function supposed to do, try giving some example inputs and outputs. Is it supposed to do the same as my get-arguments-of?

Name: Anonymous 2012-01-30 22:48

>>19
oh I thought the = operator was implemented so that it could compare strings... as for the colons no idea just heard they force compilation of a code and googleing my error, a mass of paranthesis cause it so was seeing if it would work.

as for what it's suppose to do

it completely disregards the first line of arch, then deletes it, enters the loop, stores the first atom of the list in temp then deletes it, decides which buzzword list it's to be added to then deletes the second word. adds temp and the word after as a list to the respective keyword list then deletes the list and repeats the process until arch is nil

I'm going to sleep now

Name: Anonymous 2012-01-30 22:52

>>20
There's no strings in the samples you gave, only symbols (which have string names, but are not strings themselves). To compare strings there's multiple functions: equal - compares objects in general by their contents, see Hyperspec for details (won't mention it again); equalp - similar to previous, but much weaker, case insensitive. If you want to only compare strings and nothing else, you can use functions: STRING=, STRING/=, STRING<, STRING>, STRING<=, STRING>=, STRING-EQUAL, STRING-NOT-EQUAL, STRING-LESSP, STRING-GREATERP, STRING-NOT-GREATERP, STRING-NOT-LESSP.

Name: Anonymous 2012-01-30 23:07

>>20
I see, if I understood that right, then my function should work just fine for you (just call it like in the example given), although a more efficient version using hashtables would be nicer.


(defun generate-buzzword-hashtable (list)
  (let ((ht (make-hash-table)))
    (loop for (first relation . rest) in list
      do (push (cons first rest) (gethash relation ht) ))
    ht))


Example usage:

CL-USER> (generate-buzzword-hashtable '((parts lintel post1 post2)
                 (lintel must-be-supported-by post1)
                 (lintel must-be-supported-by post2)
                 (lintel a-kind-of wedge)
                 (post1 a-kind-of brick)
                 (post1 must-not-touch post2)
                 (post2 must-not-touch post1)))
#<HASH-TABLE :TEST EQL :COUNT 4 {243D30F9}>
CL-USER> (gethash 'a-kind-of *)
((POST1 BRICK) (LINTEL WEDGE))
T

In this case, you generate the table once, use as many times as you want.

Name: Anonymous 2012-01-30 23:14

Lisp is good.

Name: Anonymous 2012-01-30 23:29

Lisp is shit.

Name: Anonymous 2012-01-31 3:45

If civilization crumbled to the point of leaving me as the only heir to what is left of computing knowledge to pass it on to others so people could rebuild technology from scratch, I would keep all that other shit a strict secret and give them only Lisp. I would give them only enough to start running, and reveal more of it to specifically kill all the bad ideas they would start to reinvent.

Name: Anonymous 2012-01-31 3:50

>>25
And then some idiot will come up with BASIC ;_;

Nobody can avoid the consequences of the Church-Turing Thesis, for better or for worse.

Name: Anonymous 2012-01-31 9:54

>>26
BASIC is better than Lisp since it's easier to read.

Lisp is fucking shit.

Name: VIPPER 2012-01-31 10:13

>>27
Your face if fucking shit.

Name: Anonymous 2012-01-31 10:30

>>27
I find Lisp considerably easier to read than BASIC.
If you only know Chinese, you may find English harder to read and vice versa.

Name: Anonymous 2012-01-31 11:40

Fun fact: If you read Haskell for a couple of days, you'll come to appreciate the readability of Lisp.

Name: Anonymous 2012-01-31 11:47

Funner fact: If you're a male who wears your sister's black leggings underneath some jeans, you'll end up with the all day boner.

Name: Anonymous 2012-01-31 12:00

>>29
I know Lisp better than I know BASIC but I bet you I could read BASIC code immediately.  Lisp is a visual mess and therefore shit.

Name: Anonymous 2012-01-31 13:00

The dynamic vroom vroom car.

Name: Anonymous 2012-01-31 13:55

>>32
I look at Lisp code and I see structure immediately as I read it, I look at BASIC code and I see spaghetti (mostly gotos). Even If I were to assume that I could read "read" BASIC faster, I sure as hell wouldn't get its high-level semantics faster - what the person that wrote the code meant when writing it (actually the same is true for Lisp, if the coder has no idea what he's doing or if he's a newbie, but this doesn't apply to most Lisp code written by those that actually know the language).

Name: Anonymous 2012-01-31 16:58

>>22
thank you a lot, it's simple and I can explain what it does, just curious what the dot does in the for between relation and rest and what the default hashing function is

Name: Anonymous 2012-01-31 20:49

>>35
A dot is used to denote an improper list in most Lisps:
(cons 'a 'b) -> (A . B)

In this case, it binds the first 2 elements of the list to first and relation, then it binds the cdr (rest[of the list]) to rest. When a list is used within the place of some variable binding in LOOP, what actually happens is that a destructuring-bind is used instead to bind multiple variables, thus this lets you loop over lists of lists and do a destructuring-bind of the elements of each inner list. After doing that it just pushes each relation into the hashtable... push is basically a modify macro for cons - it sets the generic place that was indicated (some place in the hashtable), with the value it had before and a new value cons'd on top of it. Like:
(push place value) is like writing (setf place (cons value place)), where place is a generic place and all the expansion and necessary fluff is handled transparently for you.

If you didn't understand my explanation, just look up what you don't understand in the Hyperspec (HTML/hypertext version of the ANSI CL standard), it's pretty comprehensive (although, you shouldn't really learn Lisp using only it(it'll be too much effort), like most people shouldn't learn some new language by only reading the reference).

Name: Anonymous 2012-01-31 22:10

>>36
thank you for all your help good sir

Name: Anonymous 2012-02-01 14:01

this thread has 10 replies

Name: Anonymous 2012-02-01 16:01

>>34
I look at Lisp code and I puke at the monstrosity that is its disgusting surface syntax.

Name: Anonymous 2012-02-02 21:37

the significance of the number 40 is very under appreciated

Name: luke 2012-02-02 22:01

>>40

5 x 8

five could be taken to represent man (3) and his anima (2) (/shadow) [or a woman(2) and her animus (3)?]

eight as infinity...

the neverending battle of good vs evil? = 40?? =D

Name: Anonymous 2012-02-02 22:24

>>41
cretin

Name: Lua 2012-02-03 8:51

Why don't you use Lua as your go to scripting language?

Name: Anonymous 2012-02-13 20:44

the dynamic car keeps on truckin next stop, dub central

also can anyone here explain to me what defsetf does and the proper way to do it. I'm reading an Othello program and i see

(defun bref (board square) (aref board square))
  (defsetf bref (board square) (val)
  `(setf (aref ,board ,square) ,val))


and no matter how long i look at this or examine the function that's calling it i can't make out it's intended purpose

also the link to the othello program if you want to see the calls or whatever
http://norvig.com/paip/othello.lisp

Name: Anonymous 2012-02-13 21:39

>>44
It tells setf how to modify the value of a call to bref, for example (setf (bref x y) z). It can't just call bref and modify that, because it will return something by value instead of by reference.

Name: Anonymous 2012-02-13 21:46

>>45
i still don't understand, sorry

Name: Anonymous 2012-02-13 22:19

>>46
When you write (setf (car x) y), this is what it doesn't do:

(let ((z (car x)))
  (setf z y))


z just contains the value of (car x), not a pointer to it. So (setf z y) wouldn't actually modify the cons cell, and the code wouldn't work.

Instead, when setf sees the call to car, it passes the cons cell to rplaca, which modifies the car.

defsetf tells setf which function you want it to call when you have (setf (bref x y) z). In the code you posted, that will expand to (setf (aref x y) z), which well then expand to whatever code setf was told to use to set an aref form.

Name: Anonymous 2012-02-13 22:24

>>47
alright i have a general idea, now time to look up aref

thank you

Name: Anonymous 2012-02-13 22:27

>>48
okay aref is simple enough just an array access element

again thanks wouldn't have figured it out on my own for awhile

Name: Anonymous 2012-02-13 22:39

>>48-49
If you're still not completely sure about how it works, chapter 12 of On Lisp (http://www.paulgraham.com/onlisptext.html) talks about setf, and section 12.5 is about defsetf.

Name: Anonymous 2012-02-19 14:39

can someone aid me here?
illegal syntax by setq x (read)


(defun get-move ()
(setq counter (+ counter 1))
 (let* ((x 9) )
(when (eql (mod counter 2) 0)
 (setq x (check-for-win))
 (if null x
  (setq x (check-for-loss))
    (if null x
      (setq x (random-get))
    )
 )
 (setf (aref board-values x) 2)
 (setf (aref board-symbols x) 'O)
)

(when (eql (mod counter 2) 1)
(print "Enter a number between 1 and 9: ")
(loop until (and (>= x 0) (<= 8 x) (valid-move x)
;;;;ERROR HERE;;;;;;;;;;;
   (setq x (read))
   (setq x (- x 1))
  )
 (setf (aref board-values x) 1)
 (setf (aref board-symbols x) 'X)
))

Name: Anonymous 2012-02-19 14:42

playing with a full deck get

Name: Anonymous 2012-02-19 14:56

>>51
Your parentheses are imbalanced, and based on your indentation, I don't think your ifs are doing what you want them to. First of all, null x needs to be inside parentheses. Also, in the first if expression, the inner if is part of the else block - it will be executed if x is non-null.

Here are some other suggestions. They're mostly style things, so they're not causing any problems, but they'll make your code look nicer.

(setq counter (+ counter 1)) -> (incf counter)
let* is unnecessary in this case - use let.
(if (null x) ...) -> (unless x ...) if there's no else
(equal (mod counter 2) 0) -> (evenp counter) - same for 1, oddp

As for the problem in the loop, close the parenthesis around and, and then you need the loop keyword do.

Name: Anonymous 2012-02-19 15:00

>>53
I feel retarded for getting do, time to sudoku

otherwise, thank you for the tips, didn't even know about evenp and oddp

Name: Anonymous 2012-02-19 18:54

>>55
look at this faggots dubs

Name: Anonymous 2012-02-19 20:55

I feel like I'm abusing when also

http://pastebin.com/3hGHGyGT
 This unholy mass is not functioning correctly

ideally i wanted to think of it as if 1 2 3  = 1 then return T for win/placement, but it doesn't seem to want to work

the players number is 1
the computers number is 2


(defun checkwin (board &optional player)
 (print "in check win")
(when (null player)
  (setq player 2)
 )
 (block nil
(when (eql (aref board 0) player)
 (when (and (eql (aref board 0) (aref board 1)) (eql (aref board 1) (aref board 2))) (return T))
 (when (and (eql (aref board 0) (aref board 4)) (eql (aref board 1) (aref board 8))) (return T))
 (when (and (eql (aref board 0) (aref board 3)) (eql (aref board 1) (aref board 6))) (return T))
)
(when (eql (aref board 1) player)
 (when (and (eql (aref board 1) (aref board 5)) (eql (aref board 7) (aref board 5)))  (return T))
)
(when (eql (aref board 2) player)
 (when (and (eql (aref board 2) (aref board 4)) (eql (aref board 1) (aref board 3))) (return T))
 (when (and (eql (aref board 2) (aref board 5)) (eql (aref board 1) (aref board 8))) (return T))
)
(when (eql (aref board 3) player)
 (when (and (eql (aref board 3) (aref board 4)) (eql (aref board 1) (aref board 5))) (return T))
)
(when (eql (aref board 6) player)
 (when (and (eql (aref board 6) (aref board 7)) (eql (aref board 1) (aref board 8))) (return T))
))
)


also please critizise coding style

Name: Anonymous 2012-02-19 20:57

>>56
I'm off today, meant to use code tags

I feel like I'm abusing when also

http://pastebin.com/3hGHGyGT
 This unholy mass is not functioning correctly

ideally i wanted to think of it as if 1 2 3  = 1 then return T for win/placement, but it doesn't seem to want to work

the players number is 1
the computers number is 2


(defun checkwin (board &optional player)
 (print "in check win")
(when (null player)
  (setq player 2)
 )
 (block nil
(when (eql (aref board 0) player)
 (when (and (eql (aref board 0) (aref board 1)) (eql (aref board 1) (aref board 2))) (return T))
 (when (and (eql (aref board 0) (aref board 4)) (eql (aref board 1) (aref board 8))) (return T))
 (when (and (eql (aref board 0) (aref board 3)) (eql (aref board 1) (aref board 6))) (return T))
)
(when (eql (aref board 1) player)
 (when (and (eql (aref board 1) (aref board 5)) (eql (aref board 7) (aref board 5)))  (return T))
)
(when (eql (aref board 2) player)
 (when (and (eql (aref board 2) (aref board 4)) (eql (aref board 1) (aref board 3))) (return T))
 (when (and (eql (aref board 2) (aref board 5)) (eql (aref board 1) (aref board 8))) (return T))
)
(when (eql (aref board 3) player)
 (when (and (eql (aref board 3) (aref board 4)) (eql (aref board 1) (aref board 5))) (return T))
)
(when (eql (aref board 6) player)
 (when (and (eql (aref board 6) (aref board 7)) (eql (aref board 1) (aref board 8))) (return T))
))
)

also please critizise coding style

Name: Anonymous 2012-02-19 22:11

>>57
Sorry, I don't feel like debugging it, but I'll make some more style suggestions.

For defining new variables (lines 5-9) you should use defparameter.
This is up to you, but I always use setf for consistency, even when setq will work. They compile to the same code.
For lines 25-29 and 35-59, I would use cond. For example, the first would be (cond ((evenp counter) ...) (t ...)).
Lines 35-44, without the prints, are equivalent to (setq y (or (check-for-win) (check-for-loss) (random-get))). That's assuming that those functions only return either a number or nil.
Lines 52-53 - (setq x (1- (read)))
Line 60 - (when (< -1 x 9) ...) - same for 76
Lines 76-78 - (and (<= 0 x 8) (= (aref board-values x) 0))

I'm done for now, if you post a new version I'll look at it tomorrow.

Name: Anonymous 2012-02-20 1:05

lisp is shit

Name: Anonymous 2012-02-20 8:36

You could do this in D

Name: Anonymous 2012-02-20 10:38

>>58
thanks for that, made your changes and found my problem

Name: Anonymous 2012-03-19 18:05

vroom vroom and we're back because fucking stumped

(defun gen-list (turn boards)
"minimax"
 (let ((bestscore -2) (tempscore -2)) ;keeps track of score
   (print "ALPHA")
   (listp (free-return boards))   ;;debugging statement
   (when (or (eql (free-return boards) nil) (eql turn 9)) ;;error occurs here the second time, expected type list
     (return-from gen-list bestscore)
   )
                                  (print "A")
   (if (evenp turn)
    (setf bestscore -2) ;;forces the computer to pick a move
    (setf bestscore 2))  
                                         (print "B")
(loop for x in (free-return boards) do ;gets all free areas in the form of a list
                                       (print "C")
  (when (evenp turn)                  ;;if computers turn
                                   (print "D")
   (setf (car (nth x boards)) 2)   ;;sets element at position x
                                   (print "E")
(when (checkwin boards)           ;;if there is a victory
                                  (print "F")
 (setf tempscore 1))              ;;set temp to 1
                                  (print "G")
(when (not (eql tempscore 1))
                                   (print "H")
 (if (checkwin boards 1)         ;;if the opponent has the win
  (setf tempscore 0)             ;;set score to 0
  (setf tempscore -1)            ;;else -1
                             
)))                                 (print "I")

(when (oddp turn)      ;;if players turn just perform a move
(print boards ) (terpri) (terpri) ;debug
                                       (print "J")
 (setf (car (nth x boards)) 1)
                                     (print "k")
)
                                     (print "L")
(setf tempscore (gen-list (+ 1 turn)  (free-return boards))); send
                                      (print "M")
 (setf (car (nth x boards)) 0) ;;returns board to passed value so it can try another value
                                      (print "N")
;;if it's the computers turn and a better move has been found update it
(when (and (evenp turn) (> tempscore bestscore))
 (setf bestmove x)
 (setf bestscore tempscore)
))
(return-from gen-list bestmove)
))

(defun free-return (boards)
"returns every available position to move"
(loop for x from 0 to 8 when (eql (car (nth x boards)) 0) collect x))

Name: Anonymous 2012-03-19 18:40

>>60
You could do it in Brainfuck too.

Name: Anonymous 2012-05-01 21:28

somebody give me an effective simple heuristic for evaluating checker position

for now I have a value board where I some up all the places then add 1 if pawn and 2 if king then subtract it from the opponents which is similar

however this is concentrated shit as I've found.

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