LambdaExpr_

Common Lisp Exercise

Jul 31st, 2025 (edited)
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 3.67 KB | None | 0 0
  1. CL-USER> (defstruct string-slice
  2.            (string-data nil :type string)
  3.            (begin-index 0 :type integer)
  4.            (end-index nil :type integer))
  5. STRING-SLICE
  6. CL-USER> (defun string-split-by-char (ch str)
  7.            (declare (type character ch)
  8.                     (type string str)
  9.                     (optimize (speed 3) (safety 1)))
  10.            (let ((last-index 0)
  11.                  (len (length str))
  12.                  (result nil))
  13.              (do ((i 0 (1+ i)))
  14.                  ((>= i len)
  15.                   (push (make-string-slice :string-data str
  16.                                            :begin-index last-index
  17.                                            :end-index i)
  18.                         result)
  19.                   (nreverse result))
  20.                (if (eql ch (char str i))
  21.                    (progn (push (make-string-slice :string-data str
  22.                                              :begin-index last-index
  23.                                              :end-index i)
  24.                                 result)
  25.                           (setf last-index (1+ i)))))))
  26. STRING-SPLIT-BY-CHAR
  27. CL-USER> (string-split-by-char #\Space "To be, or not to be")
  28. (#S(STRING-SLICE
  29.     :STRING-DATA "To be, or not to be"
  30.     :BEGIN-INDEX 0
  31.     :END-INDEX 2)
  32.  #S(STRING-SLICE
  33.     :STRING-DATA "To be, or not to be"
  34.     :BEGIN-INDEX 3
  35.     :END-INDEX 6)
  36.  #S(STRING-SLICE
  37.     :STRING-DATA "To be, or not to be"
  38.     :BEGIN-INDEX 7
  39.     :END-INDEX 9)
  40.  #S(STRING-SLICE
  41.     :STRING-DATA "To be, or not to be"
  42.     :BEGIN-INDEX 10
  43.     :END-INDEX 13)
  44.  #S(STRING-SLICE
  45.     :STRING-DATA "To be, or not to be"
  46.     :BEGIN-INDEX 14
  47.     :END-INDEX 16)
  48.  #S(STRING-SLICE
  49.     :STRING-DATA "To be, or not to be"
  50.     :BEGIN-INDEX 17
  51.     :END-INDEX 19))
  52. CL-USER> (defun slice-to-string (slice)
  53.            (declare (type string-slice slice)
  54.                     (optimize (speed 3) (safety 1)))
  55.            (subseq (string-slice-string-data slice)
  56.                    (string-slice-begin-index slice)
  57.                    (string-slice-end-index slice)))
  58. SLICE-TO-STRING
  59. CL-USER> (mapcar #'slice-to-string
  60.                  (string-split-by-char #\Space "To be, or not to be"))
  61. ("To" "be," "or" "not" "to" "be")
  62. CL-USER> (defstruct tree-node
  63.            (elmnt nil)
  64.            (l-child nil :type (or null tree-node))
  65.            (r-child nil :type (or null tree-node)))
  66. TREE-NODE
  67. CL-USER> (defun list-to-tree (lst)
  68.            (cond
  69.              ((null lst)
  70.               nil)
  71.              ((not (listp lst))
  72.               (error "Invalid list: ~A must be a list" lst))
  73.              ((not (eql 3 (length lst)))
  74.               (error "Invalid list: ~A must have 3 elements" lst))
  75.              ((not (atom (car lst)))
  76.               (error "Invalid list: ~A must be an atom" (car lst)))
  77.              ((not (listp (cadr lst)))
  78.               (error "Invalid list: ~A must be a list" (cadr lst)))
  79.              ((not (listp (caddr lst)))
  80.               (error "Invalid list: ~A must be a list" (caddr lst)))
  81.              (t
  82.               (make-tree-node
  83.                :elmnt (car lst)
  84.                :l-child (list-to-tree (cadr lst))
  85.                :r-child (list-to-tree (caddr lst))))))
  86. LIST-TO-TREE
  87. CL-USER> (list-to-tree nil)
  88. NIL
  89. CL-USER> (list-to-tree '(A NIL NIL))
  90. #S(TREE-NODE :ELMNT A :L-CHILD NIL :R-CHILD NIL)
  91. CL-USER> (list-to-tree '(A (B NIL NIL) (C (D NIL NIL) (E NIL NIL))))
  92. #S(TREE-NODE
  93.    :ELMNT A
  94.    :L-CHILD #S(TREE-NODE :ELMNT B :L-CHILD NIL :R-CHILD NIL)
  95.    :R-CHILD #S(TREE-NODE
  96.                :ELMNT C
  97.                :L-CHILD #S(TREE-NODE :ELMNT D :L-CHILD NIL :R-CHILD NIL)
  98.                :R-CHILD #S(TREE-NODE :ELMNT E :L-CHILD NIL :R-CHILD NIL)))
  99. CL-USER>
  100.  
  101.  
Advertisement
Add Comment
Please, Sign In to add comment