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

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

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