on d2dacad help sign in
elk/docs/repl.md
66 lines 1.3 kB history raw
1# `elm repl`
3The REPL lets you interact with Elm values and functions in your terminal.
5<br>
7## Install
9You need [Elm][install] and [node.js][node] for the REPL. Elm generates JavaScript and we currently use node.js to evaluate it!
11[install]: https://guide.elm-lang.org/install.html
12[node]: https://nodejs.org/en/download/
14<br>
16## Use
18You can type in expressions, definitions, union types, and module imports using normal Elm syntax.
20```elm
21> 1 + 1
222 : number
24> "hello" ++ "world"
25"helloworld" : String
26```
28The same can be done with definitions and union types:
30```elm
31> fortyTwo = 42
3242 : number
34> increment n = n + 1
35<function> : number -> number
37> increment 41
3842 : number
40> factorial n = if n < 1 then 1 else n * factorial (n-1)
41<function> : number -> number
43> factorial 5
44120 : number
46> type Either a b = Left a | Right b
48> case Left 32 of \
49| Left n -> 2 * n \
50| Right m -> m + 1
5164 : number
52```
54When you run `elm repl` in a project with an `elm.json` file, you can import any module available in the project:
56```elm
57> import String
59> String.length "hello"
605 : Int
62> String.reverse "flow"
63"wolf" : String
64```
66Core modules, package modules, your modules. Whatever is available! So if you create a module named `MyThing` in your project, you can say `import MyThing` in the REPL and mess with anything it exposes.