3 ms·
What's a "quasi-quoter?"
by millstone 12y ago
What's a "quasi-quoter?"
- dons 12y agoSyntax macros.
- a-saleh 12y agoIt is a way to create embedded DSLs in haskell. If I understand correctly, when haskell compiler sees [foo|some dsl language inside|] it knows, that it should use parser asigned to foo on string inside of quasi-quotes. Long time ago, I tried learning this using this tutorial http://quasimal.com/posts/2012-05-25-quasitext-and-quasiquoting.html http://quasimal.com/posts/2012-05-25-quasitext-and-quasiquot...
- chriswarbo 12y agoA quoted value is exactly what is written, for example in LISP: (let ((foo "hello")) (quote (My name is foo))) > (My name is foo) An unquoted value is interpreted completely: (let ((foo "hello")) (My name is foo)) > Error: "My" is not a function A quasiquoted value is literal by default (like a quoted value), but we can selectively "unquote" parts, to have them interpreted: (let ((foo "hello")) (quasiquote (My name is (unquote foo)))) > (My name is "hello") As you can see, in LISP we can use quasiquotes on pretty much anything. Most other languages with quasiquotes only support it in strings; for example in PHP ' is the quotation mark and " is the quasiquotation mark: $foo = "hello"; echo 'My name is $foo'; // Gives 'My name is $foo' echo "My name is $foo"; // Gives "My name is hello"; Depending on the language, some require explicit unquoting (eg. ${} syntax), others require explicit escaping (eg. \$ syntax). The article uses two forms of quasiquotation marks: [r| and |], and [l| and |] For example: person :: Person person = [r| {name = "Yuri Alekseyevich Gagarin", birthday = {year = 1934, month = 3, day = 9}, country = {name = "Soviet Union", language = "Russian"}} |] Here the value of "person" is quasiquoted, so some parts of its contents (eg. the "{", "}", "," and "=" syntax, along with the "name, "birthday", "year", "month", "day", "country" and "language" tokens) will take their 'literal' values in the record-building language. The other values, '"Yuri Alekseyevich Gagarin"', "1934", "3", "9", '"Soviet Union"' and '"Russian"' will be interpreted as Haskell values, and the results (various Strings and Nums) will be used in the record-building language, instead of their literal tokens.