In Haskell I can write a multi-line statement on a single line, like
do {x<-[1,2];y<-[3,4];return (x,y)}
but in Purescript even a single-statement do-statement with curly braces will not compile. Is there different syntax to achieve this?
No, PureScript has no syntax for doing this kind of thing. Aside from just not using do ;)
[1, 2] >>= \x -> [3, 4] >>= \y -> pure (Tuple x y)
Related
I often see output like
+`col1`col2`col3!(,`a`b`c;,{x+1};,()!()). I suspect it means a table, but wasn't able to find documentation on this syntax.
What does the leading + mean? Could someone provide a link to a reference page?
It's notation in K, the language Q operations are built in. + is the flip operator when used monadically, and internally tables are referred to as Flips.
In Q, flip is built from this and the : operator, which forces the + to be interpreted monadically. Similarly for the where keyword:
q)flip
+:
q)where
&:
I understand that both suppress evaluation of a symbol or expression. But the backtick is used for macro definitions while the apostrophe is used for symbols (among other things). What is the difference, semantically speaking, between these two notations?
Backticks allow for ,foo and ,#foo to interpolate dynamic parts into the quoted expression.
' straight up quotes everything literally.
If there are no comma parts in the expression, ` and ' can be used interchangeably.
A standard quote is a true constant literal and similar lists and list that end with the same structure can share values:
'(a b c d) ; ==> (a b c d)
A backquoted structure might not be a literal. It is evaluated as every unquote needs to be evaluated and inserted into place. This means that something like `(a ,#b ,c d) actually gets expanded to something similar to (cons 'a (append b (cons c '(d)))).
The standard is very flexible on how the implementations solves this so if you try to macroexpand the expression you get many different solutions and sometimes internal functions. The result though is well explained in the standard.
NB: Even though two separate evaluation produces different values the implementation is still free to share structure and thus in my example '(d) has the potential to be shared and if one would use mutating concatenation of the result might end up with an infinite structure.
A parallel to this is that in some algol languages you have two types of strings. One that interpolates variables and one that don't. Eg. in PHP
"Hello $var"; // ==> 'Hello Shoblade'
'Hello $var'; // ==> 'Hello $var'
How should I think of quote in the context of Category Theory? Is quote a monad? What kind of things is it?
I do not think it plays any role in category theory since it does not have anything to do with computation and everything to do with parsing and syntax. It is not a monad.
Imagine you want the string 5 + 1, what do you do? Well you enclose it in double quotes like "5 + 1" in the code and suddenly the result isn't 6 anymore but the string "5 + 1". Is "" something special in category theory? Is it a monad? Don't think so since it tells the compiler to create such a data structure that results in that string. In Haskell "hello" is just fancy syntax sugar for ['H', 'e', 'l', 'l', 'o']. In most languages a string is just a series of consecutive bytes, often an array.
The quote special form performs the same operation syck that '(+ 1 2) isn't an expression anymore, but data. The compiler does (cons '+ (cons '1 (cons '2 '()))) and store the pointer to that for everywhere you have some literal ending with (+ 1 2). Because of that (eq '(1 2) (cdr '(+ 1 2)) might be #t but #f is just as reasonable outcome since the compiler might not optimize for shared structure.
Moving forward you could imagine a fancy language that can dictate how the parser and compiler interpret literals. Almost all languages I know has string literals but if you made code to model complex numbers it would have been cool to say in the code that 3+5i should become tmp1 = make_complex 3 5 and that tmp1 is to be put everywhere the literal 3+5i exists in code. Why should numbers, strings, chars and perhaps regular expressions have special treatment?
Quite new to Swift, compared to Java and C++...I'm just wondering why Swift doesn't remove spaces when compiling code as following:
if x!=10 {...} //I have to add space before and after != to get rid of issue.
Increment like increment++ as well can not be act as increment in For syntax if I don't put a space between increment++ and { of loop block.
As in Java or C++, space and Tab do not make sense in terms of compiling. Is Swift just like Python in the way of consider space or tab as part of code?
Swift does not consider spaces as important, however it uses them when separating characters into lexemes.
Consider the following:
a != 1
a! =1
a!= 1
a!=1
The first one can be compiled because the lexical analysis correctly recognizes lexems a, != and 1, != being an infix operator.
In the second one, the lexical analysis recognizes lexem a with a postfix operator ! and a 1 with a prefix operator =.
The third one is lexem a with a postfix operator != and lexem 1.
The last one is ambiguous because it can be either a! = 1 or a != 1. The compiler decided probably based on operator priority to use a! = 1.
Spaces are ignored but they still have a meaning when distinguishing between ambiguous cases. The same is actually valid in many languages. The fact that you can define your own operators limits a bit your coding style.
To compare, try a+++b in Java or C++. Will it be a++ + b or a + ++b?
The exclamation mark is not only used as not for example. It is also used to unwrap an optional variable.
There is more syntactic difference to other languages.
It is a piece of data definition (comment) from the book How to Design Programs that teaches Scheme-like language and DrRacket IDE. What does #, inside [Maybe #, Letter] mean?
; A HMWord is [List-of [Maybe #, Letter]]
; A [Maybe X] is one of:
; – false
; – X
The source is here:
http://www.ccs.neu.edu/home/matthias/HtDP2e/part_4.html
Looks like a typo to me, I think it should read:
; A HMWord is [List-of [Maybe Letter]]
But then it talks about underlines, although LETTERS does not contain any underline.
So I think here false should take the role of the underline (an unknown letter).