Search code examples
stringcommon-lisplowercase

Check are there lowercase characters in string


I need return True or False

  • True if at least one lowercase character
  • False no lowercase characters

I tried do it with loop and lambda function Something like this

(defun check-lower-word (word)
    (loop
        for ch across word 
        ((lambda (c) (if (lower-case-p c) return T) ch)
    )
)

I need False if never worked "if"


Solution

  • With a predefined function, you could use some (manual):

    CL-USER> (some #'lower-case-p "AbC")
    T
    CL-USER> (some #'lower-case-p "ABC")
    NIL
    

    There is a similar operation for the loop syntax (manual):

    CL-USER> (loop for x across "AbC" thereis (lower-case-p x))
    T
    CL-USER> (loop for x across "ABC" thereis (lower-case-p x))
    NIL
    

    Finally, note that loop always returns nil when the iteration terminates without producing a result, so a less concise use of loop could be:

    CL-USER> (loop for x across "AbC" if (lower-case-p x) do (return t))
    T
    CL-USER> (loop for x across "ABC" if (lower-case-p x) do (return t))
    NIL