Elm syntax help required - elm

Could you please explain me what does mean in Elm next statement?
(=>) = (,)
I found it in the example of Elm architecture tutorial

This is infix notation. In effect, this defines a function
(=>) = \a b -> (a,b)
Infix is then used without the brackets, so
a => b == (a,b)

Related

Higher-Order Functions in K?

Is it possible to write higher-order functions in K?
Particularly, things like Map and Fold, where I traverse a structure and apply a function to every element.
For example, if I have a list:
A ~> B ~> C
and I want to map a function F to every element of the list:
F A ~> F B ~> F C
Or maybe produce a fold:
F A (F B (F C))
where F is [function, functional] and is meant to evaluate, not be there syntactically.
If you know is advance what functions you want to be using in a higher order fashion, you can approximate this with something like:
syntax MapFunction ::= "foo"
syntax KItem ::= MapFunction "(" KItem ")" [function]
rule foo(...) => ...
syntax List ::= map(List, MapFunction) [function]
rule map(.List, _) => .List
rule map(ListItem(K) L, F) => ListItem(F(K)) map(L, F)
I recognize this is not ideal since it's not really a higher order function and you have to be explicitly aware of every function you intend to use this way and write it in a different fashion, but it's a decent workaround if you just need something quick and dirty in a couple places in the semantics.
As for the rest of your question, we would love to support higher order functions more generally, but we do not yet.

Why doesn't elm use parenthesis?

Learning elm but don't get what author means by the below:
The reason we can avoid writing the parenthesis is because function
application associates to the left.
Any values or functions, specified after the function name, will be associated with the function as it's arguments automatically, that's really all it means.
In language, like JavaScript, you can explicitly distinguish the usage of a function, as an expression:
function foo (message) {
return message
}
console.log(foo) // Function as expression.
console.log(foo('Hello')) // Function application with result: "Hello"
In Elm this behaviour does not require parentesis.
foo message =
message
foo -- Function as expression.
foo "Hello" -- Function application with result: "Hello"
It's not like in JavaScript at all, when you want to apply the function and do something with result. Here you will have to tell the compiler explicitly, that (foo "Hello") is a single argument for String.toUpper
String.toUpper (foo "Hello") -- "HELLO"
The parentheses in question is ((divide 5) 2). My interpretation of that sentence is that you can write ((divide 5) 2) as divide 5 2 because divide 5 2 is evaluated from the left first, i.e. divide 5 -> divide5 then divide5 2 -> 2.5.
Though I can't see how else it could be evaluated! Neither 5 2 nor divide 2 then divide2 5 make sense.

Prolog - Using dynamic with asserts

I'm new to Prolog and I'm having a hard time using a dynamic predicate.
First, here's the code I'm executing
:- dynamic(list/1).
add(X, LL) :- asserta(list([])), asserta(list(X)), retract(list(LL)).
I know the code looks weird, but I'm simply looking for the right syntax to use.
Now, if I do :
add(2, LL).
Answer will be :
LL = 2 ;
LL = [].
But what I want to do is to add the X (2) INTO the array ([]). So..
LL = [2].
It looks simple (probably is to), but I can't get it work.
Thanks a lot.
If you want to add X to the front of the list:
add(X, LL) :-
( retract(list(Prev))
-> LL = [X|Prev]
; LL = [X]
),
asserta(list(LL)).
But I agree with #jschimpf's advice. Assert/retract should only be used under certain circumstances as may be quite in efficient in some applications.

Are there any right associative short-circuit operators

I'm working on a interrupter the lets one define their own operators. The goal then is to take an AST that looks like exp op exp op exp and turn it into either exp op (exp op exp) or (exp op exp) op exp based on the relative precedence and associativity of the two operators. The language is dynamic so the only way to know what version of the operator to use is to evaluate the first expression and ask it what version of op to use.
On the other hand, it is important that we not evaluate the second expression because if op is || (as commonly used) then we should be able to short-circuit if the first exp is false.
a problem would arise if some operator were both right associative and short-circuiting. My question is are there any right associative, short-circuiting operators in common use (for a chosen value of "common")?
N.b. assignment is handled separately by the parser so = is not an operator and a (op)= b is syntactic sugar for a = a op b.
Boolean implication might be.
I would probably read
a → b → c
as "a implies that b implies c" which would suggest that it should parenthesize
a → (b → c)
and boolean implication should probably be short-circuiting since when a is false then the right side of (a → b) is irrelevant to the result.

What is the name of a [foo, bar] = ["foo", "bar"] feature?

I need to know a correct name for this cool feature that some languages provide.
FYI: In some languages it is possible to do a multiple assignments by assigning a structure of values to a structure of "variables". In the example in the question title it assigns "foo" to foo and "bar" to bar.
It's generally called destructuring bind in functional languages (which don't have assignments) and destructuring assignment in imperative languages.
Some languages provide subsets of that feature and then call it something different. For example, in Python it works with Tuples, Lists or Sequences and is called Tuple unpacking, List unpacking or Sequence unpacking, in Ruby, it works with Arrays (or objects that are convertible to an array) and is called parallel assignment.
Destructuring bind can get arbitrarily complex. E.g. this (imaginary) bind
[Integer(a), b, 2, c] = some_array
would assign the first element of some_array to a, the second element to b and the fourth element to c, but only if the first element is an Integer, the third element is equal to 2 and the length is 4. So, this even incorporates some conditional logic.
Destructuring bind is a subset of more general pattern matching, which is a standard feature of functional languages like Haskell, ML, OCaml, F#, Erlang and Scala. The difference is that destructuring bind only lets you take apart a structure and bind its components to variables, whereas pattern matching also matches on values inside those structures and lets you make decisions and in particular lets you run arbitrary code in the context of the bindings. (You can see the above imaginary bind as half-way in between destructuring bind and pattern matching.)
Here's the classical example of a reverse function in an imaginary language, written using pattern matching:
def reverse(l: List): List {
match l {
when [] { return [] }
when [first :: rest] { return (reverse(rest) :: first) }
}
}
In Python it is known as list or sequence unpacking: http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences
my_list = ["foo", "bar"]
foo, bar = my_list
It's called parallel assignment in Ruby and other languages.
Perl and PHP call it list assignment
Perl:
my ($foo, $bar, $baz) = (1, 2, 3);
PHP:
list($foo, $bar, $baz) = array(1, 2, 3);
If you view the right hand side as a tuple, one could view the assignment as a kind of Tuple Unpacking.
In Erlang it's ... well, it's not assignment, it's pattern matching (seeing as there is no assignment, as such, in Erlang).
$ erl
Erlang R14B (erts-5.8.1) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:true]
Eshell V5.8.1 (abort with ^G)
1> [H1, H2, H3| Rest] = [1,2,3,4,5].
[1,2,3,4,5]
2> H1.
1
3> H2.
2
4> H3.
3
5> Rest.
[4,5]
Why is it called "pattern matching"? Because it actually is matching patterns. Look:
6> [1,2,3,4,A] = [1,2,3,4,5].
[1,2,3,4,5]
7> A.
5
8> [1,2,3,4,A] = [1,2,3,4,6].
** exception error: no match of right hand side value [1,2,3,4,6]
In the first one we did what effectively amounts to an assertion that the list would start with [1,2,3,4] and that the fifth value could be anything at all, but please bind it into the unbound variable A. In the second one we did the same thing except that A is now bound so we're looking explicitly for the list [1,2,3,4,5] (because A is now 5).
Mozilla calls it destructuring assignment. In Python, it's sequence unpacking; tuple unpacking is a common special case.
In Clojure it would be called destructuring. Simple example:
(let [[foo bar] ["foo" "bar"]]
(println "I haz" foo "and" bar))
It's also often used in function definitions, e.g. the following destructures a single point argument into x and y components:
(defn distance-from-origin [[x y]]
(sqrt (+ (* x x) (* y y))))
You can also use the same technique to destructure nested data structures or key/value associative maps.