Skip to content

Instantly share code, notes, and snippets.

@german
Created April 5, 2013 12:23
Show Gist options
  • Save german/5318900 to your computer and use it in GitHub Desktop.
Save german/5318900 to your computer and use it in GitHub Desktop.
Returns number and substrings themselves from the given string
-module(substrings).
-compile(export_all).
substring(Set, T, Start, LengthOfSubstr) ->
S = lists:sublist(T, Start, LengthOfSubstr),
StringLength = string:len(T),
if
(Start >= StringLength) or (S =:= []) -> sets:to_list(Set);
%% substring is eql to string || start position + length of substring greater or equal to string's length
(S =:= T) or (Start + LengthOfSubstr >= StringLength) ->
substring(sets:add_element(S, Set), T, Start + 1, 1); %% shifted to 1 symbol right and start with 1 length of substring
true -> substring(sets:add_element(S, Set), T, Start, LengthOfSubstr + 1)
end.
substring(T) ->
io:format("~p~n", [substring(sets:new(), T, 1, 1)]).
@german
Copy link
Author

german commented Apr 5, 2013

35> substrings:substring("Hello, World").
["Hell","e","lo, Wo","llo, W","r","Hello, W","llo, Worl","lo,","lo, Wor","o",
"Hello","ello, Worl","l",",",", ",", Wor"," Worl","Worl","el","o, Wo","or",
"Hello, Wo","lo, W","llo, ","lo, Worl","o,","o, Wor",", W","ello,","ello, ",
"ello, Wor"," "," Wor","Wor","He","ell","orl","o, W",", Wo","ello, W","llo",
"lo, ","o, Worl"," W","W","Hello, Worl","ello","Hel","llo, Wo","ello, Wo",
" Wo","Wo","rl","llo,","llo, Wor","lo","o, ","H","Hello,","Hello, ",
"Hello, Wor","ll",", Worl"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment