Skip to main content
TopMiniSite

Back to all posts

How to Print the Same Characters In A Row In Haskell?

Published on
5 min read
How to Print the Same Characters In A Row In Haskell? image

Best Haskell Programming 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 USED BOOKS FOR EVERY READER.
  • ECO-FRIENDLY CHOICE: RECYCLED LITERATURE FOR SUSTAINABLE LIVING.
  • GREAT SELECTION: FIND POPULAR TITLES AND HIDDEN GEMS EASILY!
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

  • AFFORDABLE PRICES: GET QUALITY BOOKS WITHOUT BREAKING THE BANK!
  • ECO-FRIENDLY CHOICE: SUPPORT RECYCLING BY BUYING USED BOOKS!
  • UNIQUE FINDS: DISCOVER RARE TITLES NOT AVAILABLE IN STORES!
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
7 Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming

BUY & SAVE
$25.83 $44.99
Save 43%
Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming
8 Haskell in Depth

Haskell in Depth

BUY & SAVE
$57.13 $59.99
Save 5%
Haskell in Depth
9 Introduction to Computation: Haskell, Logic and Automata (Undergraduate Topics in Computer Science)

Introduction to Computation: Haskell, Logic and Automata (Undergraduate Topics in Computer Science)

BUY & SAVE
$26.40 $37.99
Save 31%
Introduction to Computation: Haskell, Logic and Automata (Undergraduate Topics in Computer Science)
10 Learn Haskell by Example (Bookcamp)

Learn Haskell by Example (Bookcamp)

BUY & SAVE
$51.84 $59.99
Save 14%
Learn Haskell by Example (Bookcamp)
+
ONE MORE?

To print the same characters in a row in Haskell, you can use the [replicate](https://ubuntuask.com/blog/how-to-replicate-numpy-choose-in-tensorflow) function to create a list of the desired characters and then concatenate them using the concat function.

Here's a simple example that prints the character 'a' ten times in a row:

main :: IO () main = do let characters = replicate 10 'a' putStrLn $ concat characters

In this example, replicate 10 'a' creates a list ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'], and concat is used to concatenate these characters into a single string. Finally, putStrLn is used to print the string to the console. You can replace 'a' and 10 with any characters and number, respectively, to print different characters and different number of times.

How to print characters in reverse order in Haskell?

There are multiple ways to print characters in reverse order in Haskell. Here are a few examples:

  1. Using Data.List module:

import Data.List (reverse)

printReversed :: String -> IO () printReversed str = putStrLn (reverse str)

main :: IO () main = do let str = "Hello, World!" printReversed str

  1. Using recursion:

printReversed :: String -> IO () printReversed [] = putStrLn "" printReversed (x:xs) = do printReversed xs putChar x

main :: IO () main = do let str = "Hello, World!" printReversed str

  1. Using list comprehension:

printReversed :: String -> IO () printReversed str = putStrLn [str !! i | i <- [length str - 1, length str - 2..0]]

main :: IO () main = do let str = "Hello, World!" printReversed str

All three approaches will print the characters of the input string in reverse order. In the first two approaches, the printReversed function takes a string as input and recursively prints the characters in reverse order. The last approach uses a list comprehension to create a new string with characters in reverse order and then prints it.

How to sort characters in a string alphabetically in Haskell?

One way to sort characters in a string alphabetically in Haskell is to convert the string into a list of characters, sort the list, and then convert it back into a string. Here is an example of how this can be done:

import Data.List (sort)

sortString :: String -> String sortString = sort . filter (/= ' ')

main :: IO () main = do input <- getLine let sortedString = sortString input putStrLn sortedString

In this example, the sortString function takes a string as input and performs the following steps:

  1. It first removes any spaces from the input string using the filter function.
  2. It then converts the string into a list of characters using Haskell's built-in String type which is just an alias for [Char].
  3. It applies the sort function from the Data.List module to sort the list of characters alphabetically.
  4. Finally, it converts the sorted list back into a string.

The main function reads a line of input from the user, applies sortString to it, and then prints the sorted string using putStrLn.

How to trim leading and trailing whitespace from a string in Haskell?

In Haskell, you can trim leading and trailing whitespace from a string using functions from the Data.Text module. Here's an example of how you can do it:

import Data.Text (Text, pack, unpack, strip)

trim :: String -> String trim = unpack . strip . pack

In this example, we first convert the input string into a Text value using the pack function. Then, we use the strip function to remove the leading and trailing whitespace from the Text value. Finally, we convert the trimmed Text value back to a string using the unpack function.

You can then use the trim function to trim leading and trailing whitespace from any string:

main :: IO () main = do let str = " Hello, World! " let trimmedStr = trim str putStrLn trimmedStr

Output:

Hello, World!

Note that the Data.Text module provides a more efficient and performant implementation for working with textual data than using the standard String type.

How to check if a character is a digit in Haskell?

In Haskell, you can use the isDigit function from the Data.Char module to check if a character is a digit. Here's an example usage:

import Data.Char

isDigitChar :: Char -> Bool isDigitChar c = isDigit c

In this example, the isDigitChar function takes a single character c as input and checks whether it is a digit using the isDigit function. The result of isDigit c is returned, indicating whether c is a digit or not.

You can test this function in GHCi or in your Haskell program as follows:

main :: IO () main = do putStrLn $ show (isDigitChar '2') -- prints "True" putStrLn $ show (isDigitChar 'a') -- prints "False"

The show function is used to convert the Bool result to a String before printing it to the console.