|  | 
|  | 1 | +# Elm syntax basics 2 | 
|  | 2 | + | 
|  | 3 | +## Exporting functions | 
|  | 4 | + | 
|  | 5 | +Exporting functions was covered in the first concept, here is a quick refresher. | 
|  | 6 | + | 
|  | 7 | +Each file in Elm is a module and must contain a `module` statement before all other code. | 
|  | 8 | + | 
|  | 9 | +Module names must match their file name, so module `Calculator` must be in file Calculator.elm, and module `Parser.Utils` needs to be in file Parser/Utils.elm. | 
|  | 10 | + | 
|  | 11 | +Anything defined within a module is privatly scoped to it and cannot be accessed from outside this module, unless listed in `exposing`. | 
|  | 12 | + | 
|  | 13 | +```elm | 
|  | 14 | +-- Calculator.elm | 
|  | 15 | +-- Define the Calculator module, and expose the `add` function | 
|  | 16 | +module Calculator exposing (add) | 
|  | 17 | + | 
|  | 18 | +six = 3 + 3 | 
|  | 19 | + | 
|  | 20 | +add number1 number2 = number1 + number2 | 
|  | 21 | +``` | 
|  | 22 | + | 
|  | 23 | +```elm | 
|  | 24 | +-- Calculator.elm | 
|  | 25 | +-- Define the Calculator module, and expose everything within: `six` and `add` | 
|  | 26 | +module Calculator exposing (..) | 
|  | 27 | + | 
|  | 28 | +six = 3 + 3 | 
|  | 29 | + | 
|  | 30 | +add number1 number2 = number1 + number2 | 
|  | 31 | +``` | 
|  | 32 | + | 
|  | 33 | +https://elm-lang.org/docs/syntax#modules | 
|  | 34 | + | 
|  | 35 | +## Importing functions from other modules | 
|  | 36 | + | 
|  | 37 | +Accessing functions defined in other modules is done via imports. | 
|  | 38 | +All functions within that module that were exposed by it are made accessible when importing that module. | 
|  | 39 | +But how they are accessed varies depending on how the module is imported. | 
|  | 40 | + | 
|  | 41 | +Qualified imports are the default, and accessing a function within such module (for example the `map` function in the `List` module) is done by prefixing the module name (`List.map`). | 
|  | 42 | + | 
|  | 43 | +Open imports enable direct access to the exposed functions within that module, without prefixing. They are done with the `exposing` keyword, like a mirror to the one used in exports. You can expose all available functions (with `..`), or specific ones (for example `map`). | 
|  | 44 | + | 
|  | 45 | +You can also import types, and their constructors from other modules, which is a later concept. | 
|  | 46 | + | 
|  | 47 | +Qualified imports are preferred to aid clarity and avoid name clashes. | 
|  | 48 | + | 
|  | 49 | +```elm | 
|  | 50 | +-- qualified imports | 
|  | 51 | +import List                            -- List.map, List.foldl | 
|  | 52 | +import List as L                       -- L.map, L.foldl | 
|  | 53 | + | 
|  | 54 | +-- open imports | 
|  | 55 | +import List exposing (..)              -- map, foldl, concat, ... | 
|  | 56 | +import List exposing ( map, foldl )    -- map, foldl, List.concat | 
|  | 57 | +``` | 
|  | 58 | + | 
|  | 59 | +https://elm-lang.org/docs/syntax#modules | 
|  | 60 | + | 
|  | 61 | +## Type annotations | 
|  | 62 | + | 
|  | 63 | +Type annotations are defined with `name : parameter types -> return type`, parameter types also being separated by `->`. | 
|  | 64 | + | 
|  | 65 | +```elm | 
|  | 66 | +add : Int -> Int -> Int | 
|  | 67 | +add number1 number2 = number1 + number2 | 
|  | 68 | +``` | 
|  | 69 | + | 
|  | 70 | +Parentheses can be used to define function parameters (which are themselves defined with the same syntax, hence the need for brackets). | 
|  | 71 | + | 
|  | 72 | +```elm | 
|  | 73 | +-- Transform every character in a string | 
|  | 74 | +map : (Char -> Char) -> String -> String | 
|  | 75 | +map charMapperFunction string = | 
|  | 76 | +    -- ... | 
|  | 77 | +``` | 
|  | 78 | + | 
|  | 79 | +https://elm-lang.org/docs/syntax#type-annotations | 
|  | 80 | + | 
|  | 81 | +## Numbers | 
|  | 82 | + | 
|  | 83 | +There are two types of numbers available in Elm, defined by the `Int` and `Float` types. | 
|  | 84 | +`Int` corresponds to the set of positive and negative integers. | 
|  | 85 | +`Float` corresponds to the set of real numbers, limited by the precision of the computer. | 
|  | 86 | +Operations defined on numbers usually work on one type or the other, but not mixing them. | 
|  | 87 | +There exists functions to convert between the two, such that `toFloat` which converts `Int` to `Float` and `round`, `floor`, `ceiling` or `truncate` which convert a `Float` to an `Int` with different semantics. | 
|  | 88 | + | 
|  | 89 | +https://package.elm-lang.org/packages/elm/core/latest/Basics#Int | 
|  | 90 | + | 
|  | 91 | +## String operations | 
|  | 92 | + | 
|  | 93 | +To describe words and sentences in code, Elm has a native `String` type. | 
|  | 94 | +String literals are written between double quotes such as `"Hello World!"`. | 
|  | 95 | +A string concatenation operator `++` and equality operator `==` are available by default. | 
|  | 96 | + | 
|  | 97 | +```elm | 
|  | 98 | +hello : String -> String | 
|  | 99 | +hello subject = "Hello " ++ subject | 
|  | 100 | + | 
|  | 101 | +hello "World!" --> "Hello World!" | 
|  | 102 | +``` | 
|  | 103 | + | 
|  | 104 | +Most other string functions are in the `String` module, which needs to be imported. | 
|  | 105 | + | 
|  | 106 | +https://package.elm-lang.org/packages/elm/core/latest/String | 
0 commit comments