Advertisement
Guest User

Untitled

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