How to use mapcar here?

China☆狼群 提交于 2020-01-05 04:10:29

问题


(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 a class. (pt-children node) yields children of node which are also pt objects. attr is a string. suppossing I write get-value-if-attrib-present to return the value of a pt object if it has the matching attr, how do i go about getting the list of all the values of subchildren of node with matching attr here (at ####....)?


回答1:


For Common Lisp use one of these functions:

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

They go over a list and remove items. Keep those which you want.

Other wise LOOP will do it:

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

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




回答2:


mapping approach:

;; assuming get-value-if-attrib-present simply returns nil when not present
;; (i.e. 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 destructively catenates them. So you have to be careful not to return quoted lists from the lambda, or any lists that came from somewhere else (not consed up here).

In Paradigms of Artificial Intelligence Programming (a.k.a PAIP), Peter Norvig introduces a mappend function which does the same thing, but nondestructively. That's useful to have in your toolkit sometimes.



来源:https://stackoverflow.com/questions/9946612/how-to-use-mapcar-here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!