[racket] View HTML source code in racket

From: Eli Barzilay (eli at barzilay.org)
Date: Thu Jun 24 12:16:06 EDT 2010

On Jun 23, Greg Hendershott wrote:
> (define (find-elements xexpr tag-name)
>   ;; Given an x-expression return a list of all the elements starting
> with tag-name.
>   (if (empty? xexpr)
>       '()
>       (let ([x (first xexpr)])
>         (if (and (list? x) (not (empty? x)))
>             (if (and (symbol? (first x)) (symbol=? (first x) tag-name))
>                 (append (list x) ; found one!
>                         (find-elements x tag-name) ; drill down
>                         (find-elements (rest xexpr) tag-name)) ; iterate across
>                 (append (find-elements x tag-name) ; drill down
>                         (find-elements (rest xexpr) tag-name))) ; iterate across
>             (find-elements (rest xexpr) tag-name))))) ; iterate across

`match' is more convenient for such things:

  (define (find-links xexpr)
    (match xexpr
      [(list 'a _ ...) (list xexpr)]
      [(list xs ...) (append* (map find-links xs))]
      [_ '()]))

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.