-1

これは、重複する要素を持つ単一のリストのニーブケースを処理しています。ネストされたリストを処理する結び目になっていたので、最初に単純なケースを書きたかったのです。ので、私は持っています:

    (defn packDuplicatesIntoLists [listOfElements l e]
      (if(= e 'nil)
        'true
        (if(= () listOfElements)
          (if 
            (= e '())
            l
            (list l e)
          )
          (if 
              (= (first listOfElements) (first e) )
              (packDuplicatesIntoLists (rest listOfElements) l (cons (first listOfElements) e))
      (packDuplicatesIntoLists (rest listOfElements) (list l e) (first listOfElements))
          )
        )

) )

    (packDuplicatesIntoLists '(2) '(1 1) '(2 2))  (packDuplicatesIntoLists '() '(1 1) '(2 2))  (packDuplicatesIntoLists '() '() '()) (packDuplicatesIntoLists '(1 1 1 2 2 2 3 3 3 4 4 4 4) '() '())

しかし、 (packDuplicatesIntoLists (残りの listOfElements) (list le) (最初の listOfElements)) は私を困らせています、

    #'NintyNineProblems.LearnSpace/packDuplicatesIntoLists
    ((1 1) (2 2 2))
    ((1 1) (2 2))
    ()
    IllegalArgumentException Don't know how to create ISeq from: java.lang.Long  clojure.lang.RT.seqFrom (RT.java:505

その行の何が問題になっていますか?

4

1 に答える 1

0

そのため、それでも lsit を逆にしますが、末尾再帰的です

     (defn packDuplicatesIntoLists [listOfElements l e]
      (if(= '() listOfElements)
       (cons e l)
       (if 
        (= (first listOfElements) (first e) )
         (recur (rest listOfElements) l (cons (first listOfElements) e))
         (if (= '() e)
          (recur (rest listOfElements) l (list (first listOfElements)))
          (recur (rest listOfElements) (cons e l) (list (first listOfElements)))
         )
        )
       )
      )

(packDuplicatesIntoLists '(2 2 2 4 4 4 5 5 5 8 8 8 6 9 9 9 9 9) '() '()) (packDuplicatesIntoLists '() '() '()) (packDuplicatesIntoLists 'nil '() ' ())

于 2013-05-30T04:36:59.720 に答える