git @ Cat's Eye Technologies Decoy / master doc / Lists.md
master

Tree @master (Download .tar.gz)

Lists.md @masterview markup · raw · history · blame

Decoy Lists

A "list" in Decoy is a cons list. But you need to import the cons list library in order to get it.

And there is no pressing need to use it. It's not necessary to build (essentially) all structures out of cons cells and vectors, like it is in Scheme. And it's not necessary to consider the syntax based on them either.

  • (list ...)
  • (list? expr)

And because you don't get cons lists, you don't get quote neither. So some of this is just going to go in the garbage.

-> Functionality "Interpret Consful Decoy Program" is implemented by shell command
-> "./bin/decoy eval -I lib/ -m stdenv -m list %(test-body-file)"

-> Functionality "Interpret Consful Decoy Program" is implemented by shell command
-> "./bin/decoy compile -I lib/ -m stdenv -m list %(test-body-file) > out.mjs && node out.mjs"

Cons Lists

-> Tests for functionality "Interpret Consful Decoy Program"

cons lets you create a list from some thing and another list. list, given no arguments, returns the empty list.

(cons "thing" (cons "rest" (list)))
===> ("thing" "rest")

cons can be used to create an improper list. (The . syntax for improper lists is not supported in the reader though.)

(cons "thing" "rest")
===> ("thing" . "rest")

car extracts the head of a list.

(car (cons "thing" (cons "rest" (list))))
===> "thing"

cdr extracts the tail of a list.

(cdr (cons "thing" (cons "rest" (list))))
===> ("rest")

list is a variadic function that makes lists.

(let* ((a "hi") (b "there")) (list a b))
===> ("hi" "there")

(let* ((a 1) (b 2)) (list a b 3))
===> (1 2 3)

(list)
===> ()

Predicates and Types

Are lists truthy? They are.

(if (list 1) "true" "false")
===> "true"

(if (list) "true" "false")
===> "true"

equal? should work on lists. But equal? will need to be defined elsewhere!

> (equal? (list 1 2 3)
>         (cons 1 (cons 2 (cons 3 (list)))))
> ===> #t

list?

A string is not a list.

(list? "string")
===> #f

A list whose final cons cell's tail contains a null, is a list.

(list? (cons "a" (list)))
===> #t

A pair (or improper list) is not a list.

(list? (cons "a" "b"))
===> #f

Booleans are not lists.

(list? (or))
===> #f

Lambda functions are not lists.

(list? (lambda (x y) (y x)))
===> #f

But the empty list is a list.

(list? (list))
===> #t

pair?

A pair is a pair.

(pair? (cons "a" "b"))
===> #t

But other things aren't.

(pair? (list))
===> #f

(pair? 123)
===> #f

(pair? "puppy123")
===> #f

null?

(null? (list))
===> #t

(null? (list 1 2 3))
===> #f

length

(length (cons 1 (cons 2 (cons 3 (list)))))
===> 3

(length (list "a" "b" "c"))
===> 3

(length (list))
===> 0

(length "zzz")
===> 0

(length 4)
===> 0

fold

(fold (lambda (x acc) (+ x acc)) 0 (list 4 16 1))
===> 21