开发者

write comparison string function

I fully understand the use of list in lisp but I 've got problem using string. I try to write my own code of fu开发者_如何学运维nctions like string> or string< from common lisp to understand how lisp deals with string. For example, abcde is bigger than abbb and returns 1.

I think that I will use the char function or do you think that I must use subseq ? or function that deals with ASCII code ?

Here are cases that I found : -character 0 of each string are equal, we continue, with the next character. -charactere 0 are different, one is smaller that other, we stop.

I need help about "go to the next character".

Thanks a lot !!


This is the Common Lisp version. You can just use ELT because

(type-of "a") => (SIMPLE-ARRAY CHARACTER (1))

(defun my-string< (a b &key (start 0))
  (cond
    ((= start (length a) (length b))
     nil)
    ((>= start (min (length a) (length b)))
     (error "Undefined"))
    ((char= (elt a start) (elt b start))
     (my-string< a b :start (1+ start)))
    ((char< (elt a start) (elt b start))
     t)))


This is an implementation of a function that given two strings will return either -1, 0 or +1 depending on if the first is less than, equal or greater than the second. In case one string is the initial part of the other then shorter string is considered to be "less than" the longer.

The algorithm is very simple... loops for every char until either the index gets past one of the strings or if a character is found to be different.

(defun strcmp (a b)
  (do ((i 0 (1+ i))
       (na (length a))
       (nb (length b)))
      ((or (= i na) (= i nb) (char/= (elt a i) (elt b i)))
         (cond
           ((= i na nb) 0)                  ;; Strings are identical
           ((= i na) -1)                    ;; a finished first
           ((= i nb) 1)                     ;; b finished first
           ((char< (elt a i) (elt b i)) -1) ;; Different char a < b
           (t 1)))))                        ;; Only remaining case

(defun test (a b)
  (format t "(strcmp ~s ~s) -> ~s~%"
          a b (strcmp a b)))

(test "abc" "abc")
(test "ab"  "abc")
(test "abc" "ab")
(test "abd" "abc")
(test "abc" "abd")

The output is

(strcmp "abc" "abc") -> 0
(strcmp "ab" "abc") -> -1
(strcmp "abc" "ab") -> 1
(strcmp "abd" "abc") -> 1
(strcmp "abc" "abd") -> -1


Your problem has been solved already but in case you run into others, the following method might be useful:

I installed SBCL from source and keep the source around. That allows me to run M-. on a function name like string< and it will jump to the definition in your lisp implementation.

In my case I ended up at this macro:

;;; LESSP is true if the desired expansion is for STRING<* or STRING<=*.
;;; EQUALP is true if the desired expansion is for STRING<=* or STRING>=*.
(sb!xc:defmacro string<>=*-body (lessp equalp)
  (let ((offset1 (gensym)))
    `(with-two-strings string1 string2 start1 end1 ,offset1 start2 end2
       (let ((index (%sp-string-compare string1 start1 end1
                                        string2 start2 end2)))
         (if index
             (cond ((= (the fixnum index) (the fixnum end1))
                    ,(if lessp
                         `(- (the fixnum index) ,offset1)
                       `nil))
                   ((= (+ (the fixnum index) (- start2 start1))
                       (the fixnum end2))
                    ,(if lessp
                         `nil
                       `(- (the fixnum index) ,offset1)))
                   ((,(if lessp 'char< 'char>)
                     (schar string1 index)
                     (schar string2 (+ (the fixnum index) (- start2 start1))))
                    (- (the fixnum index) ,offset1))
                   (t nil))
             ,(if equalp `(- (the fixnum end1) ,offset1) nil))))))
) ; EVAL-WHEN


There is no direct concept of iteration ("next") with strings, in Scheme. That only applies to lists. So instead you have to iterate with indices:

(define (string<? lhs rhs)
  (let* ((lhslen (string-length lhs))
         (rhslen (string-length rhs))
         (minlen (min lhslen rhslen)))
    (let loop ((i 0))
      (if (= i minlen) (< lhslen rhslen)
          (let ((lhschar (string-ref lhs i))
                (rhschar (string-ref rhs i)))
            (cond ((char<? lhschar rhschar) #t)
                  ((char<? rhschar lhschar) #f)
                  (else (loop (+ i 1)))))))))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜