on master help sign in
update atom function documentation
commit6498a31829cd97653b4dc6e588f7de31c7c56dd9 authorAgentM <agentm@themactionfaction.com> dateSun 9 Apr 2017, 22:37 (10 years ago) parenta1f0542
1 file changed+3−3
docs/atomfunctions.markdown+3−3
docs/atomfunctions.markdown+3−3
@@ -15,10 +15,10 @@ In this example, we install an atom function to convert between Fahrenheit and C
1515We write the function giving it the name "celsiusToFahrenheit" with type ```Int -> Int``` which indicates to the compiler that the function take one ```Int``` argument value and returns one ```Int``` value. Finally, the body of the function is implemented in quotes or triple-quotes. Note that the function makes use of the "[ProjectM36.Base](/src/lib/ProjectM36/Base.hs)" module to handle the Atom type. The function must take a list of Atoms as arguments and return an Atom. The standard GHC Prelude is available in this AtomFunctionBody context.
1616
1717```
18−TutorialD (master/main): addatomfunction "celsiusToFahrenheit" Int -> Int """\((IntAtom c):_) -> IntAtom ( (c * 9 `div` 5) + 32)"""
18+TutorialD (master/main): addatomfunction "celsiusToFahrenheit" Int -> Either AtomFunctionError Int """(\\((IntAtom c):_) -> pure $ IntAtom ( (c * 9 `div` 5) + 32)) :: [Atom] -> Either AtomFunctionError Atom"""
1919```
2020
21−Using triple-quotes allows quotes to be used inside the string without backslash escaping, which can become tedious.
21+Using triple-quotes allows quotes to be used inside the string without backslash escaping, which can become tedious. The sum type using ```Either``` allows the function to return an error, if necessary.
2222
2323## Advanced Atom Function Example
2424
@@ -30,7 +30,7 @@ In the following example, splitting the temperature values into two types make s
3030data CelsiusTemperature = CelsiusTemperature Int
3131data FahrenheitTemperature = FahrenheitTemperature Int
3232
33−addatomfunction "celsiusToFahrenheit" CelsiusTemperature -> FahrenheitTemperature """\(ConstructedAtom _ _ (IntAtom celsius:_):_) -> ConstructedAtom "Fahrenheit" (ConstructedAtomType "Fahrenheit" empty) [IntAtom (celsius * (9 `div` 5) + 32)] """
33+addatomfunction "celsiusToFahrenheit" CelsiusTemperature -> Either AtomFunctionError FahrenheitTemperature """(\(ConstructedAtom _ _ (IntAtom celsius:_):_) -> pure $ ConstructedAtom "Fahrenheit" (ConstructedAtomType "Fahrenheit" empty) [IntAtom (celsius * (9 `div` 5) + 32)]) :: [Atom] -> Either AtomFunctionError Atom """
3434```
3535
3636First, we define our new types and use these types in the function's type definition. Next, we deconstruct the incoming ```ConstructedAtom``` argument to extract its ```Int``` value. Finally, we perform the calculation and wrap the value in a new Fahrenheit-based ```ConstructedAtom```.