Skip to main content
St Louis

Back to all posts

How to Replace A Letter In A String With Haskell?

Published on
4 min read
How to Replace A Letter In A String With Haskell? image

Best String Replacement Guides to Buy in October 2025

1 Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming

BUY & SAVE
$55.05 $57.95
Save 5%
Effective Haskell: Solving Real-World Problems with Strongly Typed Functional Programming
2 Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell

BUY & SAVE
$34.72 $49.99
Save 31%
Learn Physics with Functional Programming: A Hands-on Guide to Exploring Physics with Haskell
3 Programming in Haskell

Programming in Haskell

BUY & SAVE
$42.99 $47.00
Save 9%
Programming in Haskell
4 Learn You a Haskell for Great Good!: A Beginner's Guide

Learn You a Haskell for Great Good!: A Beginner's Guide

  • AFFORDABLE PRICES ON QUALITY PRE-OWNED READS.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY BUYING USED!
  • THOROUGHLY INSPECTED FOR QUALITY AND READABILITY.
BUY & SAVE
$35.00 $44.95
Save 22%
Learn You a Haskell for Great Good!: A Beginner's Guide
5 Real World Haskell

Real World Haskell

  • QUALITY ASSURANCE: EACH BOOK IS VETTED FOR GOOD CONDITION AND READABILITY.
  • ECO-FRIENDLY CHOICE: SAVE MONEY AND THE ENVIRONMENT WITH USED BOOKS.
  • WIDE SELECTION: DISCOVER HIDDEN GEMS AT A FRACTION OF THE ORIGINAL PRICE.
BUY & SAVE
$24.40 $49.99
Save 51%
Real World Haskell
6 Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up

Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up

BUY & SAVE
$45.99
Soar with Haskell: The ultimate beginners' guide to mastering functional programming from the ground up
+
ONE MORE?

To replace a letter in a string with Haskell, you can utilize a combination of functions and patterns. Here's an example:

replaceLetter :: Char -> Char -> String -> String replaceLetter _ _ [] = [] -- Base case for empty string replaceLetter old new (x:xs) | x == old = new : replaceLetter old new xs -- Replacing old letter with new | otherwise = x : replaceLetter old new xs -- Keeping the current letter in the string

In this example, the replaceLetter function takes three arguments: the old letter that you want to replace, the new letter that will replace the old letter, and the String in which you want to perform the replacement.

The function uses recursion to iterate through the characters in the input string. If the current character (x) matches the old letter, it replaces it with the new letter. Otherwise, it keeps the current character as it is.

Here's how you can use the function:

main :: IO () main = do let originalString = "Hello, world!" modifiedString = replaceLetter 'o' '0' originalString putStrLn modifiedString

This will output: Hell0, w0rld!

What is string replacement in Haskell?

String replacement in Haskell refers to the process of replacing a substring within a given string with another substring. It involves finding a specific substring pattern and replacing it with a different string. Haskell provides several functions and libraries to perform string replacement, such as replace function from the Text.Regex module or the sub function from the Text.Regex.Posix module. These functions allow you to specify the pattern to search for, the replacement string, and the input string on which the replacement is to be performed.

What is the difference between a string and a character in Haskell?

In Haskell, a string is a sequence of characters, while a character represents a single character in a specific character set.

A string in Haskell is represented as a list of characters, where each character is enclosed in single quotation marks (''). For example, "Hello" is a string in Haskell.

On the other hand, a character in Haskell is represented using a single quotation mark (''). For example, 'H' or 'a' are characters in Haskell.

Note that characters are of type Char, while strings are lists of characters and have type [Char].

Here is an example to illustrate the difference:

-- Character myChar :: Char myChar = 'A'

-- String myString :: [Char] myString = "Hello"

In the above example, myChar is a character variable containing the uppercase letter 'A', while myString is a string variable containing the sequence of characters "Hello".

What is the function for case-insensitive letter replacement in Haskell?

There is no built-in function for case-insensitive letter replacement in Haskell. However, you can define a custom function to achieve this. Here's an example implementation using the Data.Text module from the text package:

import qualified Data.Text as T

replaceCaseInsensitive :: Char -> Char -> T.Text -> T.Text replaceCaseInsensitive from to = T.pack . map replaceChar . T.unpack where replaceChar c | c == from = to | c == toUpper from = toUpper to | c == toLower from = toLower to | otherwise = c

-- Usage example main :: IO () main = do let text = "Hello, World!" replacedText = replaceCaseInsensitive 'h' 'X' (T.pack text) putStrLn $ T.unpack replacedText -- Output: "Xello, World!"

In this example, replaceCaseInsensitive takes a from character and a to character, along with the text where the replacement should occur. It converts the text to a list of characters using T.unpack, performs the case-insensitive replacement using map replaceChar, and converts the result back to Text using T.pack. The replaceChar function checks if the character matches the from character, its uppercase version (toUpper from), or its lowercase version (toLower from), and replaces it accordingly. If none of the conditions match, it leaves the character unchanged.