ProductPromotion
Logo

Elm

made by https://0x3d.site

GitHub - eeue56/haskell-to-elm
Contribute to eeue56/haskell-to-elm development by creating an account on GitHub.
Visit Site

GitHub - eeue56/haskell-to-elm

GitHub - eeue56/haskell-to-elm

haskell-to-elm

Collection of examples on places where Elm is different to Haskell.

Used for helping beginners moving from Haskell to Elm. Non-exhaustive list, only to be used alongside the documentation on the Elm site.

Functional programming

Type signatures

Elm uses a single colon (:) for type signatures. Double colons (::) is used for cons.

Example

	add :: Int -> Int

becomes

	add : Int -> Int

Function application

Instead of using the dollar symbol $ elm uses <| and |> for application in different directions.

Example:

    collage (round board.width) (round board.height) $ map (genRect board) board.pieces

becomes

    collage (round board.width) (round board.height) <| List.map (genRect board) board.pieces
    --
    List.map (genRect board) board.pieces |> collage (round board.width) (round board.height)

Function composition

Instead of using the (.) symbol Elm uses << and >> for composition in different directions

Example:

    isEvenSquareRoot = isEven . sqrt

becomes

    isEvenSquareRoot = sqrt >> isEven
    -- or
    isEvenSquareRoot = isEven << sqrt

List comprenhensions

There are no list comprehensions in Elm.

Lenses

Elm has the package focus for lense-like accessors. Due to a lack of template-haskell like functionality, you must always manually create your own focus

Example:

    data Patch = Patch {
		_colour :: Colour,
		_size :: Double, 
		_coord :: Coordinate
	} deriving (Show, Eq, Ord)
	
	mkLabels[''Patch]

becomes

    type alias Patch = {
        colour: Colour,
        size: Float,
        coord: Coordinate
    }
    
    colour = create .colour (\f r -> { r | colour = f r.colour })
    coord = create .coord (\f r -> { r | coord = f r.coord })
    size = create .size (\f r -> { r | size = f r.size })

where vs let

Elm has no where binding - instead use let

Pattern matching

Elm doesn't support multiple body declarations for functions, so instead you have to use case..of

Example:

	head [] = error
	head (X:xs) = x

becomes

	head xs = case xs of
  		x::xs -> Just x
  		[] -> Nothing

Purity

Functions in Elm as of 0.15.1 have pure type signatures. However, as they are actually implented in JS, it's possible that the underlying code you're calling isn't pure. This gives the effect of Elm the language being pure, but the things it can be used to do can be impure (eg, drawing to screen). Native functions can also produce runtime errors, though there is a drive to rid these from Elm entirely.

Built-in (Prelude) methods

id

Elm has renamed id to identity

Example:

	id xs

becomes

	identity xs

cons

Elm uses double colons (::) for cons.

Example

	5 : 6 : [7, 8]

becomes

	5 :: 6 :: [7, 8]

head

Instead of throwing errors for empty lists, Elm uses Maybe for head

Example

	head [4, 5] == 4

becomes

	case head [4, 5] of
		Just x -> x == 4
		Nothing -> False

tail

Instead of throwing errors for empty lists, Elm uses Maybe for tail

Example

	tail [4, 5] == [5]

becomes

	case tail [4, 5] of
		Just x -> x == [5]
		Nothing -> False

zip

Elm has no built-in zip method - instead it provides a map2 function that can be used with the tuple creator (,) to make a list of size 2 tuples from two lists.

Example:

    zip xs ys

becomes

    map2 (,) xs ys

show

Elm renamed show to toString. Confusingly, there is also a method called show in Elm - this generates a HTML element containing a textual representation of the data.

Example:

    show [1, 2, 3]

becomes

    toString [1, 2, 3]

mod

mod in Elm uses the (%) symbol.

Example:

    isEven x = x `mod` 2 == 0

becomes

    isEven x = x % 2 == 0

unwords

unwords is replaced by the join function

Example:

	unwords $ words "Hello Dave and Jeremy"

becomes

	join " " <| words "Hello Dave and Jeremy"

cycle

Elm has no cycle built in.

TODO: find documentation for this

foldl

The order of the accumalator function arguments are swapped in Elm.

Example:

	idx xs = foldl (\x y -> y : x) [] xs

becomes

	id xs = List.foldl (\x y -> x :: y) xs [] 

Module syntax

Importing names

Elm uses the exposing keyword to import names into the current namespace.

Example:

    import List (map, foldl)

becomes

    import List exposing (map, foldl)

Defining exportable names

TODO: find documentation on elm site for this

Following the module declaration, you must have no identnation level.

Example:

    module Coords (pos) where
        pos x y = (x, y)

becomes

    module Coords exposing (pos)
    pos x y = (x, y)

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory