[Elm] Functions in Elm

Functions are an important building block in Elm. In this lesson we will review stateless functions, function composition, anonymous functions, Currying, and more.

 

Write Pure function:

import Htrml exposing (text)

main = 
    view "Hello World"

view message = 
    text message

We created a function call 'view', and pass the value ("Hello World") to 'message' param.

And inside view function, we output message value.

 

Add type:

import Htrml exposing (text)

main = 
    view "Hello World"

view: String -> Html msg
view message = 
    text message

For view function, we add ':String' to say the input value should be a string type.

If we change type to "Int":

It will output the error message on the screen.

 

Function execute from inside to outside:

复制代码
import Html exposing (Html, text)
import String

main = 
 view "Hello World!!!"

view: String -> Html msg
view message =
 text (String.repeat 3((String.toUpper message)))
复制代码

Output:

HELLO WORLD!!!HELLO WORLD!!!HELLO WORLD!!!

 

Forward:

复制代码
import Html exposing (Html, text)
import String

main = 
 view "Hello World!!!"

view: String -> Html msg
view message =
 --text (String.repeat 3((String.toUpper message)))
 message
  |> String.toUpper
  |> String.repeat 3
  |> text
复制代码

'|>' fowards symbol, so the message will go thought 'toUpper' --> 'repeat 3' --> text.

 

The 'repeat 3': Since we only provide the first value, it returns a function instead of a value. When two upper is evaluated, the message is being passed in. Finally, a value is returned and passed to the next line. This is known as currying. 

Then later, we can provide the rest of the parameters. This really helps us to build new functions from others.

For example, let's replace repeat with a function of our own creation called triple.

复制代码
import Html exposing (Html, text)
import String

main = 
 view "Hello World!!!"

triple: String -> String
triple =
 -- repeat : Int --> String -> String  repeat function take int and string param and return string
 String.repeat 3

view: String -> Html msg
view message =
 --text (String.repeat 3((String.toUpper message)))
 message
  |> String.toUpper
  |> triple
  |> text
复制代码

 

Let's add an anonymous function to add a comma and a space between each repetition. Anonymous functions begin with a back slash. 

We define str to take the value being passed in from the previous line. We use the double plus or string concatenation operator to add a comma and a space.

复制代码
import Html exposing (Html, text)
import String

main =
 view "Hello World"

triple: String -> String
triple =
 -- repeat : Int --> String -> String  repeat function take int and string param and return string
 String.repeat 3

view: String -> Html msg
view message =
 --text (String.repeat 3((String.toUpper message)))
 message
  |> String.toUpper
  |> \str -> str ++ ", "
  |> triple
  |> text
复制代码

 

posted @   Zhentiw  阅读(210)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-12-02 [Redux] Reducer Composition with Arrays
2015-12-02 [Redux] Writing a Todo List Reducer (Toggling a Todo)
2015-12-02 [Redux] Writing a Todo List Reducer (Adding a Todo)
点击右上角即可分享
微信分享提示