I’m very new to Scheme and I’m getting an “ill-formed special form” error when I try to use this function. I want the function to return left if the length of original is 0. Else I want it to execute the calls to append and the recursive call. I imagine I’ve probably done something really silly that is causing this. ANy help?
(define (partition left right original)
(if (= (length original) 0) left
(append left (car original))
(append right (car (reverse original)))
(partition left right (reverse original))))
Before I begin, do try to indent your programs properly. In this case, it would have made the error much more obvious.
You’re running into a speedbump that many Scheme newbies hit coming from other languages:
ifin Lisp (including scheme) is actually the ternary operator, not theifblock from the C-like world.Your code tries to present
ifwith more than three forms, so it fails. Naively, what you could do is use the block constructbegin(prognin Common Lisp) to make sure that a single branch of the conditional only contains a single form.Now, I get the feeling that this won’t do what you want either, because
appendis functional (that is, it returns a result without modifying its arguments). In other words, neither of your calls toappendwill do anything, andoriginalwill never get shorter. What you actually seem to want here is something like