Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 5.10 KB | None | 0 0
  1.  
  2. -module(a2).
  3. -export([punct/1,wsp/1,get_word/1,drop_word/1,drop_wsp/1,words/1,get_line/2,drop_line/2,lines/1,check/1]).
  4. -export([text/0,show_line/1,print_line/1,show_doc/1,print_doc/1,test/0]).
  5. -export([replicate/2,show_line_right/1,print_line_right/1,show_doc_right/1,print_doc_right/1,test_right/0]).
  6.  
  7. %% Dummy definition
  8.  
  9. dummy() -> dummy().
  10.  
  11. %% Is a character punctuation?
  12.  
  13. -spec punct(integer()) -> boolean().
  14.  
  15. punct(Ch) -> lists:member(Ch,"\'\"\.\,\ \;\:\!\?\`\(\)").
  16.  
  17. %% Is a character whitespace?
  18.  
  19. -spec wsp(integer()) -> boolean().
  20.  
  21. wsp(Ch) -> lists:member(Ch,"\ \t\n").
  22.  
  23. %% get a word from the front of the string
  24. %% split at whitespace, as defined in wsp.
  25. %% e.g.
  26. %% a2:get_word("hello? there") = "hello?"
  27.  
  28. -spec get_word(string()) -> string().
  29.  
  30. get_word(_) -> dummy().
  31.  
  32.  
  33. %% drop a word from the front of the string
  34. %% split at whitespace, as defined in wsp.
  35. %% e.g.
  36. %% a2:drop_word("hello? there") = " there"
  37.  
  38. -spec drop_word(string()) -> string().
  39.  
  40. drop_word(_) -> dummy().
  41.  
  42. %% drop whitespace from the front of the string
  43. %% e.g.
  44. %% a2:drop_wsp(" there") = "there"
  45.  
  46. -spec drop_wsp(string()) -> string().
  47.  
  48. drop_wsp(_) -> dummy().
  49.  
  50. %% Break a string into words, using the functions above.
  51. %%
  52. %% Assumption: words is always called on a string
  53. %% without white space at the start.
  54. %% e.g. a2:words("hello? there") = ["hello?","there"]
  55.  
  56. -spec words(string()) -> list(string()).
  57.  
  58. words(_) -> dummy().
  59.  
  60. %% Splitting a list of words into lines, each of
  61. %% which is a list of words.
  62.  
  63. %% To build a line, take as many words as possible, keeping
  64. %% the total length (including a single inter-word space
  65. %% between adjacent words) below the specified length, which
  66. %% is the second parameter of get_line and drop_line.
  67.  
  68. %% e.g.
  69. %% 1> a2:get_line(["When", "riding", "at", "night,"],20).                                      
  70. %% ["When","riding","at"]
  71. %% 2> a2:drop_line(["When", "riding", "at", "night,"],20).
  72. %% ["night,"]
  73.  
  74. -define(LINELEN,40).
  75.  
  76. -spec get_line(string(),integer()) -> list(string()).
  77.  
  78. get_line(_,_) -> dummy().
  79.  
  80. %% Partner function of get_line: drops a line word of words.
  81.  
  82. -spec drop_line(string(),integer()) -> list(string()).
  83.  
  84. drop_line(_,_) -> dummy().
  85.  
  86. %% Repeatedly apply get_line and drop_line to turn
  87. %% a lost of words into a list of lines i.e.
  88. %% a list of list of words.
  89.  
  90. -spec lines(list(string())) -> list(list(string())).
  91.  
  92. lines(_) -> dummy().
  93.  
  94. %% Checking that all words no longer than ?LINELEN.
  95.  
  96. -spec check(list(string())) -> boolean().
  97.  
  98. check(_) -> dummy().
  99.  
  100. %% Showing and printing lines and documents.
  101.  
  102. %% Join words, interspersed with spaces, and newline at the end.
  103.  
  104. -spec show_line(list(string())) -> string().
  105.  
  106. show_line([W]) -> W ++ "\n";
  107. show_line([W|Ws]) -> W ++ " " ++ show_line(Ws).
  108.  
  109. %% As for show_line, but padded with spaces at the start to make
  110. %% the length of the line equal to ?LISTLEN.
  111. %% May use the replicate function.
  112.  
  113. -spec show_line_right(list(string())) -> string().
  114.  
  115. show_line_right(_) -> dummy().
  116.  
  117. %% Build a list out of replicated copies of an item.
  118. %% e.g. replicate(5,3) = [3,3,3,3,3].
  119.  
  120. -spec replicate(integer(),T) -> list(T).
  121.  
  122. replicate(_,_) -> dummy().
  123.  
  124. %% Print out a line, i.e. resolve the layout.
  125.  
  126. -spec print_line(list(string())) -> ok.
  127.  
  128. print_line(X) -> io:format(show_line(X)).
  129.  
  130. %% As for print-line, but right-aligned.
  131.  
  132. -spec print_line_right(list(string())) -> ok.
  133.  
  134. print_line_right(_) -> dummy().
  135.  
  136. %% Show a whole doc, i.e. list of lines.
  137.  
  138. -spec show_doc(list(list(string()))) -> string().
  139.  
  140. show_doc(Ls) ->
  141.     lists:concat(lists:map(fun show_line/1, Ls)).
  142.  
  143. %% Show a whole doc, i.e. list of lines, right aligned.
  144.  
  145. -spec show_doc_right(list(list(string()))) -> string().
  146.  
  147. show_doc_right(_) -> dummy().
  148.  
  149. %% Print a doc.
  150.  
  151. -spec print_doc(list(list(string()))) -> ok.
  152.  
  153. print_doc(X) -> io:format(show_doc(X)).
  154.  
  155. %% Print a doc, right-aligned.
  156.  
  157. -spec print_doc_right(list(list(string()))) -> ok.
  158.  
  159. print_doc_right(_) -> dummy().
  160.  
  161. %% Test cases
  162.  
  163. text() -> "When riding at night, make sure your headlight beam is aiming slightly " ++
  164.           "downwards so oncoming traffic can " ++
  165.           "see you without being dazzled. Be sure not to dip it too much, " ++
  166.           "though, as you’ll still want to see the road around 20 metres ahead of you if riding at speed.".
  167.  
  168. test() -> print_doc(lines(words(text()))).
  169.  
  170. test_right() -> print_doc_right(lines(words(text()))).
  171.  
  172. %% Expected outputs
  173.  
  174. %% 1> a2:test().
  175. %% When riding at night, make sure your
  176. %% headlight beam is aiming slightly
  177. %% downwards so oncoming traffic can see
  178. %% you without being dazzled. Be sure not
  179. %% to dip it too much, though, as you’ll
  180. %% still want to see the road around 20
  181. %% metres ahead of you if riding at speed.
  182. %% ok
  183.  
  184. %% 2> a2:test_right().
  185. %%     When riding at night, make sure your
  186. %%        headlight beam is aiming slightly
  187. %%    downwards so oncoming traffic can see
  188. %%   you without being dazzled. Be sure not
  189. %%    to dip it too much, though, as you’ll
  190. %%     still want to see the road around 20
  191. %%  metres ahead of you if riding at speed.
  192. %% ok
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement