Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function make_first_line(words, page_width) {
- let weight = -1;
- let accept = "";
- let leftover = [];
- for (let i = words; !is_empty_list(i); i = tail(i)) {
- weight = weight + head(i).length + 1;
- if (weight <= page_width) {
- if (!equal(i, words)) {
- accept = accept + " " + head(i);
- } else {
- accept = accept + head(i);
- }
- } else {
- leftover = (i);
- break;
- }
- }
- return pair(accept, leftover);
- }
- function make_lines(words, page_width) {
- const stack = make_first_line(words, page_width);
- if (is_empty_list(words)) {
- return [];
- } else {
- return pair(head(stack), make_lines(tail(stack), page_width));
- }
- }
- function tail_n_times(xs, n) {
- if (is_empty_list(xs)) {
- return xs;
- } else if (n <= 0) {
- return xs;
- } else {
- return tail_n_times(tail(xs), n - 1);
- }
- }
- //cuts away n heads
- function copy_list_n_times(xs, n) {
- if (is_empty_list(xs)) {
- return xs;
- } else if (n <= 0) {
- return [];
- } else {
- return pair(head(xs), copy_list_n_times(tail(xs), n - 1));
- }
- }
- //extracts first n part of list
- function make_pages(lines, page_height) {
- if (is_empty_list(lines)) {
- return [];
- } else {
- return pair(copy_list_n_times(lines, page_height), make_pages(
- tail_n_times(lines, page_height), page_height));
- }
- }
- function page_format(words, page_width, page_height) {
- return make_pages(make_lines(words, page_width), page_height);
- }
Add Comment
Please, Sign In to add comment