Learning Elm

I was having a hard time with the main tutorial, so I’m winding a different path through the language.

8/29: After doing the basics, I started hitting walls, and found the tutorial going all over the place, so I decided to dig through the Elm core libraries, and just try running commands: https://package.elm-lang.org/packages/elm/core/latest/

Typing things out helps me to understand the syntax and also the notation in the docs.

To get started, you need to install Elm. Then, you’ll get the elm command, which is like npm or composer. elm is a package manager and a build tool. Here’s part of the help when you type “elm”:

elm repl --help
elm init --help
elm reactor --help
elm make --help
elm install --help
elm bump --help
elm diff --help
elm publish --help

The main tools to use as a beginner are elm reactor, the web browser dev tool, and elm repl, the “console” where you can type Elm code and try it out.

I started with Array, List, and Maybe. Also see the syntax reference card and the core language reference.

It was a good way to get familiar with the notation:

fromList : List a -> Array aCode language: PHP (php)

The Array.fromList function takes a list as an argument, and returns an array.

push : a -> Array a -> Array aCode language: JavaScript (javascript)

The push function takes a value, and an array as arguments, and returns an array with the value appended to the end.

Arrays and Lists made sense, but Maybe was different. Type these lines into the REPL.

import Array exposing (..)

x = fromList [1, 2, 3]
get 1 x

-- returns Just 2 : Maybe number
Code language: JavaScript (javascript)

The return value, Just 2, is not 2. You expect 2, but get Just 2. What is up with that?

Just 2 is of type Maybe. Maybe values can be “Just number” or “Nothing”. From the docs:

type Maybe a
= Just a
| Nothing

“Represent values that may or may not exist. It can be useful if you have a record field that is only filled in sometimes. Or if a function takes a value sometimes, but does not absolutely need it.”

So, this looked weird to me, but here’s a quick way to convert it to a normal value:

-- here's the function
withDefault : a -> Maybe a -> a

-- usage
withDefault 0 (Just 2)
-- returns the value 2 : number

OK, that is still weird. (Most languages have type coercion or functions to convert values.)

Here’s an example from the syntax guide:

<code>case maybeList of
    Just xs -> xs
    Nothing -> []</code>Code language: HTML, XML (xml)

This is a different way to coerce values. It switches based on the type variant of maybeList.

So Maybe is a type, with two variantsJust xs” and “Nothing“.

The case switching isn’t based on the value of the variable, but on the variant.

(This might look like a variant is, itself, a type. This isn’t the case, because maybeList must be a Maybe. The case switch will match only variants of Maybe. This is more restrictive than the case switch statements you see in OOP languages with a typeof() operator.)

I’ll stop at this point, because types are important to Elm. I have to spend more time studying it. It’s reminding me of types in Haskell: and that means it’s part of how the program executes.

https://dev.to/jigargosar/elm-vs-the-feature-addiction-epidemic-in-programming-ach

https://riptutorial.com/elm

https://guide.elm-lang.org/types

https://learnyouanelm.github.io/pages/03-types.html

admin
Author: admin

This is the server’s system administrator. This site is undergoing some changes.