Skip to main content
added 134 characters in body
Source Link
Simon Bergot
  • 8k
  • 5
  • 37
  • 55

One reason isupdated after question update

I feel that variadic functions are hard in haskell. What would be the type of such a function? a -> b -> c -> ... ? Anotherpython version:

"-g -I{headers_dir} -O2".format(**locals())

is an abuse of the language. So I wouldn't say that just using something like:python really offers local variable interpolation in strings. In fact, few languages have it.

Answering sprintf :: Show awhy =>language StringX ->doesn't [a]have ->feature StringY

feels terribly unsafe in haskell. Remember often comes down to saying that unsafe function should be avoidedthe designers did not feel that it was worth it. In the case of local variable interpolation in strings, because debugging themit is harder thanvery much a niche thing used in other languages focused on command line scripts. Which Haskell is not. The fact that you can do it in python only comes from the (no callstack, less debug toolstoo?) powerful reflection facilities of the language. An alternative could be:

Now why is sprintf :: Show a => String -> [a] -> Either String StringText.InterpolatedString.Perl6

But it so complex to use? It relies on quasiquotations, which is not that much betterpart of the haskell language. Text.Printf provide a nicer solution withIt is a type classghc language extension, but which will also throw an exceptionso you have to declare its usage somewhere. If you exclude the library installation & the language extension declaration, its usage is not really more complex than in case of mismatchyour other examples, and is more safe.

You can actually have some form of typesafe printf with type families ( see fun with types paper part 4)

One reason is that variadic functions are hard in haskell. What would be the type of such a function? a -> b -> c -> ... ? Another is that just using something like:

sprintf :: Show a => String -> [a] -> String

feels terribly unsafe in haskell. Remember that unsafe function should be avoided, because debugging them is harder than in other languages (no callstack, less debug tools). An alternative could be:

sprintf :: Show a => String -> [a] -> Either String String

But it is not that much better. Text.Printf provide a nicer solution with a type class, but which will also throw an exception in case of mismatch.

You can actually have some form of typesafe printf with type families ( see fun with types paper part 4)

updated after question update

I feel that the python version:

"-g -I{headers_dir} -O2".format(**locals())

is an abuse of the language. So I wouldn't say that python really offers local variable interpolation in strings. In fact, few languages have it.

Answering why language X doesn't have feature Y often comes down to saying that the designers did not feel that it was worth it. In the case of local variable interpolation in strings, it is very much a niche thing used in languages focused on command line scripts. Which Haskell is not. The fact that you can do it in python only comes from the (too?) powerful reflection facilities of the language.

Now why is Text.InterpolatedString.Perl6 so complex to use? It relies on quasiquotations, which is not part of the haskell language. It is a ghc language extension, so you have to declare its usage somewhere. If you exclude the library installation & the language extension declaration, its usage is not really more complex than in your other examples, and is more safe.

added 189 characters in body
Source Link
Simon Bergot
  • 8k
  • 5
  • 37
  • 55

One reason is that variadic functions are hard in haskell. What would be the type of such a function? a -> b -> c -> ... ? Another is that just using something like:

sprintf :: Show a => String -> [a] -> String

feels terribly unsafe in haskell. Remember that unsafe function should be avoided, because debugging them is harder than in other languages (no callstack, less debug tools). An alternative could be:

sprintf :: Show a => String -> [a] -> Either String String

But it is not that much better. Text.Printf provide a nicer solution with a type class, but which will also throw an exception in case of mismatch.

You can actually have some form of typesafe printf with type families ( see fun with types paper part 4)

One reason is that variadic functions are hard in haskell. What would be the type of such a function? a -> b -> c -> ... ? Another is that just using something like:

sprintf :: Show a => String -> [a] -> String

feels terribly unsafe in haskell. Remember that unsafe function should be avoided, because debugging them is harder than in other languages (no callstack, less debug tools). An alternative could be:

sprintf :: Show a => String -> [a] -> Either String String

But it is not that much better.

You can actually have some form of typesafe printf with type families ( see fun with types paper part 4)

One reason is that variadic functions are hard in haskell. What would be the type of such a function? a -> b -> c -> ... ? Another is that just using something like:

sprintf :: Show a => String -> [a] -> String

feels terribly unsafe in haskell. Remember that unsafe function should be avoided, because debugging them is harder than in other languages (no callstack, less debug tools). An alternative could be:

sprintf :: Show a => String -> [a] -> Either String String

But it is not that much better. Text.Printf provide a nicer solution with a type class, but which will also throw an exception in case of mismatch.

You can actually have some form of typesafe printf with type families ( see fun with types paper part 4)

Source Link
Simon Bergot
  • 8k
  • 5
  • 37
  • 55

One reason is that variadic functions are hard in haskell. What would be the type of such a function? a -> b -> c -> ... ? Another is that just using something like:

sprintf :: Show a => String -> [a] -> String

feels terribly unsafe in haskell. Remember that unsafe function should be avoided, because debugging them is harder than in other languages (no callstack, less debug tools). An alternative could be:

sprintf :: Show a => String -> [a] -> Either String String

But it is not that much better.

You can actually have some form of typesafe printf with type families ( see fun with types paper part 4)