Title here
Summary here
Constants in Purescript can be declared using the let keyword with constant instead of var. Here’s how you can define and use constants in Purescript.
module Main where
import Prelude
import Data.Number (div)
import Effect (Effect)
import Effect.Console (log)
import Math (sin)
-- Declare a constant value.
let s :: String
s = "constant"
main :: Effect Unit
main = do
log s
-- Constants can appear anywhere a variable declaration can.
let n = 500000000
-- Constant expressions perform arithmetic with arbitrary precision.
let d = 3.0e20 / fromInt n
log (show d)
-- A numeric constant has no type until it's given one
-- Here, n and d will be given types through explicit conversion or usage in expressions.
log (show (fromInt (floor d :: Int)))
-- A number can be given a type by using it in a context that requires one.
-- For example, here `Math.sin` expects a `Number`.
log (show (sin (toNumber n)))To run this program, you would typically compile it with spago and execute the resulting JavaScript with node.
$ spago build
$ spago run
constant
6.0e+11
600000000000
-0.28470407323754404This demonstrates how to define and use constants in Purescript, including performing arithmetic with arbitrary precision and handling numeric constants without an initial type.