Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the same char n times in Haskell

Tags:

string

haskell

I would like to print a char underlining a String n times, with n the length of the String in Haskell.

How should I do this?

My String is: "Available Chars (x)" and I want to have a char underlining that sentence, which should have exactly the same length as the "Available Chars (x)". But x is an int, so it could be "1" or "10" or "1000" etc.. so the length is variable. I got the length but i didnt know how to print that char as long as the string is...

like image 379
ZelelB Avatar asked Sep 20 '25 12:09

ZelelB


1 Answers

Use replicate:

underline :: String -> String
underline = flip replicate '-' . length

This will give you a string which is n times the character '-' where n is the length of the input string. It is the same as:

underline = map (const '-')

You can then use it like this (if for example yourString = "Available Chars (111)"):

> putStrLn yourString >> putStrLn (underline yourString)
Available Chars (111)
---------------------
like image 73
felix-eku Avatar answered Sep 23 '25 15:09

felix-eku