9

How do I convert a value of type List to a String in Elm?

Basically I'm looking for a function with the signature a -> String or List -> String.

Example

Let's say I have a function intAverage:

intAverage l = case l of
  [] -> 0
  otherwise -> Debug.log (<<SHOW_FUNCTION>> l) (List.sum l // List.length l)

Here I want to inspect the list, in order to understand what's being passed to my function. Debug.log expects a String which makes me look for a function with the signature a -> String or List -> String but I have been unsuccessful in finding such a function in the Elm package docs.

Haskell has Debug.traceShow (which is simply an application of the function show on the first argument of Debug.trace) but I can't find the equivalent in Elm.

qff
  • 5,524
  • 3
  • 37
  • 62

2 Answers2

7

Edit: This is no longer true as of Elm version 0.19. See the other answer to this question.

The toString was what I was looking for, but couldn't find.

toString :: a -> String

I found it in the Basics-package: toString documentation

qff
  • 5,524
  • 3
  • 37
  • 62
4

On Elm 0.19, it's been moved to Debug.toString:

For example:

> Debug.toString [1,2,3]
"[1,2,3]" : String
Milad
  • 836
  • 7
  • 13