Last active
November 15, 2017 09:29
-
-
Save mather/9f3612313a5fb980bbbd139baff5908c to your computer and use it in GitHub Desktop.
ElmでMonoidを作ってみた?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Monoid exposing (..) | |
import Html exposing (text) | |
main = text <| toString <| sum [1,2,3] | |
type alias Monoid a = | |
{ empty : a | |
, append : a -> a -> a | |
} | |
monoid : a -> (a -> a -> a) -> Monoid a | |
monoid empty append = | |
{ empty = empty | |
, append = append | |
} | |
sumM : Monoid number -> List number -> number | |
sumM m l = | |
List.foldr m.append m.empty l | |
sum : List number -> number | |
sum l = sumM intM l | |
intM : Monoid number | |
intM = monoid 0 (+) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Haskell風なら
sumM : Monoid number => List number -> number
として自動的に解決してくれそうなんだけど、elmだとそうはならないので…。