But is it possible to pipe this operation? I can’t see any way to do this in List/Kernel, and there seems to be very little info about the cons operator in general. I would like to be able to do something like:
list
|> List.prepend(item)
Of course, I could use a custom/anonymous function but is there an inbuilt way to do this?
Thanks. Perhaps I should be clearer that I’m not looking for alternatives (I’m aware of insert_at), I just want to know if this exact feature exists. Cons is one of the most common operations in Elixir, and piping is similarly ubiquitous, so it would seem odd if you can’t use both together!
Erlang doesn’t actually have a cons “operator” but syntax for a cons cell i.e. [ term1 | term2 ] - and it actually has two distinct uses, to construct a cons cell and to match a cons cell. So it makes sense that it is a language feature rather than an operator backed by a function.
Elixir and it’s macro support build on Erlang but the primary objective of the Pipe macro is to take the result of the preceding expression (LHS) and thread it into the first parameter of the next function (RHS) (Clojure actually refers to something like this as the thread first macro rather than a “Pipe”).
So the two simply don’t mix because Erlang’s cons cell predated Elixir and Elixir’s Pipe was designed to work with functions.
Always inquire whether there is an idiomatic way of doing things as commonality makes everybody’s life easier but also realize that sometimes the DIY-as-required approach is often accepted/preferred in order to keep clutter out of the language/platform.
So it makes sense that it is a language feature rather than an operator backed by a function.
I’m not sure I quite understand why. Haskell has the same syntax for cons and pattern matching but has cons as a regular operator. Perhaps there is some reason Erlang went a different route? Maybe it was just too complicated to implement. Would be good to know why!
Elixir, Erland and Prolog use structural pattern matching, while Haskell uses staticly typed pattern matching.
These are similar, but not the same. For instance, in Haskell you cannot re-use the same name twice to say that two things are equal, you will have to use a guard.
FRANCESCO CESARINI: One question I got once is, we’re learning Haskell and you’re giving us a lecture about how Erlang’s being used in the real world. Why are they actually teaching us Haskell, and not Erlang? And a natural answer there was, you’re here to learn how to learn.
I’m being left with the impression that in Haskell functional programming is an end in itself, while in Erlang and Elixir functional programming is merely a means to an end - so Erlang/Elixir seem more practically minded.
Also Erlang (for optimization reasons AFAIK) allows the formation of improper lists with the cons cell.
I actually think that improper lists are allowed because the language is dynamically typed. Because consing is so common, why then add an extra check (that has to happen at runtime as it is dynamically typed) to see if the right hand side is a list?
If I remember correctly, most lisps (that are dynamically typed, just like elixir/erlang) allow improper lists because it is not worth this extra check.
You are totally right that Erlang was a language created for a practical purpose where being functional was a ‘side effect’, while Haskell was first a research language, which focused on practicality in the second place.
I think you will find that the cons operator in Haskell x : xs behaves the same way as [x | xs] in Elixir/Erlang both in constructing and in patterns. The difference is only in the syntax
Note that because :: as other uses in Elixir (in bitstrings and type specs) you cannot import KernelEx
iex(3)> import KernelEx
** (CompileError) iex:8: cannot import KernelEx.::/2 because it conflicts with Elixir special forms
(elixir) src/elixir_import.erl:108: :elixir_import.calculate/6
(elixir) src/elixir_import.erl:23: :elixir_import.import/4
If you prefer the name cons instead of :: then type
defmodule KernelEx do
defmacro cons(left, right) do
quote do
[unquote(left) | unquote(right)]
end
end
end
Regarding cons it can be made a function, sure, but it cannot be inlined outside the module it is defined AFAIK. Correct me if I am wrong, and Erlang / Elixir is already able to inline functions outside their defining modules.