A simple Javascript implementation of Lisp-like cons lists, as faithful as I could think. #complete
Go to file
Ken Elf Mathieu Sternberg cf0a17ad5e Prettier. 2015-06-23 17:32:30 -07:00
bin Clean up and initial commit. 2015-05-13 16:20:16 -07:00
src Fixed my terrible, broken, no-good readme. Now with API! 2015-06-23 13:10:05 -07:00
test Added fundamental traversal tests. They passed (of course, otherwise many 2015-06-23 07:35:06 -07:00
.gitignore Changed the list structure to not respect cons(nil, nil) == nil. 2015-06-22 17:03:21 -07:00
LICENSE Merge branch 'master' of github.com:elfsternberg/cons-lists 2015-05-13 16:22:58 -07:00
Makefile Changed the list structure to not respect cons(nil, nil) == nil. 2015-06-22 17:03:21 -07:00
README.md Prettier. 2015-06-23 17:32:30 -07:00
package.json Updating package.json to include Coffeescript correctly. 2015-06-22 19:47:35 -07:00

README.md

A Javascript1 implementation of Lisp-like cons(), using vectors

1Technically, Coffeescript, but it's just Javascript.

Purpose

I kinda got tired of my broken list implementations that I was working with in each and every variant of Lisp I wrote while working my way through List In Small Pieces, so I decided to break it out into its own managed repo.

API

A cons is a singly-link list consisting of pairs, two-position objects. In a true cons list, the leftmost cell (the "car") contains a data object of interest, and the rightmost cell (the "cdr") contains a pointer to the next pair. Cons lists traditionally end with a cdr object pointing to an empty list. The following functions are provided to create, identify, traverse, and modify cons lists.

nil: An empty list, used as a static sentinel

cons(): Construct an empty list

cons(obj): Create a new list of one object

cons(obj, lst): Append an object to the head of an existing list

car(lst): Return the contents of the current cell, or nil.

cdr(lst): Return a reference to the next item in the list, or nil.

nilp(lst): (Boolean) is list an empty list?

pairp(obj): (Boolean) is this a object a pair?

listp(obj): (Boolean) is this object a list?

list(a, b, ...): Construct a list out of the arguments

vectorToList(v): Return a cons list given a vector. Recursive: if a vector is encountered inside v, it will be converted to a cons list.

listToVector(l): Return a vector given a cons list. Recursive: if car(l) is itself a cons list, the returned vector will contain an internal vector at that position.

setcar(obj, l): Replace the contents of car(l) with obj

setcdr(obj, l): Replace the contents of cdr(l) with obj

cadr(lst), cddr(lst), cdar(lst), caar(lst), caddr(lst), cdddr(lst), cadar(lst), cddar(lst), caadr(lst), cdadr(lst): Common lisp functions that extend standard list addressing.

metacadr(string): For more complex addressing, metacadr() can be provided with a string that describes the address desired, and returns the qualifying function. For example, this library does not provide caddddr(), but it can easily be generated: metacadr("caddddr") will return a function to get the data content of the fifth cell.

Copyright (c) 2015 Elf M. Sternberg

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.