Skip to content

Instantly share code, notes, and snippets.

@kapranov-anton
Created September 6, 2019 23:03
Show Gist options
  • Save kapranov-anton/1b913f855850813d7811800d6be4d969 to your computer and use it in GitHub Desktop.
Save kapranov-anton/1b913f855850813d7811800d6be4d969 to your computer and use it in GitHub Desktop.
Более осмысленный пример Fix
import Html exposing (text)
type NodeF v a
= Empty v
| Elem (List a)
nodeMap : (a -> b) -> NodeF v a -> NodeF v b
nodeMap f node =
case node of
Empty v -> Empty v
Elem items -> Elem (List.map f items)
type FixNode v = FixNode (NodeF v (FixNode v))
nodeCata : (NodeF v a -> a) -> FixNode v -> a
nodeCata alg (FixNode node) =
alg (nodeMap (nodeCata alg) node)
main = text <| Debug.toString <| {
sum = nodeCata sumNode ff,
mul = nodeCata mulNode ff,
list = nodeCata aggNode ff}
ff = FixNode (Elem [
FixNode (Empty 3),
FixNode (Empty 2),
FixNode (Elem [
FixNode (Empty 5),
FixNode (Empty 6),
FixNode (Empty 1)
])
])
sumNode : NodeF Int Int -> Int
sumNode n =
case n of
Empty v -> v
Elem items -> List.sum items
mulNode : NodeF Int Int -> Int
mulNode n =
case n of
Empty v -> v
Elem items -> List.product items
aggNode : NodeF v (List v) -> List v
aggNode n =
case n of
Empty v -> [v]
Elem items -> List.concat items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment