tyro

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?

Showing Posts 1 to 10

minhajuddin

minhajuddin

There is List.insert_at

iex(3)> (1..10) |> Enum.to_list |> List.insert_at(0, :foo)
[:foo, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
tyro

tyro OP

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! :stuck_out_tongue:

peerreynders

peerreynders

As far as I can tell:

  • 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.

tyro

tyro OP

Thanks.

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! :slight_smile:

Qqwy

Qqwy

TypeCheck Core Team

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

peerreynders

Erlang Master Class 1: Video 8 - Discussion:

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.

Qqwy

Qqwy

TypeCheck Core Team

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

peerreynders

I was thinking IOLists could be improper - but that may have been a bad assumption on my part.

rvirding

rvirding

Creator of Erlang

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

mguimas

mguimas

You can do something like this

defmodule KernelEx do
  defmacro left :: right do
    quote do
      [unquote(left) | unquote(right)]
    end
  end
end

Then you can use it like this

iex(1)> require KernelEx
KernelEx
iex(2)> 1 |> KernelEx.::([2, 3])
[1, 2, 3]

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

and do

iex(4)> import KernelEx
KernelEx
iex(5)> 1 |> cons([2,3])
[1, 2, 3]

Better than this would be to have cons out-of-the-box in the Kernel module.

Where Next? Top

Trending in Questions Top

RSP87
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
kpanic
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
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 Top

GenericJam
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
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews