Re: [racket-users] Converting bulleted list to s-expression?
On Sun, Aug 19, 2018 at 11:03 AM, Andrew J wrote: > Very nice. The Racket special sauce is the (match*...). I need to get my > head around what's going on there. > > If you already understand match, perhaps this will help: > (match (list 5 6) [(list a b) (+ a b)]) 11 More concise, match* equivalent: > (match* (5 6) [(a b) (+ a b)]) 11 Dan -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Converting bulleted list to s-expression?
As an example, this... - a - b c - d e f - g h would get transformed into something like this... '(a (b c) (d e f (g h))) You can implement this in 2 stages. Stage 1 is line based parsing, turn the input text into this list: '((0 a) (2 b c) (2 d e f) (4 g h))) the number is the indent level. This part is quite easy, you could do this with regex. Stage 2 is a bit more difficult, the algorithm to do this is based on recursive stream processing. You are reading or 'parsing' a tree off of a flat string of indent levels. When processing a head at indent level N you read off all the subtrees at indent level >N you can see, then return two values: the tree and the remainder of the stream. (define (collect inputs) (let-values (((h t) (collect^ inputs))) (unless (null? t) (error 'collect "failed to collect everything" t)) h)) (define (collect^ inputs) (let ((indent (caar inputs)) (head (cdar inputs)) (inputs (cdr inputs))) (n-collect indent head inputs))) (define (n-collect n head stream) ;; n-collect will collect up all ;; subtrees off the stream whose ;; level is > N (let loop ((subtrees '()) (stream stream)) (if (or (null? stream) (<= (caar stream) n)) (values (append head (reverse subtrees)) stream) (let-values (((subtree stream) (collect^ stream))) (loop (cons subtree subtrees) stream) -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Converting bulleted list to s-expression?
Den lør. 18. aug. 2018 kl. 03.31 skrev Andrew J : > Hi. In a little side project, I'm looking for an simple Racket-y way to > transform a bulleted list into a tree structure, maybe like sxml. > > As an example, this... > > - a > - b c > - d e f > - g h > > would get transformed into something like this... > > '(a (b c) (d e f (g h))) > > I can see a few ways to do this, from recursively counting indents, right > through to making a mini DSL (à la *Beautiful Racket*), but I'm wondering > what a minimal idiomatic solution might look like? > > > Here is how I would write it. #lang racket (define (non-empty-string? s) (not (string=? s ""))) (define (string->lines s) (filter non-empty-string? (regexp-match* #rx"[^\n]*" s))) (define (indentation-level s) (for/first ([c s] [i (in-naturals)] #:unless (char=? c #\space)) i)) (define (string->symbols s) (for/list ([x (in-port read (open-input-string s))]) x)) (define (strip-dash xs) (rest xs)) (define (bullet-string->nested-list s) (define lines (string->lines s)) (define levels (map indentation-level lines)) (define symbols (map strip-dash (map string->symbols (string->lines s (define (build js xss) (match* (js xss) [('() _) '()] [((list j) (list xs)) xs] [((list* i j js) (list* xs xss)) (cond [(= i j) (cons xs (list (build (cons j js) xss)))] [(< i j) (append xs (list (build (cons j js) xss)))] [else(error 'build "not proper nesting")])])) (build levels symbols)) (bullet-string->nested-list " - g h") (bullet-string->nested-list " - g h - i j") (bullet-string->nested-list " - d e f - g h") (define example "- a - b c - d e f - g h") (bullet-string->nested-list example) -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] Converting bulleted list to s-expression?
Thanks. That's a useful package to know and solves the parsing problem. Now I need to syntax transform the xexpr into my target minimal list. Time to dig into *Fear of Macros*. A. On Saturday, 18 August 2018 12:19:12 UTC+10, Shu-Hung You wrote: > > The markdown package by Greg Hendershott provides a parser that create > HTML like xexprs from a string. The rest would be easy. > > https://docs.racket-lang.org/markdown/index.html > > > > On Fri, Aug 17, 2018 at 8:31 PM, Andrew J > wrote: > > Hi. In a little side project, I'm looking for an simple Racket-y way to > > transform a bulleted list into a tree structure, maybe like sxml. > > > > As an example, this... > > > > - a > > - b c > > - d e f > > - g h > > > > would get transformed into something like this... > > > > '(a (b c) (d e f (g h))) > > > > I can see a few ways to do this, from recursively counting indents, > right > > through to making a mini DSL (à la Beautiful Racket), but I'm wondering > what > > a minimal idiomatic solution might look like? > > > > > > A. > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "Racket Users" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to racket-users...@googlegroups.com . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[racket-users] Converting bulleted list to s-expression?
Hi. In a little side project, I'm looking for an simple Racket-y way to transform a bulleted list into a tree structure, maybe like sxml. As an example, this... - a - b c - d e f - g h would get transformed into something like this... '(a (b c) (d e f (g h))) I can see a few ways to do this, from recursively counting indents, right through to making a mini DSL (à la *Beautiful Racket*), but I'm wondering what a minimal idiomatic solution might look like? A. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.