[racket] datum-intern-literal and lists?
I agree that the call to `datum-intern-literal' below doesn't make
sense. The `datum-intern-literal' function should be applied to the
`substring' results, not to the `append' result.
At Mon, 25 Jun 2012 16:09:32 -0400, 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.