Guest User

Untitled

a guest
Nov 14th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. function make_first_line(words, page_width) {
  2. let weight = -1;
  3. let accept = "";
  4. let leftover = [];
  5. for (let i = words; !is_empty_list(i); i = tail(i)) {
  6. weight = weight + head(i).length + 1;
  7. if (weight <= page_width) {
  8. if (!equal(i, words)) {
  9. accept = accept + " " + head(i);
  10. } else {
  11. accept = accept + head(i);
  12. }
  13. } else {
  14. leftover = (i);
  15. break;
  16. }
  17. }
  18. return pair(accept, leftover);
  19. }
  20.  
  21. function make_lines(words, page_width) {
  22. const stack = make_first_line(words, page_width);
  23. if (is_empty_list(words)) {
  24. return [];
  25. } else {
  26. return pair(head(stack), make_lines(tail(stack), page_width));
  27. }
  28. }
  29.  
  30. function tail_n_times(xs, n) {
  31. if (is_empty_list(xs)) {
  32. return xs;
  33. } else if (n <= 0) {
  34. return xs;
  35. } else {
  36. return tail_n_times(tail(xs), n - 1);
  37. }
  38. }
  39. //cuts away n heads
  40.  
  41. function copy_list_n_times(xs, n) {
  42. if (is_empty_list(xs)) {
  43. return xs;
  44. } else if (n <= 0) {
  45. return [];
  46. } else {
  47. return pair(head(xs), copy_list_n_times(tail(xs), n - 1));
  48. }
  49. }
  50. //extracts first n part of list
  51.  
  52. function make_pages(lines, page_height) {
  53. if (is_empty_list(lines)) {
  54. return [];
  55. } else {
  56. return pair(copy_list_n_times(lines, page_height), make_pages(
  57. tail_n_times(lines, page_height), page_height));
  58. }
  59. }
  60.  
  61.  
  62. function page_format(words, page_width, page_height) {
  63. return make_pages(make_lines(words, page_width), page_height);
  64. }
Add Comment
Please, Sign In to add comment