[racket] datum-intern-literal and lists?

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Mon Jun 25 16:30:05 EDT 2012

I think the docs are just worded a little bit carefully. From what I can
tell, it is the identity function on pairs (and on anything that isn't
explicitly listed in the docs, actually).

But one note: in the common case, it looks like your code will copy all of
the strings:

> (define x "abc")
> (substring x 0)
"abc"
> (eq? x (substring x 0))
#f

I guess these strings are not mutated, so you can probably avoid that in
the start = 0 case (something I see the original code does).

Robby


On Monday, June 25, 2012, Danny Yoo wrote:

> Hi everyone,
>
> Is there a reason to use the function "datum-intern-literal" on a list
> structure?  According to the documentation, datum-intern-literal does
> not traverse compound values, so I'm unsure about its use on list
> structures.
>
> I'm curious because I'm staring at the definition of decode-string in
> scribble/decode, and it's trying to apply datum-intern-literal against
> a whole list, which seems strange to me:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (define (decode-string s)
>  (let loop ([l '((#rx"---" mdash)
>                  (#rx"--" ndash)
>                  (#rx"``" ldquo)
>                  (#rx"''" rdquo)
>                  (#rx"'" rsquo))])
>    (cond [(null? l) (list s)]
>          [(regexp-match-positions (caar l) s)
>           => (lambda (m)
>                (datum-intern-literal
>                 (append (decode-string (substring s 0 (caar m)))
>                         (cdar l)
>                         (decode-string (substring s (cdar m))))))]
>          [else (loop (cdr l))])))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
>
> I'm looking at this function because the Racket statistical profiler
> is telling me that it might be helpful to optimize it.  I had expected
> something more along the lines of:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (define (decode-string s)
>  (define pattern #rx"(---|--|``|''|')")
>  (let loop ([start 0])
>    (cond
>     [(regexp-match-positions pattern s start)
>      => (lambda (m)
>           (define the-match (substring s (caar m) (cdar m)))
>           (list* (substring s start (caar m))
>                  (cond
>                   [(string=? the-match "---") 'mdash]
>                   [(string=? the-match "--") 'ndash]
>                   [(string=? the-match "``") 'ldquo]
>                   [(string=? the-match "''") 'rdquo]
>                   [(string=? the-match "'") 'rsquo])
>                  (loop (cdar m))))]
>     [else
>      (list (substring s start))])))
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>
> but I'm not sure about the role of the datum-intern-literal stuff with
> regards to lists.
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20120625/3ed403c8/attachment.html>

Posted on the users mailing list.