How to use kartkar here?

(defun find-attr (node attr) (let ((children (pt-children node))) (if (null children) nil (let ((subchildren (mapcar ############## (get-value-if-attrib-present (node attrib) ...) 

pt is the class. (pt-children node) gives the children of the node , which are also pt objects. attr is a string. Suppose I write get-value-if-attrib-present to return the value of the pt object if it has an attr mapping, how can I get a list of all the values โ€‹โ€‹of the children of the node with matching attr here (in #### .. ..)?

+1
source share
2 answers

For Common Lisp, use one of the following functions:

  • REMOVE-IF
  • REMOVE-IF-NOT
  • REMOVE

They scan the list and delete items. Save the ones you want.

Another wise LOOP will do this:

 (LOOP for item in some-list when (predicate-p item) collect it) 

IT is a LOOP function -> it refers to the value returned by the predicate in the WHEN clause.

+1
source

mapping approach:

 ;; assuming get-value-if-attrib-present simply returns nil when not present ;; (ie your attribute value cannot be nil without ambiguity) ;; ;; get the list of values from the children, matching attrib ;; (mapcan (lambda (child) (if (get-value-if-attrib-present child attrib) (list child))) children) 

mapcan expects the function to return lists, and it destroys them. Therefore, you must be careful not to return the quoted lists from the lambda or any lists that came from somewhere else (not here).

In artificial programming paradigms (aka PAIP), Peter Norwig introduces the mappend function, which does the same, but indestructible. It is useful to have in your toolkit sometimes.

+1
source

Source: https://habr.com/ru/post/1438496/


All Articles