-1

I am using a Windows 11 computer, running Steel Bank Common Lisp (version 2.0.0) in PowerShell by inputting functions directly into the SBCL.exe REPL to see if they work. In the process, I have hit upon a problem with the following code.

The point of this function is to take the string passed into it and trim off excess spaces (for example, "This is a string" becomes "This is a string")

(defun trim-spaces (str)
   (let* 
     ((len (length(str))) 
     (i 1)
     (last-char (char str (- 1 i)))
     (curr-char (char str i)) )
  
     (dotimes (i len) 
        (cond 
           ((eql last-char #\Space)
              (cond 
                 ((eql curr-char #\Space)
                    (setq str (remove curr-char str)))))))))

(trim-spaces "This   is  a string")

I have made similar functions before (such as one function to remove parentheses from a string) and they have worked fine. In this case, however, if I run the above code, I get the following message. This message is the same if I input a string variable instead of a string literal:

debugger invoked on a UNDEFINED-FUNCTION @1000000C2C in thread #<THREAD tid=19952 "main thread" RUNNING {1100BC8003}>: The function COMMON-LISP-USER::STR is undefined.

I would like to know how this problem is coming about, and how to fix/sidestep it. Thank you for your time.

5
  • does (length(str)) count as a typo? Commented Aug 3 at 23:04
  • 3
    Yes, the (str) should be just str. - I don't underestand why you guys closed this question. It contained all information necessary and needed. The code shows everything and he told what is the interpreter and his OS - and the error which makes clear what the problem was. No need to close it. Commented Aug 4 at 7:06
  • 1
    if it counts as a typo, that's a valid reason to close, officially documented. typos are too varied (and inconsequential) for any one question to be of value to anyone else. the existence of a typo also indicates that OP probably didn't debug or even just read their own code, nor did they understand the error message that usually indicates the exact place the issue came from. we expect a bare minimum of diligence before posting questions. this is a fairly low bar. Commented Aug 4 at 10:31
  • It was not a typo, although I had a suspicion I should have altered it Commented Aug 4 at 13:08
  • note also that the inside of dotimes does not depend on i Commented Aug 4 at 14:29

1 Answer 1

4

On this line ((len (length(str))), you are calling str - but str isn't function, but variable. The whole line should look like this: ((len (length str)). The whole function won't work even if you fix that, so here are some approaches:

One-liner (cl-ppcre):

(ppcre:regex-replace-all " +" "This   is  a string" " ")

Loop version:

(defun trim-spaces-loop (str)
  (let ((last-char-added nil)) 
    (coerce 
     (loop for current-char across str
           when (or (not (eql current-char #\ )) 
                    (not (eql last-char-added #\ )))
           collect current-char
           and do (setq last-char-added current-char))
     'string)))

(trim-spaces-loop "This   is  a string")

Reduce version:

(defun trim-spaces-reduce (str)
  (cdr (reduce (lambda (result c)
            (let ((last-seen (car result))
                  (acc (cdr result)))
              (if (and (eql c #\ )
                       (eql last-seen c))
                  (cons c acc)
                (cons c (concatenate 'string acc (list c))))))
          (coerce str 'list)
          :initial-value (cons nil ""))))

(trim-spaces-reduce "This   is  a string")
Sign up to request clarification or add additional context in comments.

1 Comment

(defun trim-spaces (str) (coerce (loop for (x y . rest) on (coerce str 'list) when (not (and y (char= #\space x) (char= #\space y))) collect x) 'string)) would do it. It uses destructuring on the character list for lookup of the next character and collects only when no two consecutive space follow and coerces to a string back.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.