- Overview
- Tutorials
- Getting started
- Get started with Canton and the JSON Ledger API
- Get Started with Canton, the JSON Ledger API, and TypeScript
- Get started with Canton Network App Dev Quickstart
- Get started with smart contract development
- Basic contracts
- Test templates using Daml scripts
- Build the Daml Archive (.dar) file
- Data types
- Transform contracts using choices
- Add constraints to a contract
- Parties and authority
- Compose choices
- Handle exceptions
- Work with dependencies
- Functional programming 101
- The Daml standard library
- Test Daml contracts
- Next steps
- Application development
- Getting started
- Development how-tos
- Component how-tos
- Explanations
- References
- Application development
- Smart contract development
- Daml language cheat sheet
- Daml language reference
- Daml standard library
- DA.Action.State.Class
- DA.Action.State
- DA.Action
- DA.Assert
- DA.Bifunctor
- DA.Crypto.Text
- DA.Date
- DA.Either
- DA.Exception
- DA.Fail
- DA.Foldable
- DA.Functor
- DA.Internal.Interface.AnyView.Types
- DA.Internal.Interface.AnyView
- DA.List.BuiltinOrder
- DA.List.Total
- DA.List
- DA.Logic
- DA.Map
- DA.Math
- DA.Monoid
- DA.NonEmpty.Types
- DA.NonEmpty
- DA.Numeric
- DA.Optional
- DA.Record
- DA.Semigroup
- DA.Set
- DA.Stack
- DA.Text
- DA.TextMap
- DA.Time
- DA.Traversable
- DA.Tuple
- DA.Validation
- GHC.Show.Text
- GHC.Tuple.Check
- Prelude
- Smart contract upgrading reference
- Glossary of concepts
Reference: Built-in Functions¶
This page gives reference information on built-in functions for working with a variety of common concepts.
Work with Time¶
Daml has these built-in functions for working with time:
datetime
: creates aTime
given year, month, day, hours, minutes, and seconds as argument.subTime
: subtracts one time from another. Returns theRelTime
difference betweentime1
andtime2
.addRelTime
: add times. Takes aTime
andRelTime
and adds theRelTime
to theTime
.days
,hours
,minutes
,seconds
: constructs aRelTime
of the specified length.pass
: (in Daml Script tests only) usepass : RelTime -> Script Time
to advance the ledger time by the argument amount. Returns the new time.
Work with Numbers¶
Daml has these built-in functions for working with numbers:
round
: rounds aDecimal
number toInt
.round d
is the nearestInt
tod
. Tie-breaks are resolved by rounding away from zero, for example:round 2.5 == 3 round (-2.5) == -3 round 3.4 == 3 round (-3.7) == -4
truncate
: converts aDecimal
number toInt
, truncating the value towards zero, for example:truncate 2.2 == 2 truncate (-2.2) == -2 truncate 4.9 == 4 v (-4.9) == -4
intToDecimal
: converts anInt
toDecimal
.
The set of numbers expressed by Decimal
is not closed under division as the result may require more than 10 decimal places to represent. For example, 1.0 / 3.0 == 0.3333...
is a rational number, but not a Decimal
.
Work with Text¶
Daml has these built-in functions for working with text:
<>
operator: concatenates twoText
values.show
converts a value of the primitive types (Bool
,Int
,Decimal
,Party
,Time
,RelTime
) to aText
.
To escape text in Daml strings, use \
:
Character |
How to escape it |
---|---|
|
|
|
|
|
|
Newline |
|
Tab |
|
Carriage return |
|
Unicode
(using |
|
Work with Lists¶
Daml has these built-in functions for working with lists:
foldl
andfoldr
: see Fold below.
Fold¶
A fold takes:
a binary operator
a first accumulator value
a list of values
The elements of the list are processed one-by-one (from the left in a foldl
, or from the right in a foldr
).
Note
We’d usually recommend using foldl
, as foldr
is usually slower. This is because it needs to traverse the whole list before starting to discharge its elements.
Processing goes like this:
The binary operator is applied to the first accumulator value and the first element in the list. This produces a second accumulator value.
The binary operator is applied to the second accumulator value and the second element in the list. This produces a third accumulator value.
This continues until there are no more elements in the list. Then, the last accumulator value is returned.
As an example, to sum up a list of integers in Daml:
sumList =
script do
assert (foldl (+) 0 [1, 2, 3] == 6)