Skip to content

Elm 치트시트

안정적인 웹 앱을 위한 함수형 언어 — 런타임 예외가 없습니다.

01

시작하기

Hello World

Elm 앱은 main 함수에서 시작합니다.

elm
module Main exposing (main)

main =
  text "Hello, World!"

타입

타입 별칭(type alias)으로 레코드의 구조를 정의합니다.

elm
type alias Person =
  { name : String
  , age : Int
  }

p = Person "Alice" 30

함수

모든 함수는 커링되며, 타입은 -> 표기법으로 작성합니다.

elm
add : Int -> Int -> Int
add a b =
  a + b

Elm 아키텍처

The Elm Architecture: Model-View-Update 패턴입니다.

elm
-- Model
type alias Model = { count : Int }

-- Update
type Msg = Increment | Decrement

update msg model =
  case msg of
    Increment -> { model | count = model.count + 1 }
    Decrement -> { model | count = model.count - 1 }
02

자료구조

리스트

List 모듈에는 map, filter, foldl 등이 있습니다.

elm
nums = [1, 2, 3]
List.map (\n -> n * 2) nums
List.filter (\n -> n > 1) nums

Maybe

Maybe는 null을 안전하게 처리합니다 — Just 또는 Nothing.

elm
type Maybe a = Just a | Nothing

safeDiv a b =
  if b == 0 then Nothing else Just (a // b)

Result

Result는 에러 처리에 사용합니다 — Ok 또는 Err.

elm
type Result e a = Ok a | Err e

parse s =
  if String.length s > 0 then Ok s else Err "empty"

레코드

레코드는 .으로 접근하고 |으로 업데이트합니다.

elm
p = { name = "Alice", age = 30 }
p.name
{ p | age = 31 }  -- update

Was this helpful?