Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.70 KB | None | 0 0
  1. -module(shunt).
  2. -import(list,[position/2,take/2,drop/2,append/2]).
  3. -export([find/2]).
  4. -export([split/2]).
  5. -export([few/2]).
  6.  
  7. find([],[]) -> [];
  8. find(Xs,Ys)-> {Hs,Ts}=split(Xs,hd(Ys)),
  9.           State = append(Ts,Hs),
  10.               Lone = length(Ts)+1,
  11.               Ltwo = length(Hs),
  12.           [{one,Lone},{two,Ltwo},{one,-Lone},{two,-Ltwo}|find(State,tl(Ys))].
  13.  
  14. split(Train,E) ->
  15.     N = position(Train,E), {take(Train,N-1),drop(Train,N)}.
  16.  
  17. few([],[]) -> [];
  18. few(Xs,Ys)->
  19.     if
  20.     hd(Xs) == hd(Ys) ->
  21.         find(tl(Xs),tl(Ys));
  22.         hd(Xs) /= hd(Ys) ->
  23.         {Hs,Ts}=split(Xs,hd(Ys)),
  24.         State = append(Ts,Hs),
  25.         [{one,length(Ts)+1},{two,length(Hs)},{one,-(length(Ts)+1)},{two,-length(Hs)}|find(State,tl(Ys))]
  26.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement