Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion 将2个值合并到返回列表的方法?_Recursion_Clojure_Artificial Intelligence - Fatal编程技术网

Recursion 将2个值合并到返回列表的方法?

Recursion 将2个值合并到返回列表的方法?,recursion,clojure,artificial-intelligence,Recursion,Clojure,Artificial Intelligence,如您所见,一旦代码找到匹配项,我将返回obj数据的第三个值,是否有办法使obj数据的第二个值每次与第三个值一起返回,然后在该值的末尾添加一个分隔符 它当前返回项目(水果/代理)的位置,但我希望它返回特定的项目和位置 所以它看起来像: (defn returnLoc [obj-data super-cat] ;If the list passed through is not empty (if-not (empty? obj-data) ;If the super-cat

如您所见,一旦代码找到匹配项,我将返回obj数据的第三个值,是否有办法使obj数据的第二个值每次与第三个值一起返回,然后在该值的末尾添加一个分隔符

它当前返回项目(水果/代理)的位置,但我希望它返回特定的项目和位置

所以它看起来像:

    (defn returnLoc [obj-data super-cat]
  ;If the list passed through is not empty
  (if-not (empty? obj-data)
    ;If the super-category passed in (i.e. Fruit/Agent) is equal
    ;to the Super-Category (the second object in the first row)
    (if (= super-cat (nth (first obj-data) 2))
      ;Recurvisely goes through the same process as above,
      ;To see if there is any other records in the list with the same super-cat
      ;then finds the location of the object and conj[oin]'s that to the returned values
      (conj (returnLoc (rest obj-data) super-cat)
            (nth (first obj-data) 3))
      ;If the super-cat passed through is not equal, it does not add it to the list
      ;And recursively goes back through to check if there are any other possible items to add to
      ;the list.
      (returnLoc (rest obj-data) super-cat)
      )
    ())
  )
返回:

(returnLoc obj-data 'agent)
理想情况下,我希望它返回:

=>(hallway bedroom)

有人有解决方法吗?

想出了我自己的解决方案:

=>(tom is in hallway | jerry is in  bedroom)

找到了我自己的有效解决方案:

=>(tom is in hallway | jerry is in  bedroom)

conj
将多个元素添加到集合中:

(def obj-data
              '((apple#3 apple fruit kitchen)
                 (mango#5 mango fruit kitchen)
                 (tom cat agent hallway)
                 (jerry mouse agent bedroom)
                 (matthew JavaStudentMatthew student livesAtHome)
                 (tom NetworkStudentTom student newcastleHome)
                 (Nathan NetworkStudentNathan student middlesbroughHome)
                 (Jack NetworkStudentJack student kexgillHome)
                 ))

(defn returnLoc [obj-data super-cat]
  ;If the list passed through is not empty
  (if-not (empty? obj-data)
    ;If the super-category passed in (i.e. Fruit/Agent) is equal
    ;to the Super-Category (the second object in the first row)
    (if (= super-cat (nth (first obj-data) 2))
      ;Recurvisely goes through the same process as above,
      ;To see if there is any other records in the list with the same super-cat
      ;then finds the location of the object and conj[oin]'s that to the returned values
      (cons (list (nth (first obj-data) 1) " is in " (nth (first obj-data) 3))
      (returnLoc (rest obj-data) super-cat))
      ;If the super-cat passed through is not equal, it does not add it to the list
      ;And recursively goes back through to check if there are any other possible items to add to
      ;the list.
      (returnLoc (rest obj-data) super-cat)
      )
    ())
  )

conj
将多个元素添加到集合中:

(def obj-data
              '((apple#3 apple fruit kitchen)
                 (mango#5 mango fruit kitchen)
                 (tom cat agent hallway)
                 (jerry mouse agent bedroom)
                 (matthew JavaStudentMatthew student livesAtHome)
                 (tom NetworkStudentTom student newcastleHome)
                 (Nathan NetworkStudentNathan student middlesbroughHome)
                 (Jack NetworkStudentJack student kexgillHome)
                 ))

(defn returnLoc [obj-data super-cat]
  ;If the list passed through is not empty
  (if-not (empty? obj-data)
    ;If the super-category passed in (i.e. Fruit/Agent) is equal
    ;to the Super-Category (the second object in the first row)
    (if (= super-cat (nth (first obj-data) 2))
      ;Recurvisely goes through the same process as above,
      ;To see if there is any other records in the list with the same super-cat
      ;then finds the location of the object and conj[oin]'s that to the returned values
      (cons (list (nth (first obj-data) 1) " is in " (nth (first obj-data) 3))
      (returnLoc (rest obj-data) super-cat))
      ;If the super-cat passed through is not equal, it does not add it to the list
      ;And recursively goes back through to check if there are any other possible items to add to
      ;the list.
      (returnLoc (rest obj-data) super-cat)
      )
    ())
  )

如果您使您的解决方案更加习惯化,那么您眼前的问题就会消失:

  • obj数据的每个元素表示为映射(或记录),而不是列表
  • 识别
    returnLoc
    计算的模式
  • 1。将
    obj数据的每个元素表示为映射(或记录),而不是列表。

    您想要的解决方案可能是

    (conj '(on this list) 'elements 'two)
    ; ==> (two elements on this list)
    
    这是相当可读的,所以您不必急于将其翻译成纯文本

    2。识别
    returnLoc
    计算的模式。

    returnLoc
    做什么

    • 它选择具有特定属性的obj数据元素
      特征:它们的
      :其中
      值为
      super-cat
      。这是一个 手术
    • 它从所有这些元素中提取
      :who
      属性。这是一个 手术
    然后,您的
    returnLoc
    函数可能会

    [{:who 'Tom, :where 'hallway} {:who 'Jerry, :where 'bedroom}]
    
    。。。或者,使用线程宏

    (defn returnLoc [obj-data super-cat]
      (map
        :who
        (filter
          #(= (:where %) super-cat)
          obj-data)))
    
    • 关键字
      :who
      :where
      用作访问函数
    • 这些版本维护
      obj数据中元素的顺序。你的
      代码将其反转

    既然您想保留
    :who
    :where
    字段,为什么不简单地返回整个地图/记录:

    (defn returnLoc [obj-data super-cat]
      (->> obj-data
           (filter #(= (:where %) super-cat))
           (map :who)))
    
    这节省了工作,因为不可变映射是通过引用返回的。不需要构建新地图

    如果决定删除其他字段,请使用
    选择键

    (defn returnLoc [obj-data super-cat]
      (filter
        #(= (:where %) super-cat)
        obj-data))
    

    如果您使您的解决方案更加习惯化,那么您眼前的问题就会消失:

  • obj数据的每个元素表示为映射(或记录),而不是列表
  • 识别
    returnLoc
    计算的模式
  • 1。将
    obj数据的每个元素表示为映射(或记录),而不是列表。

    您想要的解决方案可能是

    (conj '(on this list) 'elements 'two)
    ; ==> (two elements on this list)
    
    这是相当可读的,所以您不必急于将其翻译成纯文本

    2。识别
    returnLoc
    计算的模式。

    returnLoc
    做什么

    • 它选择具有特定属性的obj数据元素
      特征:它们的
      :其中
      值为
      super-cat
      。这是一个 手术
    • 它从所有这些元素中提取
      :who
      属性。这是一个 手术
    然后,您的
    returnLoc
    函数可能会

    [{:who 'Tom, :where 'hallway} {:who 'Jerry, :where 'bedroom}]
    
    。。。或者,使用线程宏

    (defn returnLoc [obj-data super-cat]
      (map
        :who
        (filter
          #(= (:where %) super-cat)
          obj-data)))
    
    • 关键字
      :who
      :where
      用作访问函数
    • 这些版本维护
      obj数据中元素的顺序。你的
      代码将其反转

    既然您想保留
    :who
    :where
    字段,为什么不简单地返回整个地图/记录:

    (defn returnLoc [obj-data super-cat]
      (->> obj-data
           (filter #(= (:where %) super-cat))
           (map :who)))
    
    这节省了工作,因为不可变映射是通过引用返回的。不需要构建新地图

    如果决定删除其他字段,请使用
    选择键

    (defn returnLoc [obj-data super-cat]
      (filter
        #(= (:where %) super-cat)
        obj-data))