Arc Forumnew | comments | leaders | submitlogin
Strings as lists, generic regexes
2 points by vrk 6112 days ago | 3 comments
Yet another reason why strings should be a special case of lists in Arc and in any programming language (ignoring how they are implemented) is that the empty string would naturally and logically map to the empty list, and vice versa. So,

  (string '()) ; --> ""
  (is '() "")  ; --> t
  (is nil "")  ; --> t
I can't run Arc at work, so I can't test if this is the case already. If not, it should be. Also, last time I tried it was not possible to take the car or cdr of a string, but it was possible to use scar and scdr. This is illogical.

If we had non-deterministic choose and fail as defined in On Lisp, we would have a very nice partial implementation of regular expressions:

  (def m (str pat)
     (rfn match (str pat result)
        (if (no str) result
            (no cdr.str) (if (and (matches str pat) (no cdr.pat) result (fail))
            (choose 
               (if (matches car.str pat) (match car.str advance.pat (cons car.str result)) (fail))
               (match cdr.str pat '()))
            (fail))
      (reverse (match str pat '())))

  (def advance (pat)
     ; advance the pattern in some way (taking into account
     ; repetition etc.)
   )

  (def matches (elt pat)
     ; test if the element matches the beginning of the pattern
     ; (for example, elt == "a" and pat == ".?" --> t
   )

  (m "hi there" '(h .?)) ; succeeds (twice even)
The above is probably inefficient and more importantly incorrect, but there's no reason why it couldn't be applied to any list next, given that we have a way to define the pattern to search for:

  (m '(a b c) '(.? b c)) ; succeeds
  (m '(a b c) '(a .*)) ; succeeds
  (m '(a (b c) d) '(a .*)) ; succeeds
  (m '(a (b c) d) '(a b)) ; fails
  (m '(a (b c) d) '(a (b .*)) ; succeeds
(End of random thought of the day.)


3 points by bOR_ 6112 days ago | link

Tested for you (arc1):

  Use (quit) to quit, (tl) to return here after an interrupt.
  arc> (string '())
  ""
  arc> (is '() "")
  nil
  arc> (is nil "")
  nil
  arc>

-----

2 points by stefano 6111 days ago | link

Implementing strings as list would be rather slow and memory consuming: you would have to use a cons cell for every character. Implementing strings as vectors and making them visible to the user as lists would be interesting.

-----

3 points by vrk 6111 days ago | link

It doesn't matter how they are implemented (they can even be cons cells implemented as closures), as long as you can use the ordinary list functions to manipulate them. But this is the direction Arc is headed to anyway.

-----