What is equivalent of Haskell 'show'

Hello, I’m experienced in Haskell but fairly new to Elm. I’m also a CS tutor and I want to use it to tutor beginners in functional programming. The sticking point is that Elm tutorials are almost all built around web pages, and fairly sophisticated ones at that. Whereas most of my students have minimal web design experience (they can usually do simple vanilla JS and HTML).

In Haskell, one would begin by working with strings and lists and such, implementing basic algorithms and “showing” the result. (After deriving show.) In Elm, must I write custom code to turn any result into a String or does a module provide this for the Core types?

Also I expect that elm-run is the best way to demonstrate simple concepts before getting into websites. Correct?

Its called Debug.toString.

personally, I would suggest starting straight with a normal Elm program and use

main : Html msg
main =
    Html.text <| Debug.toString <| view

{- this function can have any type you like
-}
view =
    ..

I would use Ellie to share example codes.

If you want to have some IO in your code, then I believe you should start with the basic example that starts in Ellie and go from there. For Css i would recommand using mdgriffith/elm-ui instead.

If you’re teaching fundamentals I’d use elm-test and in particular koans. I just had a quick look and found https://github.com/robertjlooby/elm-koans which is out of date but could be a great starting point for you.

There is no equivalent to Haskell’s ‘show’ in Elm as Elm doesn’t have user implementable type classes and has no concept of ‘deriving’.

Elm does have Debug.log and Debug.toString which can stringify any type by they are restricted to only be used during development as they break the type system. If you want a simple way to convert a value to a string of elm syntax for learning purposes then Debug.toString is what you want.

For production use you need to write custom stringifying functions for each of the types you’d want to stringify.

elm repl can be useful for a simple introduction to Elm but people outgrow it pretty quickly (you can’t learn about the Elm architecture or do any effects) so https://ellie-app.com or elm reactor tend to be a better place for further learning.

3 Likes

You might want to look at litvis, which provides a literate Elm environment for Atom and VSCode. It doesn’t require any HTML, nor does it need to use The Elm Architecture, so can work well for teaching functional concepts (I use it for teaching data visualization in a functional style and have also converted a functional programming teaching module from Haskell into Elm). You can use it to generate any function’s return value as text or formatted markdown. Here’s a simple example for approximating e (view in Litvis to see the output). For a much richer set of literate Elm examples, see these Advent of Code solutions.

Thanks everyone, this is some great stuff! I can start with Debug.string and move toward using litvis with my students.
M