Look-and-say sequence: Difference between revisions

Add ABC
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Add ABC)
 
(17 intermediate revisions by 9 users not shown)
Line 221:
newline: db 13,10,'$' ; Newline to print in between members
memb: db '1$' ; This is where the current member is stored</syntaxhighlight>
 
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN look.and.say seq:
PUT "" IN result
PUT 0 IN n
PUT "" IN c
FOR ch IN seq:
SELECT:
c=ch:
PUT n+1 IN n
ELSE:
IF n>0: PUT result^"`n`"^c IN result
PUT 1 IN n
PUT ch IN c
RETURN result^"`n`"^c
 
PUT "1" IN item
 
FOR i IN {1..14}:
WRITE item/
PUT look.and.say item IN item</syntaxhighlight>
{{out}}
<pre>1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211
1321132132111213122112311311222113111221131221
11131221131211131231121113112221121321132132211331222113112211</pre>
 
 
Line 630 ⟶ 667:
 
=={{header|BASIC}}==
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC|any}}
{{works with|Run BASIC}}
{{works with|Just BASIC}}
<syntaxhighlight lang="basic">10 DEFINT A-Z: I$="1"
20 FOR Z=1 TO 15
Line 658 ⟶ 703:
11131221131211131231121113112221121321132132211331222113112211
311311222113111231131112132112311321322112111312211312111322212311322113212221</pre>
 
==={{header|Applesoft BASIC}}===
{{trans|BASIC}}
<syntaxhighlight lang="vb">10 I$="1"
20 FOR Z=1 TO 15
30 PRINT I$
40 O$=""
50 FOR I=1 TO LEN(I$)
60 C=1
70 IF MID$(I$,I,1)=MID$(I$,I+C,1) THEN C=C+1: GOTO 70
80 O$=O$+CHR$(C+48)+MID$(I$,I,1)
90 I=I+C-1
100 NEXT I
110 I$=O$
120 NEXT Z</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 dim x$(2)
120 i = 0 ' índice de cadena de entrada
130 x$(i) = "1"
140 input "Indica cuantas repeticiones: ",r
150 print "Secuencia:"
160 print x$(i)
170 for n = 1 to r-1
180 j = 1-i ' índice de cadena de salida
190 x$(j) = ""
200 k = 1
210 while k <= len(x$(i))
220 k0 = k+1
230 while ((k0 <= len(x$(i))) and (mid$(x$(i),k,1) = mid$(x$(i),k0,1)))
240 k0 = k0+1
250 wend
260 x$(j) = x$(j)+str$(k0-k)+mid$(x$(i),k,1)
270 k = k0
280 wend
290 i = j
300 print x$(j)
310 next n
320 end</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LET i$ = "1"
FOR z = 1 TO 15
PRINT i$
LET o$ = ""
FOR i = 1 TO LEN(i$)
LET c = 1
DO WHILE (i$)[i:i+1-1] = (i$)[i+c:i+c+1-1]
LET c = c+1
LOOP
LET o$ = o$ & CHR$(c+48) & (i$)[i:i+1-1]
LET i = i+c-1
NEXT i
LET i$ = o$
NEXT z
END</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry.</pre>
 
=={{header|BASIC256}}==
Line 1,074 ⟶ 1,188:
<syntaxhighlight lang="clojure">user> (take 8 (iterate look-and-say 1))
(1 11 21 1211 111221 312211 13112221 1113213211)</syntaxhighlight>
 
-----
 
The math above is lovely, Clojure using Java's regular expressions is also powerful:
 
<syntaxhighlight lang="clojure">(defn look-and-say
[n]
(->> (re-seq #"(.)\1*" n)
(mapcat (comp (juxt count first) first))
(apply str)))
 
(take 12 (iterate look-and-say "1"))</syntaxhighlight>
 
{{out}}
 
<syntaxhighlight lang="clojure">("1"
"11"
"21"
"1211"
"111221"
"312211"
"13112221"
"1113213211"
"31131211131221"
"13211311123113112211"
"11131221133112132113212221"
"3113112221232112111312211312113211")</syntaxhighlight>
 
=={{header|CLU}}==
Line 1,766 ⟶ 1,907:
number := lookAndSayNext(number)
}</syntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
proc lookandsay . a$ .
c = 1
p$ = substr a$ 1 1
for i = 2 to len a$
if p$ = substr a$ i 1
c += 1
else
s$ &= c & p$
p$ = substr a$ i 1
c = 1
.
.
s$ &= c & p$
swap a$ s$
.
b$ = 1
print b$
for k = 1 to 10
lookandsay b$
print b$
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 2,524 ⟶ 2,691:
13211311123113112211
11131221133112132113212221</pre>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">(function look-and-say n x
(return-when (empty? n) x)
(let digit (0 n)
[before after] (part-before (!= digit) n))
(recur after (strn x (len before) digit)))
 
(var result "1")
(loop 10 i
(print (var! result look-and-say)))</syntaxhighlight>
 
=={{header|J}}==
Line 2,925 ⟶ 3,103:
v</syntaxhighlight>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE LOKSAY
.MCALL .TTYOUT,.EXIT
LOKSAY::MOV #START,R0
MOV #BUFR1,R1
JSR PC,COPY
MOV #^D14,R5
$1: MOV #BUFR1,R1
JSR PC,PRINT
MOV #NEWLIN,R1
JSR PC,PRINT
JSR PC,STEP
SOB R5,$1
.EXIT
STEP: MOV #BUFR1,R0
MOV #BUFR2,R1
BR 2$
1$: INC R3
CMPB (R0)+,R4
BEQ 1$
ADD #60,R3
MOVB R3,(R1)+
MOVB R4,(R1)+
DEC R0
2$: CLR R3
MOVB (R0)+,R4
BNE 1$
MOV #BUFR2,R0
MOV #BUFR1,R1
COPY: MOVB (R0)+,(R1)+
BNE COPY
RTS PC
PRINT: MOVB (R1)+,R0
.TTYOUT
BNE PRINT
RTS PC
NEWLIN: .BYTE 15,12,0,0
START: .ASCIZ /1/
BUFR1: .BLKB 400
BUFR2: .BLKB 400
.END LOKSAY</syntaxhighlight>
{{out}}
<pre>1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211
1321132132111213122112311311222113111221131221
11131221131211131231121113112221121321132132211331222113112211</pre>
=={{header|Maple}}==
<syntaxhighlight lang="maple">generate_seq := proc(s)
Line 2,967 ⟶ 3,201:
"13211311123113112211"
</pre>
 
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 3,032 ⟶ 3,265:
"31131211131221"
"13211311123113112211" */</syntaxhighlight>
 
Implementation treating the sequence as numbers
 
<syntaxhighlight lang="maxima">
ciphers(n):=block(makelist(floor(mod(n, 10^(k+1)) / 10^k), k, 0,floor(log(n)/log(10))),reverse(%%));
 
collect(a) := block(
[n: length(ciphers(a)), b: [ ], x: ciphers(a)[1], m: 1],
for i from 2 thru n do
(if ciphers(a)[i] = x then m: m + 1 else (b: endcons([x, m], b), x: ciphers(a)[i], m: 1)),
b: endcons([x, m], b),
map(reverse,%%),
flatten(%%),
at(sum((part(%%,k))*y^(length(%%)-k),k,1,length(%%)),y=10)
)$
 
block(i:1,append([i],makelist(i:collect(i),9)),table_form(%%));
/* matrix(
[1],
[11],
[21],
[1211],
[111221],
[312211],
[13112221],
[1113213211],
[31131211131221],
[13211311123113112211]
) */
</syntaxhighlight>
 
=={{header|MAXScript}}==
Line 3,090 ⟶ 3,353:
end</syntaxhighlight>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (take 15 (iterate looksay "1")))]
 
looksay :: [char]->[char]
looksay = concat . map f . split
where f xs = show (#xs) ++ [hd xs]
 
split :: [*]->[[*]]
split = foldr f []
where f x [] = [[x]]
f x (ys:yss) = (x:ys):yss, if x = hd ys
= [x]:ys:yss, otherwise</syntaxhighlight>
{{out}}
<pre>1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211
1321132132111213122112311311222113111221131221
11131221131211131231121113112221121321132132211331222113112211
311311222113111231131112132112311321322112111312211312111322212311322113212221</pre>
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">// Look and Say Sequence
Line 4,476 ⟶ 4,769:
return ! || $ /*return the ! string plus the $ string*/</syntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version &nbsp; (the simple version).}}<br><br>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Sequence 10 1>>;
};
 
Sequence {
0 e.seq = ;
s.N e.seq = <Prout e.seq>
<Sequence <- s.N 1> <LookSay e.seq>>;
}
 
LookSay {
= ;
e.1,
<First <Group e.1> e.1>: (e.group) e.rest,
<Lenw e.group>: s.num s.item e.discard =
s.num s.item <LookSay e.rest>;
}
 
Group {
s.1 s.1 e.rest = <+ 1 <Group s.1 e.rest>>;
s.1 e.rest = 1;
};</syntaxhighlight>
{{out}}
<pre>1
1 1
2 1
1 2 1 1
1 1 1 2 2 1
3 1 2 2 1 1
1 3 1 1 2 2 2 1
1 1 1 3 2 1 3 2 1 1
3 1 1 3 1 2 1 1 1 3 1 2 2 1
1 3 2 1 1 3 1 1 1 2 3 1 1 3 1 1 2 2 1 1</pre>
 
=={{header|Ring}}==
Line 4,499 ⟶ 4,827:
return o
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪ DUP 1 DUP SUB → str char
≪ 2 '''WHILE''' str OVER DUP SUB char == '''REPEAT''' 1 + '''END'''
DUP 1 - →STR char +
str ROT OVER SIZE SUB
≫ ≫ 'CountCut' STO
"" 1 CF SWAP
'''WHILE''' 1 FC? '''REPEAT'''
CountCut '''IF''' DUP "" == '''THEN''' 1 SF '''END'''
ROT ROT + SWAP
'''END''' DROP
≫ 'LKSAY' STO
|
''( "####" -- "n#" "remainder" )''
Count occurences of 1st char
Build "n#"
Extract remaining string
''("M(n)" -- "M(n+1)" )''
Initialize M(n+1) and flag
set flag if at end of "M(n)"
build "M(n+1)"
Forget "M(n)"
|}
The following line of code delivers what is required:
≪ {} 1 "1" 12 START SWAP OVER + SWAP LKSAY NEXT DROP ≫
{{out}}
<pre>
1: { "1" "11" "21" "1211" "111221" "312211" "13112221" "1113213211" "31131211131221" "13211311123113112211" "11131221133112132113212221" "3113112221232112111312211312113211" }
</pre>
 
=={{header|Ruby}}==
Line 4,793 ⟶ 5,163:
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program looksay;
s := "1";
loop for i in [1..10] do
print(s);
s := looksay(s);
end loop;
 
proc looksay(s);
loop for c in s do;
if cur /= c then
if count /= om then
r +:= count + cur;
end if;
cur := c;
count := 1;
else
count +:= 1;
end if;
end loop;
r +:= count + cur;
return r;
end proc;
end looksay;</syntaxhighlight>
{{out}}
<pre>1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211</pre>
=={{header|Sidef}}==
{{trans|Perl}}
Line 5,453 ⟶ 5,858:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var lookAndSay = Fn.new { |s|
var res = ""
var digit = s[0]
Line 5,475 ⟶ 5,880:
}</syntaxhighlight>
 
{{out}}
<pre>
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211
1321132132111213122112311311222113111221131221
11131221131211131231121113112221121321132132211331222113112211
311311222113111231131112132112311321322112111312211312111322212311322113212221
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">char Seq0(100), Seq1(100);
int Iter, Digit, Count, I0, I1, T;
string 0;
[Seq0(0):= ^1; Seq0(1):= 0;
Text(0, Seq0); CrLf(0);
for Iter:= 2 to 15 do
[I1:= 0; I0:= 0;
repeat Digit:= Seq0(I0);
Count:= ^1;
I0:= I0+1;
while Seq0(I0) = Digit do
[Count:= Count+1;
I0:= I0+1;
];
Seq1(I1):= Count; I1:= I1+1;
Seq1(I1):= Digit; I1:= I1+1;
until Seq0(I0) = 0;
Seq1(I1):= 0;
T:= Seq0; Seq0:= Seq1; Seq1:= T;
Text(0, Seq0); CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
2,094

edits