• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

eeue56/haskell-to-elm

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

eeue56/haskell-to-elm

开源软件地址(OpenSource Url):

https://github.com/eeue56/haskell-to-elm

开源编程语言(OpenSource Language):


开源软件介绍(OpenSource Introduction):

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)



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
oxij/haskell-course-ru发布时间:2022-06-24
下一篇:
HeinrichApfelmus/threepenny-gui: GUI framework that uses the web browser as a di ...发布时间:2022-06-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap