PeaBox Lab › PeaLang › Learn › Chapter 5 › Lesson 1

Part I — Foundations · Chapter 5 · Lesson 1 of 5 · 10 min read

Lesson 1: null, Booleans, Numbers, Strings, Lists, and String-Keyed Maps

Lesson level: Foundation

Purpose: Build a precise mental model of PeaLang's core portable values and choose representations that make data meaning, absence, and structure explicit.

Learning objectives

By the end of this lesson, you should be able to:

1. Core values form the portable boundary

PeaLang's portable data model consists of null, booleans, integers, floating-point numbers, strings, lists, and string-keyed maps. These values can be passed between PeaLang and supported hosts without exposing host-specific objects.

Lists and maps can contain other portable values, allowing nested records and sequences. A boundary remains portable only when every nested value is portable.

2. null represents absence

The value null represents no value or an unavailable result. It is not the same as false, zero, an empty string, an empty list, or an empty map.

let assigned_agent = null if assigned_agent == null { print "No agent assigned" }

Use null only when absence is meaningful and expected. Document whether a field or function result may be null, and check it before using operations that require a present value.

3. Absence versus emptiness

These states can carry different domain meaning. An absent response time differs from a measured response time of zero.

4. Booleans represent explicit facts

The values true and false represent two-state facts. Comparisons and equality tests also produce booleans.

let security_relevant = false let has_affected_users = affected_users > 0 let is_gold_customer = customer_tier == "gold"

Choose positive, question-like names such as is_active, has_discount, or requires_review. Avoid confusing double negatives.

5. Truthiness is broader than booleans

Conditions interpret values by truthiness, but a truthy value is not necessarily the boolean true. For domain rules, prefer explicit comparisons when the value itself has meaning.

if affected_users > 0 { print "Users are affected" }

This is clearer than relying on the numeric value directly as a condition.

6. Integers

Integers represent whole numbers such as counts, indices, thresholds, and identifiers that are genuinely numeric.

let quantity = 3 let affected_users = 125 let retry_count = 0

Use integers for discrete values. Do not use a number for a code that must preserve leading zeros or is never used arithmetically; use a string instead.

7. Floating-point numbers

Floating-point values represent numbers with fractional parts, such as rates, measurements, and calculated amounts.

let unit_price = 12.5 let tax_rate = 0.18 let confidence = 0.875

Printed integral floats omit the trailing decimal part, so 12.0 may appear as 12. Do not infer the original literal spelling from printed text.

8. Numeric operations

Numbers support arithmetic, comparisons, and unary minus. Validate before dividing and make units visible in names.

let subtotal_amount = unit_price * quantity let average_score = total_score / item_count let within_limit = memory_mb <= max_memory_mb

Mixed integer and floating-point calculations can produce floating-point results. Test the actual numeric behavior rather than assuming text formatting or host-language rules.

9. Floating-point precision

Binary floating-point arithmetic may produce small representation differences. For derived measurements and round trips, compare against a documented tolerance rather than requiring identical decimal text.

For money-like examples in this foundational chapter, keep formulas explicit and avoid claiming accounting-grade decimal guarantees. A production contract should define precision, rounding, and currency policy deliberately.

10. Numeric validation

11. Strings represent text

Strings are sequences of characters written with matching single or double quotation marks. Use them for names, labels, reason codes, identifiers, messages, and textual categories.

let customer_tier = "gold" let case_id = "CASE-1042" let reason_code = "IMPACT"

A quoted number such as "00125" remains text and preserves leading zeros. An unquoted 125 is numeric and supports arithmetic.

12. String equality and categories

String equality is exact and case-sensitive. Normalize external categories at a trusted boundary when the contract permits it, or require one canonical spelling.

if customer_tier == "gold" { print "Gold policy applies" }

Stable categories and reason codes should be documented. A misspelled category may be syntactically valid but semantically unsupported.

13. Interpolation and text conversion

Interpolated strings embed expressions inside braces:

print "Case {case_id}: {affected_users} affected users"

The expression result is converted to text. Keep complex calculations outside the string when a named intermediate improves clarity. Use doubled braces for literal brace output.

14. Strings are not structured records

A string such as "CASE-1042|gold|6" may look compact, but its fields and validation rules are hidden. Use a map when values have names, and use a list when values form an ordered collection.

Do not build machine interfaces from ad hoc printed text unless the protocol explicitly defines parsing and escaping rules.

15. Safe string handling

16. Lists represent ordered collections

A list stores values in sequence order. Use it when position or traversal order matters and items share one conceptual role.

let scores = [8, 9, 10] let priorities = ["low", "medium", "high"] let mixed = ["CASE-1", 6, false, null]

PeaLang lists can contain different portable value categories, but homogeneous lists are often easier to validate and process.

17. Empty and nested lists

An empty list is present but contains no items:

let warnings = []

Lists can contain lists or maps:

let rows = [["A", 10], ["B", 20]] let cases = [{"id": "CASE-1"}, {"id": "CASE-2"}]

Nested structures remain portable when every nested value is portable.

18. String-keyed maps represent records

A map associates string keys with values. Use it when fields have names or when a portable record must cross a host boundary.

let case = { "id": "CASE-1042", "customer_tier": "gold", "service_impact": 6, "security_relevant": false }

Keys express field meaning. Values may be any portable value, including nested lists and maps.

19. Stable map contracts

A map is flexible, but a reusable interface still needs a schema-like contract: required keys, optional keys, allowed value categories, nullability, units, and invariants.

Do not access a required field before verifying that the expected key is present through supported map operations. A missing-key failure should be distinguished from a present key whose value is null.

20. Lists versus maps

21. Nested portable example

let report = { "case_id": "CASE-1042", "valid": true, "scores": [20, 15, 10], "decision": { "priority": "medium", "response_minutes": 240 }, "note": null }

This structure combines every core value category except a floating-point number. It can remain portable across supported host boundaries because all keys are strings and all nested values are portable.

22. Validate shape before content

Validation should proceed from broad structure to specific meaning:

This ordering prevents later checks from assuming structure that has not been established.

23. Equality and semantic meaning

Equality asks whether values compare equal according to the language's value rules. A later lesson examines collection equality, copying, and mutation in depth. At this stage, distinguish equality from identity and from domain equivalence.

For example, two different textual spellings may refer to the same business category only if the contract defines normalization. Two floating-point calculations may be close enough for a tolerance even when their exact representations differ.

24. Choosing a representation

25. Common mistakes

Guided practice

Hands-on lab: Build a portable case-record validator

Scenario. Create a source-defined support-case record and validate its core value shape before using it.

Record fields

Requirements

Test variants

Deliverable. Submit the valid record, validation functions, result contract, test matrix, output, and a representation rationale for every field.

Review questions

Applied challenge: Portable order payload

Design a portable order payload containing an order identifier, customer record, list of line-item maps, nullable promotion code, monetary summary, boolean flags, and validation metadata. Use only null, booleans, numbers, strings, lists, and string-keyed maps.

Write a representation rationale, required-key contract, nullability rules, units, approved categories, and invariants. Create valid, empty, boundary, malformed, and missing-field cases. Validate shape before content and return a complete portable validation result.

Success criteria

Lesson summary

PeaLang's core portable values are null, booleans, integers, floating-point numbers, strings, lists, and string-keyed maps. Reliable programs distinguish absence from empty or false values, make numeric units and precision explicit, use strings for text and non-arithmetic identifiers, represent order with lists, represent named records with maps, and validate structure before content. Nested collections remain portable only when every contained value is portable.

Next lesson: Indexing, Slicing, Mutation, Copying, and Equality

Lesson status: Approved.

← Previous Next →