Stanza by Example

Patrick S. Li

Stanza by Example is an introductory book for teaching readers how to program in the L.B. Stanza programming language. Readers are assumed to have basic programming experience, at about the level required to implement and understand a basic sorting algorithm. This book is not a reference book, and is meant to be read in order from front to back. The material is written expecting readers to follow along with the coding examples and to the suggested exercises. By following the book, readers will gain a thorough understanding of Stanza's fundamental mechanisms and coding style.

For absolute beginners to programming, the pace of the first chapter will feel a bit fast, and readers are encouraged to take their time to understand and experiment with the examples. When I was young, I taught myself to program by reading Beginning Java 2 by Ivor Horton, and one of the goals of this book is to help beginners get started with programming in the same way that Mr. Horton's book has helped me. Once you get the hang of it, programming is an extremely creative and satisfying endeavor.

I hope you enjoy the book, and Stanza.

  -Patrick

For those people that would prefer an offline version of the book, you can also find a PDF version here.

Getting Started

This chapter explains how to download and install Stanza for your system, compile the example programs, and also write your own very first Stanza program.

  1. Get Stanza
  2. Write a Program

The Very Basics

This chapter introduces the basic programming constructs in Stanza. After this chapter, you'll be able to write basic programs that do simple things.

  1. Project Framework
  2. Printing Simple Messages
  3. Lexical Structure
  4. Comments
  5. Operators
  6. Values
  7. Variables
  8. Functions
  9. Comparisons
  10. If Expressions
  11. Expression Sequences
  12. Structure Through Indentation
  13. While Loops
  14. For "Loops"
  15. Labeled Scopes
  16. Scopes and the Let Expression
  17. Arrays
  18. Tuples
  19. Basic Types
  20. Structs
  21. Exercises

The Less Basic

This chapter teaches some constructs in Stanza that you may not be familiar with coming from other languages.

  1. More about Structs
  2. The Match Expression
  3. The Is Expression
  4. Casts
  5. Deep Casts
  6. Operations on Strings
  7. Operations on Tuples
  8. Packages
  9. Function Overloading
  10. Operator Mapping
  11. Vectors
  12. HashTables
  13. KeyValue Pairs
  14. For Loops over Sequences
  15. Extended Example: Complex Number Package

Architecting Programs

This chapter teaches you how to organize and architect large programs using Stanza's class-less object system.

  1. A Shape Library
  2. Creating a New Shape
  3. Subtyping
  4. Multis and Methods
  5. Default Methods
  6. Underneath the Hood
  7. Intersection Types
  8. The Flexibility of Functions
  9. Fundamental and Derived Operations
  10. Multiple Dispatch
  11. Ambiguous Methods
  12. Revisiting Print
  13. The New Expression
  14. Constructor Functions
  15. Revisiting Defstruct

Programming with First-Class Functions

This chapter teaches you how to use functions as values, which is one fundamental part of the functional programming paradigm.

  1. Nested Functions
  2. Functions as Arguments
  3. Functions as Return Values
  4. Core Library Functions
  5. Anonymous Functions
  6. The For Construct
  7. Stanza Idioms
  8. Tail Calls
  9. Revisiting While

Programming with Sequences

This chapter teaches you how to program using Stanza's sequence abstraction, as well as how to make full use of the core sequence library.

  1. Fundamental Operations
  2. Writing a Sequence Function
  3. Lazy Sequences
  4. Using The Sequence Library
  5. Collection versus Seqable
  6. Revisiting Stack

Programming with Immutable Datastructures

This chapter teaches you how to program using datastructures that cannot be changed after being initialized. This is the other fundamental part of functional programming paradigm.

  1. Lists
  2. Example: Coin Counting
  3. List Library
  4. Example: More Coin Counting
  5. Extended Example: Automatic Differentiation

Parametric Polymorphism

This chapter teaches you how to define types that are themselves parameterized by other types. After this chapter, you will be able to define datastructures and libraries that are as general and as powerful as the ones included with Stanza.

  1. The Need for Polymorphism
  2. Explicit Type Arguments
  3. Captured Type Arguments
  4. Parametric Types
  5. Match Expressions and Type Erasure
  6. Revisiting Stack

Advanced Control Flow

This chapter teaches you about Stanza's advanced control flow constructs, as well as about the fundamental mechanism underlying all of them: the targetable coroutine system.

  1. First Class Labeled Scopes
  2. Dynamic Wind
  3. Dynamically Scoped Variables
  4. Attempts and Failures
  5. Example: S-Expression Parser
  6. Exception Handling
  7. Generators
  8. Coroutines
  9. Example: Key Listener

Stanza's Macro System

This chapter teaches you about Stanza's macro system: what macros are, how to write your own, and some examples of using them. (This chapter is still a work in progress - May 31, 2022)

  1. What Is a Macro?
  2. Defining and Using Your First Macro
  3. Exploring Further
  4. The DefSyntax System: A Small Experiment Framework
  5. The Pattern Language
  6. Productions and Rules
  7. The Stanza Core Macros

Stanza's Type System

This chapter summarizes and explains the mechanisms behind Stanza's optional type system in more depth.

  1. Kinds of Types
  2. The Subtype Relation
  3. Ground Types
  4. Parametric Types
  5. Tuple Types
  6. Function Types
  7. Union Types
  8. Intersection Types
  9. The Void Type
  10. The Unknown Type

Calling Foreign Functions

This chapter teaches you write programs that interact with code written in other languages. It will teach you how to call functions written in other languages, and how to write Stanza functions meant to be called from other languages.

  1. Writing a C Function
  2. Calling our C Function
  3. Calling LoStanza from Stanza
  4. LoStanza Types
  5. External Global Variables
  6. Function Pointers
  7. The Address Operator
  8. Calling LoStanza from C
  9. Passing Callbacks to C

Appendix

Stanza has a number of convenience constructs that make your life easier, but they are not necessary for day to day programming. You may skim through this appendix and learn about these constructs as their need arises.

  1. Stanza Compiler Options
  2. The When Expression
  3. The Where Expression
  4. The Switch Expression
  5. More on Visibility
  6. Macro Plugins