Look-and-say sequence: Difference between revisions

add JavaScript, ws
(→‎{{header|Perl}}: added php)
(add JavaScript, ws)
Line 17:
 
=={{header|Ada}}==
<lang ada>with Ada.Text_IO, Ada.Strings.Fixed;
with Ada.Text_IO, Ada.Strings.Fixed;
use Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;
 
Line 30 ⟶ 29:
end loop;
return Trim (Integer'Image (S'Length), Both) & Item;
end "+"; </lang>
</lang>
This function can be used as follows:
<lang ada>Put_Line (+"1");
Put_Line (+"1");
Put_Line (+(+"1"));
Put_Line (+(+(+"1")));
Line 43 ⟶ 40:
Put_Line (+(+(+(+(+(+(+(+"1"))))))));
Put_Line (+(+(+(+(+(+(+(+(+"1")))))))));
Put_Line (+(+(+(+(+(+(+(+(+(+"1")))))))))); </lang>
</lang>
Sample output:
<pre>
Line 303 ⟶ 299:
 
=={{header|D}}==
<lang d>import std.stdio;
import std.stdio;
import std.string;
void main() {
Line 334 ⟶ 329:
output = toString(count)~last~output;
return output;
} </lang>
</lang>
 
=={{header|E}}==
Line 366 ⟶ 360:
 
=={{header|Forth}}==
<lang forth>create buf1 256 allot
create buf1 256 allot
create buf2 256 allot
buf1 value src
Line 394 ⟶ 387:
0 do next-look-and-say cr src count type loop ;
 
10 look-and-say </lang>
</lang>
 
=={{header|Fortran}}==
Line 446 ⟶ 438:
end do
end program LookAndSayTest </lang>
</lang>
 
=={{header|Haskell}}==
 
<lang haskell>import Control.Monad (liftM2)
import Control.Monad (liftM2)
import Data.List (group)
 
Line 476 ⟶ 466:
where describe run = show (length run) ++ take 1 run
 
main = mapM_ print (iterate lookAndSay 1) -- display sequence until interrupted </lang>
</lang>
 
=={{header|J}}==
Line 532 ⟶ 521:
31131211131221
13211311123113112211</pre>
 
=={{header|JavaScript}}==
{{trans|Perl}} {{works with|JavaScript|1.6}}
<lang javascript>function lookandsay(str) {
var next = "";
str.match(/(.)\1*/g).forEach(function(seq){next += seq.length.toString() + seq[0]})
return next;
 
var num = "1";
for (var i = 10; i > 0; i--) {
print(num);
num = lookandsay(num);
}</lang>
 
=={{header|M4}}==
Using regular expressions:
{{trans|Perl}}
<lang M4>divert(-1)
divert(-1)
define(`for',
`ifelse($#,0,``$0'',
Line 552 ⟶ 554:
`v
define(`v',las(v))')dnl
v </lang>
v
</lang>
 
=={{header|Mathematica}}==
Custom Functions:
<lang Mathematica>RunLengthEncode[x_List]:=(Through[{First,Length}[#]]&)/@Split[x]
LookAndSay[n_,d_:1]:=NestList[Flatten[Reverse/@RunLengthEncode[#]]&,{d},n-1] </lang>
RunLengthEncode[x_List]:=(Through[{First,Length}[#]]&)/@Split[x]
LookAndSay[n_,d_:1]:=NestList[Flatten[Reverse/@RunLengthEncode[#]]&,{d},n-1]
</lang>
If second argument is omitted the sequence is started with 1. Second argument is supposed to be a digits from 0 to 9. If however a larger number is supplied it will be seen as 1 number, not multiple digits. However if one wants to start with a 2 or more digit number, one could reverse the sequence to go back to a single digit start. First example will create the first 13 numbers of the sequence starting with 1, the next example starts with 7:
<lang Mathematica> FromDigits /@ LookAndSay[13] // Column
FromDigits /@ LookAndSay[13, 7] // Column </lang>
FromDigits /@ LookAndSay[13, 7] // Column
</lang>
gives back:
<pre style='height:15em;overflow:scroll'>1
<lang Mathematica>
1
11
21
Line 594 ⟶ 590:
132113213221133112132123222117
11131221131211132221232112111312111213322117
31131122211311123113321112131221123113111231121123222117 </pre>
</lang>
 
=={{header|MAXScript}}==
Line 782 ⟶ 777:
=={{header|R}}==
Returning the value as an integer limits how long the sequence can get, so the option for integer or character return values are provided.
<lang R>look.and.say <- function(x, return.an.int=FALSE)
<lang R>
look.and.say <- function(x, return.an.int=FALSE)
{
#convert number to character vector
Line 797 ⟶ 791:
#convert to number, if desired
if(return.an.int) as.integer(newstr) else newstr
} </lang>
}
</lang>
Example usage.
<lang R>x <- 1
x <- 1
for(i in 1:10)
{
x <- look.and.say(x)
print(x)
} </lang>
}
</lang>
 
=={{header|Ruby}}==
Line 902 ⟶ 893:
Each new sequence is inserted as a new line. 10 sequences are created in this example.
 
<lang vedit>Repeat(10) {
Repeat(10) {
BOL
Reg_Empty(20)
Line 912 ⟶ 902:
}
Ins_Newline Reg_Ins(20)
} </lang>
}
</lang>
 
Output:
Anonymous user