How To Insert Into List in Haskell

by|inArticles||3 min read
Lists in Haskell<br>
Lists in Haskell<br>

Inserting an element into a list is a common operation in functional programming languages like Haskell. Haskell, being a purely functional language, treats lists as immutable data structures. This means that when you "insert" an element into a list, you actually create a new list with the new element included. Here's a guide on how to insert into a list in Haskell:

Understanding Lists in Haskell

In Haskell, lists are a fundamental data structure. A list of elements of type a is written as [a]. For example, a list of integers is [Int]. Lists are homogeneous, meaning all elements must be of the same type.

Basic Insertion Functions

Haskell provides several built-in functions to work with lists:

Cons Operator (:)

The cons operator is used to prepend an element to the front of a list. For example, x : xs creates a new list with x as the first element and xs as the remaining list:

let newList = 3 : [4, 5] -- newList is now [3, 4, 5] 

Append Function (++)

The append function concatenates two lists. To append an element, you can append a singleton list.

let newList = [1, 2] ++ [3] -- newList is now [1, 2, 3] 

Inserting at a Specific Position

To insert an element at a specific position, you need to define a function. Haskell does not have a built-in function for this specific purpose. Here's a simple implementation:

insertAt :: a -> Int -> [a] -> [a]
insertAt x k ys = as ++ (x:bs)
  where (as,bs) = splitAt k ys

In this function, insertAt x k ys inserts x at position k in the list ys. The splitAt function splits the list into two parts, and then the element is inserted between them.

Using List Comprehensions

List comprehensions in Haskell can also be used for more complex insertion tasks. For example, you can create a new list where an element is conditionally inserted based on some criteria.

Example: Inserting Elements Based on a Condition

Suppose we want to insert the number 99 into a list of integers, but only at positions where the existing element is even. Here's how we can do it using list comprehension:

insertIfEven :: [Int] -> [Int]
insertIfEven xs = [ if even x then 99 else x | x <- xs ]

In this example, insertIfEven is a function that takes a list of integers (xs). The list comprehension [ if even x then 99 else x | x <- xs ] goes through each element x in the list xs. For each element, it checks if x is even using the even function. If x is even, it replaces x with 99; otherwise, it leaves x as is.

Testing the Function

To see this function in action, you can load it into a Haskell interpreter like GHCi and then call the function with a test list:

-- Load this function into GHCi and then test it
testList = [1, 2, 3, 4, 5, 6]
result = insertIfEven testList
-- result will be [1, 99, 3, 99, 5, 99]

In this test, insertIfEven is applied to testList. The function replaces every even number in testList with 99, resulting in [1, 99, 3, 99, 5, 99].


This example demonstrates the elegance and succinctness of list comprehensions in Haskell. By using a conditional expression within the comprehension, you can concisely specify complex list transformations. List comprehensions are especially useful for filtering, mapping, and combining elements of lists based on various criteria.

Custom Insertion Functions

You can also write more complex insertion functions, such as inserting in a sorted list while maintaining the order. These functions will depend on your specific requirements and understanding of Haskell's recursion and higher-order functions.


  • Immutability: Remember that in Haskell, lists are immutable. Insertion functions create and return new lists.
  • Performance: Be mindful of the performance implications. Prepending an element is generally more efficient than appending or inserting at a specific index.


Inserting elements into a list in Haskell is straightforward once you understand the basic principles of list manipulation in functional programming. The key is to familiarize yourself with Haskell's standard library functions and to practice writing custom functions for specific use cases. As you get more comfortable with Haskell's syntax and functional paradigms, these operations become second nature.

Thank you for reading this far! Let’s connect. You can @ me on X (@debilofant) with comments, or feel free to follow. Please like/share this article so that it reaches others as well.

Related Articles

© Copyright 2024 - - All rights reservedVer. 415