fork download
  1. (defun ebi-lang-p (s)
  2. (loop with stack = ()
  3. for c across s
  4. do (cond ((char= c #\A)
  5. (push c stack))
  6. ((char= c #\B)
  7. (if stack
  8. (pop stack)
  9. ;; 対応する A のない B に出会した
  10. (return nil)))
  11. (t
  12. ;; A と B 以外を含む場合は偽とする
  13. (return nil)))
  14. finally (return (null stack))))
  15.  
  16. (loop for s in '("" "A" "B" "AB" "ABB" "AAB" "AABB" "ABAABBAAABBB" "ABC")
  17. for v = (ebi-lang-p s)
  18. do (format t "~A → ~:[🐟~;🦐~]~%" s v))
  19.  
Success #stdin #stdout 0.01s 29280KB
stdin
Standard input is empty
stdout
 → 🦐
A → 🐟
B → 🐟
AB → 🦐
ABB → 🐟
AAB → 🐟
AABB → 🦐
ABAABBAAABBB → 🦐
ABC → 🐟