如何从方案中的列表中制作 HTML,球拍 [英] how to make HTML from a list in scheme, racket

查看:54
本文介绍了如何从方案中的列表中制作 HTML,球拍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很长的问题......我是新加入的,所以请不要攻击我.为我糟糕的英语交流道歉.我有一些定义:

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:

HTML(H) 是其中之一

An HTML(H) is one of

  • Str
  • 标签

标签是

  • (cons Sym (listof H))

我想使用相互递归,把HTML变成真正的HTML代码.例如,

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")) 

变成:

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

这应该适用于任何列表或字符串.有将其转换为 HTML 的规则:

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

  • 字符串不需要转成 HTML.
  • 每个标签(来自定义)以 开头,以 结尾.所以 .

如果他们输入字符串Hello",则它不会转换任何内容.我在这里写了代码,但它不起作用......

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))))]))

如果我输入以下内容:

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

然后它给了我:

(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?

推荐答案

您的想法是正确的,但仍然 - 您提出的解决方案存在三个主要问题:

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

  • 输出将是一个字符串,而不是一个列表,因此我们必须在递归步骤中附加字符串并在基本情况下返回字符串.
  • 如果我们想将符号与其他字符串连接起来,必须将它们显式转换为字符串.
  • 最后但并非最不重要的一点:您遗漏了几个基本情况,这些对于编写正确的解决方案至关重要:如果给定的 html 是空列表,会发生什么情况?如果列表中的第一个元素不是符号而是另一个列表怎么办?
  • 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)))]))

它按预期工作:

(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>"

这篇关于如何从方案中的列表中制作 HTML,球拍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆