50

What is Haskell's equivalent of

string str = string.Format("{0} {1}",10,20); // C#
1
  • How to do {0, 10}? Commented Apr 28, 2020 at 18:46

5 Answers 5

56

There is a Printf module in GHC.

import Text.Printf
str :: String
str = printf "%d %d" 10 20

however it is probably simpler to just do

str = show 10 ++ " " ++ show 20
Sign up to request clarification or add additional context in comments.

6 Comments

Its not working I got ERROR filename.hs:3:Cannot justify constraints in explicitly typed binding. I am using WinHugs.
You should switch to GHC. Hugs is unmaintained, slow, and supports very few of the packages on Hackage. It isn't part of the Haskell Platform specification either.
Text.Printf.printf returns an IO action (IO ()) when fully applied, not a String.
@SwiftsNamesake: It also returns String. It can return any type that is PrintfType.
anything like python's f-string?
|
30

You could use the format function provided by the text-format-simple package:

import Text.Format
format "{0} {1}" [show 10, show 20]

This function has the signature:

format :: String -> [String] -> String

So all you need is provide your parameters as strings.
Another example:

format "Some {0} believes that 1 + 1 = {1}." ["people",show 10]

3 Comments

I'd recommend /not/ using that package, as it's very constrained in what it can do. I'm working on a package named text-format which is generally more capable, and already quite usable for this purpose.
Dmitry, all the packages you listed save for double-conversion are included in the Haskell Platform. The vast majority of users won't have to install that much. You argument is still valid though.
@BryanO'Sullivan Thanks for the pointer. I've had a look at your package, and while it looks much more useful than the alternatives (and has a type for the format specifier rather than plain string, which seems like an extremely good idea) unfortunately for me it produces Data.Text as its output, rather than String. As I need to integrate with existing code that relies on String, this would require an unnecessary conversion for me. Is there an alternative that produces a String output and supports a format specifier that allows you to change the order of fields in the output?
14

Putting answer here in case somebody searching for formatting libraries in Haskell on StackOverflow. There's type-safe and fast formatting library called fmt now. With it you can write code like this:

> "There are "+|n|+" million bicycles in "+|city|+"."

Comments

7

Is this what you are looking for?

printf "%d %d" 10 20

See Text.Printf.

2 Comments

This answer does not work for me: Ambiguous type variable ‘t0’ arising from a use of ‘printf’ prevents the constraint ‘(Text.Printf.PrintfArg t0)’ from being solved.
@Julia Works for me, perhaps you forgot to import the module?
3

Alternatively you could write

unwords [show 10, show 20]

or even

unwords (map show [10, 20])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.