Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Commonmark migration
Source Link

Erlang, 188 172 132 chars

f([H|C=[_|_]])->T=[lists:last(C)],[H|s(C--T,T)];f(X)->X. s([],N)->N;s(D,N)->E=[lists:nth(random:uniform(length(D)),D)],s(D--E,E++N).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module):

-module(string_shuffle).
-export([f/1]).

f([H|C=[_|_]])->
    T=[lists:last(C)],
    [H|s(C--T,T)];f(X)->X.
f(X)->X.

s([],N)->N;
s(D,N)->
    E=[lists:nth(random:uniform(length(D)),D)],
    s(D--E,E++N).

Edit

##Edit TookTook the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

Edit 2

##Edit 2 RestructuredRestructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

Erlang, 188 172 132 chars

f([H|C=[_|_]])->T=[lists:last(C)],[H|s(C--T,T)];f(X)->X. s([],N)->N;s(D,N)->E=[lists:nth(random:uniform(length(D)),D)],s(D--E,E++N).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module):

-module(string_shuffle).
-export([f/1]).

f([H|C=[_|_]])->
    T=[lists:last(C)],
    [H|s(C--T,T)];f(X)->X.
f(X)->X.

s([],N)->N;
s(D,N)->
    E=[lists:nth(random:uniform(length(D)),D)],
    s(D--E,E++N).

##Edit Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

##Edit 2 Restructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

Erlang, 188 172 132 chars

f([H|C=[_|_]])->T=[lists:last(C)],[H|s(C--T,T)];f(X)->X. s([],N)->N;s(D,N)->E=[lists:nth(random:uniform(length(D)),D)],s(D--E,E++N).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module):

-module(string_shuffle).
-export([f/1]).

f([H|C=[_|_]])->
    T=[lists:last(C)],
    [H|s(C--T,T)];f(X)->X.
f(X)->X.

s([],N)->N;
s(D,N)->
    E=[lists:nth(random:uniform(length(D)),D)],
    s(D--E,E++N).

Edit

Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

Edit 2

Restructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

deleted 2 characters in body
Source Link

Erlang, 188 172 133132 chars

f([H|C=[_|_]])->T=[lists:last(C)],[H|s(C--T,T)];f(X)->X. s([],N)->N;s(D,N)->E=lists>E=[lists:nth(random:uniform(length(D)),D)],s(D--[E]E,[E|N]E++N).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module):

-module(string_shuffle).
-export([f/1]).

f([H|C=[_|_]])->
    T=[lists:last(C)],
    [H|s(C--T,T)];f(X)->X.
f(X)->X.

s([],N)->N;
s(D,N)->
    E=listsE=[lists:nth(random:uniform(length(D)),D)],
    s(D--[E]E,[E|N]E++N).

##Edit Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

##Edit 2 Restructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

Erlang, 188 172 133 chars

f([H|C=[_|_]])->T=[lists:last(C)],[H|s(C--T,T)];f(X)->X. s([],N)->N;s(D,N)->E=lists:nth(random:uniform(length(D)),D),s(D--[E],[E|N]).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module):

-module(string_shuffle).
-export([f/1]).

f([H|C=[_|_]])->
    T=[lists:last(C)],
    [H|s(C--T,T)];f(X)->X.
f(X)->X.

s([],N)->N;
s(D,N)->
    E=lists:nth(random:uniform(length(D)),D),
    s(D--[E],[E|N]).

##Edit Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

##Edit 2 Restructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

Erlang, 188 172 132 chars

f([H|C=[_|_]])->T=[lists:last(C)],[H|s(C--T,T)];f(X)->X. s([],N)->N;s(D,N)->E=[lists:nth(random:uniform(length(D)),D)],s(D--E,E++N).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module):

-module(string_shuffle).
-export([f/1]).

f([H|C=[_|_]])->
    T=[lists:last(C)],
    [H|s(C--T,T)];f(X)->X.
f(X)->X.

s([],N)->N;
s(D,N)->
    E=[lists:nth(random:uniform(length(D)),D)],
    s(D--E,E++N).

##Edit Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

##Edit 2 Restructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

added 157 characters in body
Source Link

Erlang, 188 172172 133 chars

f([])->[];f([H|[]])->[H];f([H|C][H|C=[_|_]])->[T|D]=lists>T=[lists:reverselast(C)],[H|s(length(D),DC--T,[]T)]++[T]];f(X)->X. s(0,_[],N)->N;s(L,D,N)->E=lists:nth(random:uniform(Llength(D)),D),s(L-1,lists:delete(E,D)--[E],[E|N]).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module): -module(string_shuffle). -export([f/1]).

f-module([]string_shuffle)->[];.
f-export([H|[]][f/1])->[H];    .

f([H|C][H|C=[_|_]])->    
    [T|D]=listsT=[lists:reverselast(C)],    
    [H|s(lengthC--T,T)];f(DX),D,[]->X.
f(X)]++[T]->X.

s(0,_[],N)->
    N;>N;
s(L,D,N)->
    E=lists:nth(random:uniform(Llength(D)),D),
    s(L-1,lists:delete(E,D)--[E],[E|N]).

##Edit Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

##Edit 2 Restructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

Erlang, 188 172 chars

f([])->[];f([H|[]])->[H];f([H|C])->[T|D]=lists:reverse(C),[H|s(length(D),D,[])]++[T]. s(0,_,N)->N;s(L,D,N)->E=lists:nth(random:uniform(L),D),s(L-1,lists:delete(E,D),[E|N]).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module): -module(string_shuffle). -export([f/1]).

f([])->[];
f([H|[]])->[H];    
f([H|C])->    
    [T|D]=lists:reverse(C),    
    [H|s(length(D),D,[])]++[T].

s(0,_,N)->
    N;
s(L,D,N)->
    E=lists:nth(random:uniform(L),D),
    s(L-1,lists:delete(E,D),[E|N]).

##Edit Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

Erlang, 188 172 133 chars

f([H|C=[_|_]])->T=[lists:last(C)],[H|s(C--T,T)];f(X)->X. s([],N)->N;s(D,N)->E=lists:nth(random:uniform(length(D)),D),s(D--[E],[E|N]).

I'm still learning Erlang so any tips on making this shorter are appreciated.

full code(string_shuffle module):

-module(string_shuffle).
-export([f/1]).

f([H|C=[_|_]])->
    T=[lists:last(C)],
    [H|s(C--T,T)];f(X)->X.
f(X)->X.

s([],N)->N;
s(D,N)->
    E=lists:nth(random:uniform(length(D)),D),
    s(D--[E],[E|N]).

##Edit Took the shuffle part out as a seperate function which no longer requires the head and tail of the list to be passed around.

##Edit 2 Restructured to remove one of the ffunction patterns, changed the shuffle function to accept only two parameters, changed lists:delete for --[], swapped a lists:reverse call for a lists:last

added 124 characters in body
Source Link
Loading
Source Link
Loading