A good way to learn about Elm is to try writing some expressions in the REPL (Read-Eval-Print Loop). Open a console in your elm-app
folder (that you have created in the Initialize and build phase) and try the following:
$ elm repl
---- elm-repl 0.17.1 -----------------------------------------------------------
:help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
> 2 + 2
4 : number
> \x -> x
<function> : a -> a
> (\x -> x + x)
<function> : number -> number
> (\x -> x + x) 2
4 : number
>
elm-repl
is actually a pretty powerful tool.
Let's say you create a Test.elm
file inside your elm-app
folder with the following code:
module Test exposing (..)
a = 1
b = "Hello"
Now, you go back to your REPL (which has stayed opened) and type:
import Test exposing (..)
> a
1 : number
> b
"Hello" : String
>
Even more impressive, if you add a new definition to your Test.elm
file, such as
s = """
Hello,
Goodbye.
"""
Save your file, go back once again to your REPL, and without importing Test
again, the new definition is available immediately:
> s
"\nHello,\nGoodbye.\n" : String
>
It's really convenient when you want to write expressions which span many lines. It's also very useful to quickly test functions that you have just defined. Add the following to your file:
f x =
x + x * x
Save and go back to the REPL:
> f
<function> : number -> number
> f 2
6 : number
> f 4
20 : number
>
Each time you modify and save a file that you have imported, and you go back to the REPL and try to do anything, the full file is recompiled. Therefore it will tell you about any error in your code. Add this:
c = 2 ++ 2
Try that:
> 0
-- TYPE MISMATCH -------------------------------------------------- ././Test.elm
The left argument of (++) is causing a type mismatch.
22| 2 ++ 2
^
(++) is expecting the left argument to be a:
appendable
But the left argument is:
number
Hint: Only strings, text, and lists are appendable.
>
To conclude this introduction to the REPL, let's add that elm-repl
also knows about the packages that you have installed with elm package install
. For instance:
> import Html.App
> Html.App.beginnerProgram
<function>
: { model : a, update : b -> a -> a, view : a -> Html.Html b }
-> Platform.Program Basics.Never
>