tyro
You can prepend to a list with:
[item | list]
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?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
minhajuddin
There is
List.insert_attyro
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!
peerreynders
As far as I can tell:
[ 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.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.
tyro
Thanks.
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!
Qqwy
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.
I think this is at least a partial reason.
peerreynders
Erlang Master Class 1: Video 8 - Discussion:
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.
Qqwy
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.
peerreynders
I was thinking IOLists could be improper - but that may have been a bad assumption on my part.
rvirding
I think you will find that the cons operator in Haskell
x : xsbehaves the same way as[x | xs]in Elixir/Erlang both in constructing and in patterns. The difference is only in the syntaxmguimas
You can do something like this
Then you can use it like this
Note that because
::as other uses in Elixir (in bitstrings and type specs) you cannot importKernelExIf you prefer the name
consinstead of::then typeand do
Better than this would be to have
consout-of-the-box in theKernelmodule.