Clojure beginner trying to debug a function I wrote for a homework assignment (see below). We’re supposed to do this recursively and I’m trying to nail it down but running into the error that I made the title of this post. From what I found online, it’s making reference to a number being used where a collection should be and the compiler doesn’t like it. Not sure where the problem lies. Any tips or tweaks to the function I can make? My function is here. The compiler makes reference to line 39.
Create a function of 3 arguments, (merge-pred pred lst1 lst2), where pred is a predicate function
of two arguments, and lst1 and lst2 are lists. pred will indicate an ordering relationship. (pred a b)
returns true if and only if a ≤p b and returns false otherwise. Note that this is the reverse of the ordering predicate used in max-pred. The arguments lst1 and lst2 must already be sorted in increasing order according to pred. In the event of a tie (pred considers two elements to be “equal”), the elements from lst1 should appear before the tied elements of lst2. Note: “equal” does not mean the same. For example, if pred compares elements based on their string length, ”hello” and ”olleh” would be considered equal. The return value of merge-pred should be the results of merging the elements of the lists in sorted order (according to pred). Sample runs:
conj expects a collection as the first argument and one or more elements to add to the collection as the second and subsequent arguments.
That works on line 37 because you have (conj collection (first collection)) but on line 39 you have (conj (first collection) collection) so that first argument is a Long, not a collection.
You can use cons there instead which expects (cons element collection) and will produce a new collection that starts with that element, followed by all the items in collection.
(I won’t make any other comments on the code until you have it working and are looking for feedback on improving it)
Although you’re taking the first element of the sorted collection when merging results, you then merge the unsorted(rest collection) – so the element you’re dropping is the first element before sorting.
I wonder if things would be simpler if you just assumed the inputs were sorted, and handled that requirement elsewhere (in another function that passes sorted lists to this one).
That part makes sense. What I don’t get is the output was the same even before I sorted that list. I guess I’m misunderstanding what you meant by the below:
Although you’re taking the first element of the sorted collection when merging results, you then merge the unsorted(rest collection) – so the element you’re dropping is the first element before sorting.
Were you referring to (rest lst1)? That’s what I thought you were referring to so that’s whaat I was attempting to sort. The output was the same.
I second that. Note that if we’re allowed to use a (stable) sorting function, then there’s little to be won by not taking, say, (defn merge-pred [p xs ys] (sort p (concat xs ys)))¹.
(From how the assignment is stated – which also says the inputs “must already be sorted” – I’d expect relying on sorting oracles to be prohibited, since such merge functions are then often used to implement sorting in the first place.)
¹ P.S. I see now that clojure’s sort is stable when given strict order relation, so we’d have to go with
Both satisfy OP’s requirements afaics, although first one probably not guaranteed to do so. Still, I’d assume that using sort in any way is not in the spirit of the assignment, even though it’s not explicitly stated.