您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Elm 基本语法

前面了解了关于 Elm 的介绍与安装,接下来告诉你如何用 Elm 编写简单的Elm程序。

现在,在此目录中:Hello.elm

上图了项目夹 HelloApp 和在 VSCode 中打开的终端。

Elm 中的软件包管理器是 elm-package 。安装 elm-lang / html 软件包。该软件包将帮助我们在浏览器中Elm的。

通过右键单击VSCode的“”→“在命令符中打开”,遍历到HelloApp项目夹。

在终端窗口中执行以下命令-

C:\Users\F2er\Elm\HelloApp> elm-package install elm-lang/html

在安装软件包时,以下/夹将到项目目录中。

elm-package.json(),存储项目元数据

elm-stuff(夹),存储外部包装

成功安装软件包后,将出现以下消息。

-- importing Html module and the function text
import Html exposing (text)

-- create main method
main =
-- invoke text function
text "Hello Elm from F2er.com"

上面的程序将在浏览器中来自 F2er.com 的字符串消息 Hello Elm 。

为此,我们需要在Html模块中导入文本。文本用于在浏览器中打印任何字符串值。主要是程序的入口点。的主要文本,并传递字符串值到它。

在VSCode终端窗口中执行以下命令。

elm make Hello.elm

上面命令的如下所示

//update path to the proj folder in the command elm make
C:\Users\F2er\elm\HelloApp>elm make Hello.elm
Success! Compiled 38 modules.
Successfully generated index.html

上面的命令将 index.html 。elm编译器将.elm为JavaScript,并将其嵌入到index.html中。

在任何浏览器中打开index.html。

Elm 注释

注释是提高程序可读性的一种。

注释可用于包含有关程序的其他信息,例如作者,有关构造的等。编译器将忽略注释。

Elm以下类型的注释:

单行注释(-)-在-和行尾之任何文本均被视为注释。

多行注释({--})-这些注释可能跨越多行。

-- this is single line comment

{- This is a
   Multi-line comment
-}

行缩进

Elm 不提供大括号来指示用于定义或流控制的块。

块由行缩进表示,行缩进严格执行。块中的所有语句的缩进量必须相同。

例如:

module ModuleIf exposing (..)
x = 0

function1 =
   if x > 5 then
      "x is greater"
   else
      "x is small"

但是,以下块会产生-

-- Create file ModuleIf.elm
module ModuleIf exposing (..)
x = 0

function1 =
   if x > 5 then
      "x is greater"
         else --Error:else indentation not at same level of if statement
      "x is small"

因此,在 Elm 中,以相同的空格缩进的所有连续线将形成块。

C:\Users\admin>elm repl
---- elm-repl 0.18.0 -----------------------------------------------------------
   :help for help, :exit to exit, more at 
   <https://github.com/elm-lang/elm-repl>
   ---------------------------------------
   -----------------------------------------

> import ModuleIf exposing(..) -- importing module from ModuleIf.elm file
>function1 -- executing function from module
--  PROBLEM ---------------------------------------------------

I need whitespace, but got stuck on what looks like a new declaration. 
You are either missing some stuff in the declaration above or just need to add some spaces here:
7| else
   ^
I am looking for one of the following things:

   whitespace

联系我
置顶