Guest User

Untitled

a guest
Jul 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. -module(day1).
  2. -export([count_words/1,count_to_N/1,messenger/1]).
  3.  
  4. % I don't know how to count words in a string recursively
  5. % so I'll do it like I usually do it by splitting up the string
  6. % using spaces as a boundary and computing the length of the resulting
  7. % list
  8. count_words(S) -> L = re:split(S," +"), length(L).
  9.  
  10. % recursive up counting with anonymous recursive functions. this is really weird.
  11. % I should look into the whole Y combinator business.
  12. count_to_N(N) ->
  13. Counter =
  14. fun(F,X) when X > N ->
  15. io:format("done~n",[]);
  16. (F,X) ->
  17. io:format("~w~n",[X]),F(F,X+1) end,
  18. Counter(Counter,1).
  19.  
  20. % pattern matching messenger
  21. messenger({error,Message}) -> io:format("error: ~w~n",[Message]);
  22. messenger(_) -> io:format("success~n",[]).
Add Comment
Please, Sign In to add comment