APL (Dyalog), 34 Charachters
Still trying to golf it a bit more, new to APL. Tips appreciated.
y←(⍴x←⍞)-2⋄x[1],x[1+⍳y][y?y],x[⍴x]
Here is an attempt to explain it, I also simplified it a bit (no charachter improvement, though)
⋄
is a statement separator, think of it as a new line.
That leaves us with 2 statements.
y←(⍴x←⍞)-2
and x[1],x[1+⍳y][y?y],x[⍴x]
APL works from right to left in statements, but follows parentesis still, so (⍴x←⍞)
is executed first. ⍞
takes charachter input. ←
assigns that to x
and ⍴
gives the length of x
. Then the -2
is executed, which subtracts 2 from the length of x
. Finally, the length-2 is assigned to y
and we move on to the next statement.
x[⍴x]
takes the last character of x
, think of it as x[x.length]
(using the length as the index of the last character).
,
is catenate.
So we concatenate the last character of x
with x[1+⍳y][y?y]
which takes the middle indices of x
using 2+⍳y
and applies a randomization using [y?y]
.
⍳y
generates 1 2 3 ... y
and 1+
turns this into 2 3 4 ... y+1
which are the middle indices of x, for example, this returns bcdef
from abcdefg
.
[y?y]
"deals" y values from 1 to y.
So, x[1+⍳y][y?y]
grabs the middle of the word and randomizes it.
Finally, we concatenate the first charachter of x
using x[1],
to the rest of the string, and that is the output of the program.
Hopefully that was understandable...
r=id
. \$\endgroup\$id
is the identity function. I would still like to see Haskell solution to this problem in less than 100 characters. \$\endgroup\$