Published
Edited
Jan 10, 2020
1 star
Insert cell
Insert cell
// Create a list holding the first four positive integers
listOfInts = lib.array2list([1,2,3,4])
Insert cell
// Generic multiplier function
multiplier = val1 => val2 => val1 * val2
Insert cell
// Partial function to double a number
doubler = multiplier(2)
Insert cell
Insert cell
IS_EMPTY =
pair => (lib.HEAD(pair) === null && lib.TAIL(pair) === null)
? lib.TRUE
: lib.FALSE
Insert cell
IS_LAST = pair => IS_EMPTY(lib.TAIL(pair))
Insert cell
Insert cell
// First attempt at the recursive function used by MAP
make_map1 =
next_map =>
new_list =>
fn =>
list =>
(list_acc =>
// Have we reached the end of the list?
IS_LAST(list)
// Yup, so we're done
(list_acc)

// Nope, so recursively call ourselves passing
// in the list accumulator, the function being
// applied and whatever is left in the current
// list
(f => (next_map(list_acc)
(fn)
(lib.TAIL(list)))
(f))
)
// Apply the mapping function to the first list item and
// append the result to the start of the new list. This
// value is then received as argument list_acc in the
// function above
(lib.PAIR(fn(lib.HEAD(list)))(new_list))
Insert cell
// Create the MAP function using the Y-Combinator and an initial empty list
MAP1 = lib.Y(make_map1)(lib.EMPTY_LIST)
Insert cell
Insert cell
doubled_list1 = MAP1(doubler)(listOfInts)
Insert cell
Insert cell
Insert cell
do_reverse =
next_rev =>
new_list =>
old_list =>
IS_LAST(old_list)
(lib.PAIR(lib.HEAD(old_list))(new_list))
(f => (next_rev(lib.PAIR(lib.HEAD(old_list))(new_list))
(lib.TAIL(old_list)))
(f))
Insert cell
REVERSE = lib.Y(do_reverse)(lib.EMPTY_LIST)
Insert cell
Insert cell
// Second attempt at the recursive function used by MAP
make_map2 =
next_map =>
new_list =>
fn =>
list =>
(list_acc =>
// Have we reached the end of the list?
IS_LAST(list)
// Yup, so reverse the items in the list and
// we're done
(f => REVERSE(list_acc)(f))

// Nope, so recursively call ourselves passing
// in the list accumulator, the function being
// applied and whatever is left in the current
// list
(f => (next_map(list_acc)
(fn)
(lib.TAIL(list)))
(f))
)
// Apply the mapping function to the first list item and
// append the result to the start of the new list. This
// value is then received as argument list_acc in the
// function above
(lib.PAIR(fn(lib.HEAD(list)))(new_list))
Insert cell
// Create the MAP function using the Y-Combinator and an initial empty list
// This version of MAP now returns the items in the correct order!
MAP = lib.Y(make_map2)(lib.EMPTY_LIST)
Insert cell
Insert cell
doubled_list2 = MAP(doubler)(listOfInts)
Insert cell
// We're expecting the list to contain [2,4,6,8]...
lib.list2array(doubled_list2)
Insert cell
Insert cell
{
// Create a list hold 4 numbers
var listOfInts = lib.array2list([1,2,3,4]);

// Generic multiplier function
var multiplier = val1 => val2 => val1 * val2;

// Functions to perform variety of numerical operations
var doubler = multiplier(2);
var tripler = multiplier(3);
var squarer = val => multiplier(val)(val);
var sqrter = val => Math.sqrt(val);

return {
double : lib.list2array(MAP(doubler)(listOfInts))
, triple : lib.list2array(MAP(tripler)(listOfInts))
, squared : lib.list2array(MAP(squarer)(listOfInts))
, sqrt : lib.list2array(MAP(sqrter)(listOfInts))
}
}
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