Examples-0.1.0: Haskell code examples
Copyright© Frank Jung 2024
LicenseGPL-3.0-only
Safe HaskellSafe-Inferred
LanguageHaskell2010

Defunc

Description

Defunctionalization

A small example to show how to defunctionalise lambda functions.

References

Synopsis

Types

data Arrow p r where Source #

Defunctionalisation

Defunctionalisation of lambda expressions from the motivating example.

Arrow data type with two function constructors representing the lambda expressions from our motivating example.

Constructors

FPlus :: Arrow (Int, Int) Int 
FPlusCons :: Int -> Arrow (Int, [Int]) [Int] 

Functions

fold :: (a -> b -> b) -> b -> [a] -> b Source #

Motivation

The motivating example is the following functions.

Fold a list using recursion.

sum :: [Int] -> Int Source #

Sum using fold.

add :: Int -> [Int] -> [Int] Source #

Add one to each element using fold.

apply :: Arrow p r -> p -> r Source #

Apply the Arrow to the input.

fold' :: Arrow (a, b) b -> b -> [a] -> b Source #

Fold a list using the Arrow.

sum' :: [Int] -> Int Source #

Sum using fold'.

add' :: Int -> [Int] -> [Int] Source #

Add n to each element using fold'.