Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure can't pass list to a function Error PersistentList cannot be cast to clojure.lang.IFn

I have a few functions that deal with lists. I have an even function which accepts a list parameter and gets the even indexes of the list. The odd function does the same thing but with odd indexes. I also have another function that merges two sorted lists called merge-list that takes two lists as parameters.

The problem is with the function I am writing now: merge-sort.

Here is what I have:

(defn merge-sort [lis]
    (if (empty? (rest lis))
        lis
        (merge-list (merge-sort (odd(lis))) (merge-sort (even(lis))))))))

For some reason I keep getting the error

java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to clojure.lang.IFn

I can pass the odd function rest lis like this (odd(rest lis)) (same with even). It runs fine but that obviously doesn't give me the solution I want.

I'm very new to Clojure so any tips would be appreciated. Thanks.

like image 613
Link Avatar asked Feb 25 '23 08:02

Link


1 Answers

(odd lis) and (even lis), not (odd (lis)). You want to pass it as a parameter, not call it as a function and then pass the result of that.

like image 163
amalloy Avatar answered May 09 '23 03:05

amalloy