shithub: limbobyexample

Download patch

ref: 87e325f2c9792ece2583761ca26c57318a698be9
parent: 0283ccf588a5243b2a992d3d726c2f1344c5438a
author: seh <henesy.dev@gmail.com>
date: Mon Mar 18 13:08:49 EDT 2019

add lists example

--- a/Lists/README.md
+++ b/Lists/README.md
@@ -1,17 +1,48 @@
 # Lists
 
-Limbo supports lists of a type as a basic construct. 
+Limbo supports lists of arbitrary types as a basic construct. 
 
 ## Source
 
-### 
+### lists.b:16,20
 
+Three lists are declared. 
 
+The `len` statement can be used on a list to return the number of elements in the respective list. Note that the list `persons` is a list of tuples whose pairing is that of a string and an integer. 
 
-## Demo
+### lists.b:22,28
 
+The lists `names` and `ages` are both populated.
 
+The `::` operator functions on lists by prepending the given element before the head of the current list. Note the interaction between successive calls to the `::` operator prior to assignment and how it affects order. 
 
+### lists.b:30,46
+
+The list `persons` is built by making copies of the `names` and `ages` lists as `n` and `a`, respectively.
+
+The `hd` statement returns the head of the list to its right. 
+
+The `tl` statement returns the tail, that is the list bar the head element, of the list to its right. 
+
+An empty list is equivocal to `nil`. That is, a nil list is an empty list.
+
+Note: The effects upon temporary copies of `names` and `ages` do not affect the original lists they are copies of, as the temporary copies are copies, rather than references to the original lists. 
+
+## Demo
+
+	; limbo lists.b
+	; lists
+	Lens: 0, 0, 0
+	Lens: 3, 3, 0
+	Persons:
+	Spike: 27
+	Jet: 36
+	Ed: 13
+	Tmp lens: 0, 0
+	Lens: 3, 3, 0
+	; 
+
 ## Exercises
 
 - Try reversing a list.
+- Try interleaving two lists.
--- a/Lists/lists.b
+++ b/Lists/lists.b
@@ -3,19 +3,47 @@
 include "sys.m";
 include "draw.m";
 
-include "arg.m";
-
 sys: Sys;
 print: import sys;
 
 Lists: module {
-	init: fn(nil: ref Draw->Context, argv: list of string);
+	init: fn(nil: ref Draw->Context, nil: list of string);
 };
 
-init(nil: ref Draw->Context, argv: list of string) {
+init(nil: ref Draw->Context, nil: list of string) {
 	sys = load Sys Sys->PATH;
 
-	
+	names: list of string;
+	ages: list of int;
+	persons: list of (string, int);
+
+	print("Lens: %d, %d, %d\n", len names, len ages, len persons);
+
+	names = "Spike" :: names;
+	ages = 27 :: ages;
+
+	names = "Ed" :: "Jet" :: names;
+	ages = 13 :: 36 :: ages;
+
+	print("Lens: %d, %d, %d\n", len names, len ages, len persons);
+
+	n := names;
+	a := ages;
+
+	while(n != nil && a != nil) {
+			persons = (hd n, hd a) :: persons;
+			n = tl n;
+			a = tl a;
+	}
+
+	print("Persons:\n");
+	for(; persons != nil; persons = tl persons) {
+		(name, age) := hd persons;
+		print("%s: %d\n", name, age);
+	}
+
+	print("Tmp lens: %d, %d\n", len n, len a);
+	print("Lens: %d, %d, %d\n", len names, len ages, len persons);
 
 	exit;
 }
--- a/README.md
+++ b/README.md
@@ -38,6 +38,7 @@
 - [Switch Case](./Switch)
 - [Arrays](./Arrays)
 - [Slices](./Slices)
+- [Lists](./Lists)
 - [Functions](./Functions)
 - [Function References](./Function-Refs)
 - [Spawn](./Spawn)