Published
Edited
Jan 10, 2020
Insert cell
Insert cell
{
var EMPTY = lib.PAIR(null)(null);
var is_empty = pair => (lib.HEAD(pair) === null) &&
(lib.TAIL(pair) === null);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Create a list consisting of a single item and the special
// termination item EMPTY
var list1 = lib.PAIR("A")(EMPTY);
var result = {};
result.list1_head = lib.HEAD(list1);
result.list1_tail = lib.TAIL(list1);
result.list1_is_empty = is_empty(list1);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Add a second item to the list
var list2 = lib.PAIR("B")(list1);
result.list2_head = lib.HEAD(list2);
result.list2_tail = lib.TAIL(list2);

result.list2_head_tail = lib.HEAD(lib.TAIL(list2));
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Add a third item
var list3 = lib.PAIR("C")(list2);
result.list3_head = lib.HEAD(list3);
result.list3_tail = lib.TAIL(list3);

result.list3_head_tail = lib.HEAD(lib.TAIL(list3));
result.list3_tail_tail = lib.TAIL(lib.TAIL(list3));

result.list3_head_tail_tail = lib.HEAD(lib.TAIL(lib.TAIL(list3)));
return result;
}
Insert cell
Insert cell
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Convert a character string to a list whilst preserving the
// character order
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
var make_str2list =
fn_next=>
list =>
str => (str.length > 0)
? fn_next(lib.PAIR(str.substr(-1))(list))(str.slice(0,-1))
: list;
var str2list = str => lib.Y(make_str2list)(lib.EMPTY)(str);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Convert a list back into a character string
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
var make_list2str =
fn_next=>
str =>
list => lib.is_last(list)
? str.concat(lib.HEAD(list))
: fn_next(str.concat(lib.HEAD(list)))(lib.TAIL(list));
var list2str = list => lib.Y(make_list2str)("")(list);
var l = str2list("Hello world!");
return {
list : l,
list_char1 : lib.HEAD(l),
list_char2 : lib.HEAD(lib.TAIL(l)),
list_char3 : lib.HEAD(lib.TAIL(lib.TAIL(l))),
list_as_string : list2str(l)
}
}
Insert cell
Insert cell
// Create our own push function that returns the new array rather
// than the index of the newly inserted item
push = arr => val => (idx => arr)(arr.push(val))
Insert cell
Insert cell
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Convert a character string to a list whilst preserving the
// character order
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
var make_str2list =
fn_next=>
list =>
str => (str.length > 0)
? fn_next(lib.PAIR(str.substr(-1))(list))(str.slice(0,-1))
: list;
var str2list = str => lib.Y(make_str2list)(lib.EMPTY)(str);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Convert a JavaScript array into a list
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
var make_array2list =
fn_next=>
list =>
array => (array.length === 0)
? list
: fn_next(lib.PAIR(array[array.length-1])(list))
(array.slice(0,-1));
var array2list = lib.Y(make_array2list)(lib.EMPTY);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Convert a list to a JavaScript array
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
var make_list2array =
fn_next=>
arr =>
list => lib.is_last(list)
? push(arr)(lib.HEAD(list))
: fn_next(push(arr)(lib.HEAD(list)))(lib.TAIL(list));
var list2array = list => lib.Y(make_list2array)([])(list);
var list_of_nums = array2list([1,2,3,4]);
var list_of_chars = str2list("Hello");

return {
list_of_nums : list_of_nums,
list_of_nums_1st : lib.HEAD(list_of_nums),
list_of_nums_2nd : lib.HEAD(lib.TAIL(list_of_nums)),
list_of_nums_3rd : lib.HEAD(lib.TAIL(lib.TAIL(list_of_nums))),
list_of_chars : list_of_chars,
list_of_chars_2_array : list2array(list_of_chars)
}
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more