What's the meaning of the backslash in \[1,2] with pack Markdown?

Hi there! I’m new to prolog and having a lot of fun with it. While working with the markdown library I sometimes get structures using a backslashed-list, like \[1,2], and I’m curious what sort of type this represents. What I found out is that it’s a compound term but not compatible with regular lists. I’ve not had much luck Googling it (maybe because \[ isn’t much of a search term).

If anyone could point me in the right direction I’d super appreciate it! I’m also curious if there’s a function I could have used on a type like this that would have given me a name for it that I could have looked up.

Thank you!

I have never used the pack - markdown so this is a pure guess.

In the documentation is

The specification for the parser was taken from Daring Fireball: Markdown Syntax Documentation (Gruber’s Markdown).

The link notes backslash escapes

However since you did not give a much larger example and one that shows the input and converted output is hard to know if this is correct.

1 Like

Hi, thanks for your help and sorry for not giving a better example! The simplest example I can create is this one:

?- md_parse:md_parse_string("Hi", Blocks).
Blocks = [p([\["Hi"]])].

There’s no special formatting in the input string so the contents get parsed into a paragraph (p) block. That all makes sense to me. Then there’s a list inside the paragraph and the weird escaped list structure inside of that.

The reason I didn’t give a more comprehensive markdown example was because I assumed the backslashed list is a more general concept, not unique to the markdown library. Some investigations in a swipl session that doesn’t import the markdown module:

% It's doesn't look like an atom with an escaped `[` in it, or I could do:
?- append([1], \[2, X).
ERROR: Syntax error: Illegal start of term
ERROR: append([1], \[2,
ERROR: ** here **
ERROR: X) .

% It's not a regular list, else I would be able to append it:
?- append(\[1], [2], X).
false.

To figure this out I actually installed the Markdown pack then recreated what you did.

In reading the documentation for for the Markdown pack at GitHub it demonstrates two different predicates,

md_parse_string/2 which was in your question and
md_html_string/2 which was not in your question.

Upon trying this and seeing that

?- md_html_string("# Hello #",Html).
Html = "\n\n<h1>Hello</h1>\n\n".

generates valid HTML and

?- md_parse:md_parse_string("Hi", Blocks).
Blocks = [p([\["Hi"]])].

generates a list ready for use with html//1 it became much clearer.

If all you want to do is create Markdown from an input string then use

md_html_string/2

If you need to create markdown that will be embedded with other input to html//1 to create some html then use md_parse_string/2.

Since md_parse_string/2 created a list ready for html//1 the understanding of the \ is learned by reading the documentation for html//1 which notes

\ List
Escape sequence to add atoms directly to the output list. This can be used to embed external HTML code or emit script output. List is a list of the following terms:

  • Fmt - Args
    Fmt and Args are used as format-specification and argument list to format/3. The result is added to the output list.
  • Atomic
    Atomic values are added directly to the output list.

However that is different from

\ Term
Invoke the non-terminal Term in the calling module. This is the common mechanism to realise abstraction and modularisation in generating HTML.

which is noted in this question

What is this? \css and \js Something with library(http/http_server)?

HTH


In checking the GitHub documentation for the pack Markdown, there are references to html/1 which is not a standard SWI-Prolog predicate. However html//1 is a standard SWI-Prolog library predicate.

1 Like