How to resolve the algorithm Monads/List monad step by step in the Swift programming language
How to resolve the algorithm Monads/List monad step by step in the Swift programming language
Table of Contents
Problem Statement
A Monad is a combination of a data-type with two helper functions written for that type. The data-type can be of any kind which can contain values of some other type – common examples are lists, records, sum-types, even functions or IO streams. The two special functions, mathematically known as eta and mu, but usually given more expressive names like 'pure', 'return', or 'yield' and 'bind', abstract away some boilerplate needed for pipe-lining or enchaining sequences of computations on values held in the containing data-type. The bind operator in the List monad enchains computations which return their values wrapped in lists. One application of this is the representation of indeterminacy, with returned lists representing a set of possible values. An empty list can be returned to express incomputability, or computational failure. A sequence of two list monad computations (enchained with the use of bind) can be understood as the computation of a cartesian product. The natural implementation of bind for the List monad is a composition of concat and map, which, used with a function which returns its value as a (possibly empty) list, provides for filtering in addition to transformation or mapping.
Demonstrate in your programming language the following:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Monads/List monad step by step in the Swift programming language
Source code in the swift programming language
precedencegroup MonadPrecedence {
higherThan: BitwiseShiftPrecedence
associativity: left
}
infix operator >>-: MonadPrecedence // Monadic bind
extension Array
{
static func unit(_ x: Element) -> [Element]
{
return [x]
}
func bind<T>(_ f: (Element) -> [T]) -> [T]
{
return flatMap(f)
}
static func >>- <U>(_ m: [Element], _ f: (Element) -> [U]) -> [U]
{
return m.flatMap(f)
}
}
func adjacent(_ x: Int) -> [Int]
{
[x - 1, x + 1]
}
func squareRoots(_ x: Int) -> [Double]
{
guard x >= 0 else { return [] }
return [Double(x).squareRoot(), -(Double(x).squareRoot())]
}
print("\([Int].unit(8).bind(adjacent).bind(squareRoots))")
print("\([Int].unit(8) >>- adjacent >>- squareRoots)")
print("\([Int].unit(0) >>- adjacent >>- squareRoots)")
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Phix programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the R programming language