Capis_O
Capis_O

Reputation: 21

how to make HTML from a list in scheme, racket

This is a very long question ... I am new and joined, so please don't attack me. Apologies for my bad communications in English. I have some defintions:

An HTML(H) is one of

A Tag is

I want to use mutual recursion,make the HTML into real HTML code. For example,

(list 'html (list 'head (list 'title "Hi")) (list 'body (list 'h1 "Welcome") "Text")) 

Turns into:

"<html><head><title>Hi</title></head><body><h1>Welcome</h1>Text</body></html>"

This should work for any list or string. There are rules for turning it into HTML:

If they enter a string, "Hello", then it does not convert anything. I wrote code here, but it does not work ...

(define (html->string html)
  (cond [(string? html) html]
    [else (append (list "<" (first html) ">") (change-tag (rest html)) (list "</" (first html) ">"))]))


(define (change-tag lst)
  (cond [(empty? lst) empty]
    [else (append (html->string (list (first lst)))
                  (html->string (list (rest lst))))]))

If I enter something like:

(list 'html (list 'head (list 'title "Hi")) (list 'body (list 'h1 "Welcome") "Text")) 

Then it gives me:

(list "<" 'html ">" "<" (list 'head (list 'title "Hi")) ">" "</" (list 'head (list 'title "Hi")) ">" "<" (list  (list 'body  (list 'h1 "Welcome").....etc etc

It is a very long output and it does not work. How to be able to fix this?

Upvotes: 2

Views: 394

Answers (1)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236024

You have the right idea, but still - there are three main issues with your proposed solution:

  • The output will be a string, not a list, so we must append strings in the recursive steps and return strings in the base cases.
  • Symbols must be explicitly converted to strings if we want to concatenate them with other strings.
  • And last but not least: you're missing a couple of base cases, and these are essential for writing a correct solution: what should happen if the given html is an empty list? What if the first element in a list is not a symbol, but another list?

This will work, take a careful look at the things that changed:

(define (html->string html)
  (cond [(empty? html) ""]
        [(string? html) html]
        [(not (symbol? (first html)))
         (html->string (first html))]
        [else (string-append
               "<" (symbol->string (first html)) ">"
               (change-tag (rest html))
               "</" (symbol->string (first html)) ">")]))

(define (change-tag lst)
  (cond [(empty? lst) ""]
        [else (string-append
               (html->string (first lst))
               (html->string (rest lst)))]))

It works as expected:

(define html
  (list 'html
        (list 'head
              (list 'title "Hi"))
        (list 'body
              (list 'h1 "Welcome")
              "Text")))

(html->string html)
=> "<html><head><title>Hi</title></head><body><h1>Welcome</h1>Text</body></html>"

Upvotes: 3

Related Questions