forked from julycoding/The-Art-Of-Programming-By-July-2nd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
开个Erlang的头,添加了位移字符串的递归方法。
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
%%%----------------------------------- | ||
%%% @Module : july_1_1 | ||
%%% @Author : hejavac | ||
%%% @Email : hejavac@gmail.com | ||
%%% @Created : 创建日期 | ||
%%% @Description : 1.1、左旋转字符串 : 字符串旋转问题,例如abcdef 左旋2位 变成 cdefab | ||
%%%----------------------------------- | ||
|
||
-module(july_1_1). | ||
|
||
-compile(export_all). | ||
|
||
%% 启动 | ||
%% Str:字符串 | ||
%% Step:位移量 | ||
%% 返回:位移后的字符串 | ||
start(Str, Step) -> | ||
shift(Str, Step). | ||
|
||
%% 字符串位移递归函数 | ||
%% Str:字符串 | ||
%% Step:位移量 | ||
%% 返回:位移后的字符串 | ||
shift(S, 0) -> | ||
S; | ||
shift([T|S], Step) -> | ||
%或者是:shift(lists:concat([S, [T]]), Step-1). | ||
shift(S ++ [T], Step-1). | ||
|
||
%% 测试(bcdef 左旋2位 变成 cdefab) | ||
test() -> | ||
dbg:tracer(), | ||
dbg:p(all,[c]), | ||
dbg:tpl(?MODULE, [{'_', [], [{return_trace}]}]), | ||
start("abcdef", 2). |