Created
February 18, 2022 23:30
-
-
Save derrickturk/db82e27f13d3644cc7e7b05baf0d14ed to your computer and use it in GitHub Desktop.
step step step step step it up
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
{- requires to >= from -} | |
let stepsFromTo: Natural -> Natural -> Natural -> Natural = | |
\(from: Natural) -> | |
\(to: Natural) -> | |
\(step: Natural) -> | |
let diff = Natural/subtract from to | |
let State = { i: Natural, val: Natural } | |
let state = { i = 0, val = from } | |
let foldState = \(s: State) -> if Natural/isZero (Natural/subtract s.val to) | |
then s | |
else { i = s.i + 1, val = s.val + step } | |
let stateFinal = Natural/fold diff State foldState state | |
in stateFinal.i + 1 | |
let iota: Natural -> Natural -> Natural -> List Double = | |
\(from: Natural) -> | |
\(to: Natural) -> | |
\(step: Natural) -> | |
let count = stepsFromTo from to step | |
let listBuilder = \(list: Type) -> | |
\(cons: Double -> list -> list) -> | |
\(nil: list) -> | |
let State = { i: Natural, val: Natural, vals: list } | |
let state = { i = 0, val = from, vals = nil } | |
let foldStep = \(s: State) -> | |
let v = Integer/toDouble (Natural/toInteger s.val) | |
in { i = s.i + 1 | |
, val = s.val + step | |
, vals = cons v s.vals | |
} | |
let finalState = Natural/fold count State foldStep state | |
in finalState.vals | |
in List/build Double listBuilder | |
in iota |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment