Look-and-say sequence: Difference between revisions

Add ABC
(Add ABC)
 
(23 intermediate revisions by 14 users not shown)
Line 42:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F lookandsay(=number)
V result = ‘’
V repeat = number[0]
Line 61:
L 10
print(num)
num = lookandsay(num)</langsyntaxhighlight>
 
{{out}}
Line 80:
 
 
<langsyntaxhighlight lang="8080asm">bdos: equ 5 ; CP/M calls
puts: equ 9
 
Line 163:
; Due to how CP/M loads programs, the memory after here
; is free until we hit the stack.
</syntaxhighlight>
</lang>
 
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm"> bits 16
cpu 8086
puts: equ 9h ; MS/DOS system call to print a string
Line 220:
section .data
newline: db 13,10,'$' ; Newline to print in between members
memb: db '1$' ; This is where the current member is stored</langsyntaxhighlight>
 
 
=={{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>
 
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE FUNC GetLength(CHAR ARRAY s BYTE pos)
CHAR c
BYTE len
Line 288 ⟶ 325:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Look-and-say_sequence.png Screenshot from Atari 8-bit computer]
Line 307 ⟶ 344:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Text_IO, Ada.Strings.Fixed;
use Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;
 
Line 319 ⟶ 356:
end loop;
return Trim (Integer'Image (S'Length), Both) & Item;
end "+";</langsyntaxhighlight>
This function can be used as follows:
<langsyntaxhighlight lang="ada">Put_Line (+"1");
Put_Line (+(+"1"));
Put_Line (+(+(+"1")));
Line 330 ⟶ 367:
Put_Line (+(+(+(+(+(+(+(+"1"))))))));
Put_Line (+(+(+(+(+(+(+(+(+"1")))))))));
Put_Line (+(+(+(+(+(+(+(+(+(+"1"))))))))));</langsyntaxhighlight>
{{out}}
<pre>
Line 350 ⟶ 387:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<langsyntaxhighlight lang="algol68">OP + = (STRING s)STRING:
BEGIN
CHAR item = s[LWB s];
Line 376 ⟶ 413:
print ((+(+(+(+(+(+(+(+"1"))))))), new line));
print ((+(+(+(+(+(+(+(+(+"1")))))))), new line));
print ((+(+(+(+(+(+(+(+(+(+"1"))))))))), new line))</langsyntaxhighlight>
{{out}}
<pre>
Line 392 ⟶ 429:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
string(1) function digit(n);
integer n;
Line 430 ⟶ 467:
curlen := j - 1;
end;
end</langsyntaxhighlight>
{{out}}
<pre>1
Line 449 ⟶ 486:
 
=={{header|APL}}==
<langsyntaxhighlight lang="apl">
⎕IO←0
d←{(1↓⍵)-¯1↓⍵}
f←{m←(0≠d ⍵),1 ⋄ ,(d ¯1,m/⍳⍴⍵),[.5](m/⍵)}
{(f⍣⍵) ,1}¨⍳10
</syntaxhighlight>
</lang>
 
 
This is an ugly little APL2 function that accepts a numeric vector (or scalar) and returns the result.
Apologies for the labeled loop...
<langsyntaxhighlight lang="apl2">
R←LNS V;T
R←0⍴0 ⍝ initiate empty reply
Line 465 ⟶ 502:
R←R,T,↑V ⍝ append t and the 1st digit
→(0≠↑⍴V←T↓V)/LOOP ⍝ drop t digits and iterate
</syntaxhighlight>
</lang>
 
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">on lookAndSay(startNumber, howMany)
if (howMany < 1) then return {}
Line 515 ⟶ 552:
 
-- Test code:
return lookAndSay(1, 10)</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{"1", "11", "21", "1211", "111221", "312211", "13112221", "1113213211", "31131211131221", "13211311123113112211"}</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">lookAndSay: function [n][
if n=0 -> return "1"
previous: lookAndSay n-1
Line 545 ⟶ 582:
loop 0..10 'x [
print [x "->" lookAndSay x]
]</langsyntaxhighlight>
 
{{out}}
Line 562 ⟶ 599:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">AutoExecute:
Gui, -MinimizeBox
Gui, Add, Edit, w500 r20 vInput, 1
Line 598 ⟶ 635:
}
Return, r c d
}</langsyntaxhighlight>
 
=={{header|AWK}}==
 
<langsyntaxhighlight lang="awk">function lookandsay(a)
{
s = ""
Line 627 ⟶ 664:
print b
}
}</langsyntaxhighlight>
 
=={{header|BASIC}}==
{{works with|QBasic}}
<lang BASIC>10 DEFINT A-Z: I$="1"
{{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
30 PRINT I$
Line 641 ⟶ 686:
100 NEXT I
110 I$=O$
120 NEXT Z</langsyntaxhighlight>
{{out}}
<pre>1
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}}==
<syntaxhighlight lang="basic256">
<lang BASIC256>
# look and say
 
Line 686 ⟶ 800:
print a$[j]
next n
</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> number$ = "1"
FOR i% = 1 TO 10
number$ = FNlooksay(number$)
Line 708 ⟶ 822:
i% = j%
UNTIL i% > LEN(n$)
= o$</langsyntaxhighlight>
{{out}}
<pre>
Line 724 ⟶ 838:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
manifest $(
Line 774 ⟶ 888:
move(buf1,buf2)
$)
$)</langsyntaxhighlight>
{{out}}
<pre>1
Line 793 ⟶ 907:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">LookSay ← ∾´((⊑∾˜ ≠+'0'˙)¨1↓((+`»≠⊢)⊸⊔))
 
>((⌈´≠¨)↑¨⊢) LookSay⍟(↕15)"1"</langsyntaxhighlight>
{{out}}
<pre>┌─
Line 817 ⟶ 931:
=={{header|Bracmat}}==
In this example we use a non-linear pattern and a negation of a pattern: the end of e sequence of equal digits is (1) the end of the string or (2) the start of a sequence starting with a different digit.
<langsyntaxhighlight lang="bracmat">( 1:?number
& 0:?lines
& whl
Line 839 ⟶ 953:
)
)
);</langsyntaxhighlight>
{{out}}
<pre>11
Line 854 ⟶ 968:
=={{header|C}}==
This program will not stop until killed or running out of memory.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 875 ⟶ 989:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Text;
using System.Linq;
Line 917 ⟶ 1,031:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 932 ⟶ 1,046:
 
Alternate version using Regex (C#2 syntax only):
<langsyntaxhighlight lang="csharp">
using System;
using System.Text.RegularExpressions;
Line 972 ⟶ 1,086:
}
}
}</langsyntaxhighlight>
{{out}} (with args: 1 15):
<pre>1
Line 991 ⟶ 1,105:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <sstream>
#include <string>
Line 1,020 ⟶ 1,134:
std::cout << laf << '\n';
}
}</langsyntaxhighlight>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
function lookAndSay(Integer|String input) {
Line 1,050 ⟶ 1,164:
print(result);
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
No ugly int-to-string-and-back conversions.
 
<langsyntaxhighlight lang="clojure">(defn digits-seq
"Returns a seq of the digits of a number (L->R)."
[n]
Line 1,070 ⟶ 1,184:
(defn look-and-say [n]
(->> n digits-seq (partition-by identity)
(mapcat (juxt count first)) join-digits))</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="clojure">user> (take 8 (iterate look-and-say 1))
(1 11 21 1211 111221 312211 13112221 1113213211)</langsyntaxhighlight>
 
-----
 
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}}==
<langsyntaxhighlight lang="clu">look_and_say = proc (s: string) returns (string)
out: array[char] := array[char]$[]
count: int := 0
Line 1,109 ⟶ 1,250:
cur := look_and_say(cur)
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,128 ⟶ 1,269:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
Line 1,170 ⟶ 1,311:
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,188 ⟶ 1,329:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun compress (array &key (test 'eql) &aux (l (length array)))
"Compresses array by returning a list of conses each of whose car is
a number of occurrences and whose cdr is the element occurring. For
Line 1,206 ⟶ 1,347:
(parse-integer (string (cdr pair)))))
(compress (princ-to-string number))
:initial-value 0))</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight lang="lisp">(next-look-and-say 9887776666) ;=> 19283746</langsyntaxhighlight>
 
Straight character counting:
<langsyntaxhighlight lang="lisp">(defun look-and-say (s)
(let ((out (list (char s 0) 0)))
(loop for x across s do
Line 1,223 ⟶ 1,364:
(loop for s = "1" then (look-and-say s)
repeat 10
do (write-line s))</langsyntaxhighlight>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
include "strings.coh";
 
Line 1,270 ⟶ 1,411:
CopyString(&nextbuf as [uint8], &curbuf as [uint8]);
members := members - 1;
end loop;</langsyntaxhighlight>
 
{{out}}
Line 1,294 ⟶ 1,435:
{{trans|Ruby}}
The simplest one:
<langsyntaxhighlight lang="ruby">class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
Line 1,301 ⟶ 1,442:
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }</langsyntaxhighlight>
 
{{trans|Ruby from Perl}}
<langsyntaxhighlight lang="ruby">def lookandsay(str)
str.gsub(/(.)\1*/) { |s| s.size.to_s + $1 }
end
num = "1"
12.times { puts num; num = lookandsay(num) }</langsyntaxhighlight>
 
{{trans|Ruby}}
Using Enumerable#chunks
<langsyntaxhighlight lang="ruby">def lookandsay(str)
str.chars.chunks(&.itself).map{ |(c, x)| x.size.to_s + c }.join
end
num = "1"
12.times { puts num; num = lookandsay(num) }</langsyntaxhighlight>
 
{{out}}
Line 1,337 ⟶ 1,478:
=={{header|D}}==
===Short Functional Version===
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range;
 
enum say = (in string s) pure => s.group.map!q{ text(a[1],a[0]) }.join;
Line 1,343 ⟶ 1,484:
void main() {
"1".recurrence!((t, n) => t[n - 1].say).take(8).writeln;
}</langsyntaxhighlight>
{{out}}
<pre>["1", "11", "21", "1211", "111221", "312211", "13112221", "1113213211"]</pre>
 
===Beginner Imperative Version===
<langsyntaxhighlight lang="d">import std.stdio, std.conv, std.array;
 
pure string lookAndSay(string s){
Line 1,364 ⟶ 1,505:
for(auto i=0; i<10; i++)
(s = s.lookAndSay).writeln;
}</langsyntaxhighlight>
{{out}}
<pre>11
Line 1,378 ⟶ 1,519:
 
===Fast Imperative Version===
<langsyntaxhighlight lang="d">import core.stdc.stdio, std.math, std.conv, std.algorithm, std.array;
 
void showLookAndSay(bool showArrays)(in uint n) nothrow {
Line 1,471 ⟶ 1,612:
immutable n = (args.length == 2) ? args[1].to!uint : 10;
n.showLookAndSay!true;
}</langsyntaxhighlight>
{{out}}
<pre>Allocated 116 bytes.
Line 1,488 ⟶ 1,629:
 
With:
<syntaxhighlight lang ="d">70.showLookAndSay!false;</langsyntaxhighlight>
{{out}}
<pre>Allocated 158045069 bytes.
Line 1,514 ⟶ 1,655:
===Intermediate Version===
This mostly imperative version is intermediate in both speed and code size:
<langsyntaxhighlight lang="d">void main(in string[] args) {
import std.stdio, std.conv, std.algorithm, std.array, std.string;
 
Line 1,532 ⟶ 1,673:
writefln("%2d: n. digits: %d", i, seq.length);
}
}</langsyntaxhighlight>
The output is the same as the second version.
 
Line 1,549 ⟶ 1,690:
This recursive version is able to generate very large sequences in a short time without memory for the intermediate sequence (and with stack space proportional to the sequence order).
 
<langsyntaxhighlight lang="d">import core.stdc.stdio, std.conv;
 
// On Windows this uses the printf from the Microsoft C runtime,
Line 1,685 ⟶ 1,826:
evolve(39, n - base);
'\n'.putchar;
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 1,691 ⟶ 1,832:
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">\util.g
 
proc nonrec look_and_say(*char inp, outp) void:
Line 1,722 ⟶ 1,863:
CharsCopy(&buf1[0], &buf2[0])
od
corp</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,740 ⟶ 1,881:
 
=={{header|E}}==
<langsyntaxhighlight lang="e">def lookAndSayNext(number :int) {
var seen := null
var count := 0
Line 1,765 ⟶ 1,906:
println(number)
number := lookAndSayNext(number)
}</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="scheme">
(lib 'math) ;; for (number->list) = explode function
(lib 'list) ;; (group)
Line 1,782 ⟶ 1,949:
(next L)))
 
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(task 10 1)
1
Line 1,796 ⟶ 1,963:
31131211131221
13211311123113112211
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule LookAndSay do
def next(n) do
Enum.chunk_by(to_char_list(n), &(&1))
Line 1,821 ⟶ 1,988:
end
 
LookAndSay.main(System.argv)</langsyntaxhighlight>
 
{{out}}
Line 1,827 ⟶ 1,994:
 
'''Regex version:'''
<langsyntaxhighlight lang="elixir">defmodule RC do
def look_and_say(n) do
Regex.replace(~r/(.)\1*/, to_string(n), fn x,y -> [to_string(String.length(x)),y] end)
Line 1,834 ⟶ 2,001:
end
 
IO.inspect Enum.reduce(1..9, [1], fn _,acc -> [RC.look_and_say(hd(acc)) | acc] end) |> Enum.reverse</langsyntaxhighlight>
 
{{out}}
Line 1,843 ⟶ 2,010:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">-module(str).
-export([look_and_say/1, look_and_say/2]).
 
Line 1,860 ⟶ 2,027:
look_and_say(T, H, N+1, Acc);
look_and_say([H|T], Current, N, Acc) ->
look_and_say(T, H, 1, [Current, $0+N | Acc]).</langsyntaxhighlight>
 
{{out}}
Line 1,876 ⟶ 2,043:
 
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM LOOK
 
Line 1,901 ⟶ 2,068:
END FOR
END PROGRAM
</syntaxhighlight>
</lang>
<pre>
11
Line 1,917 ⟶ 2,084:
=={{header|F Sharp|F#}}==
Library functions somehow missing in F# out of the box (but present in haskell)
<langsyntaxhighlight lang="fsharp">
let rec brk p lst =
match lst with
Line 1,938 ⟶ 2,105:
 
let group lst : list<list<'a>> when 'a : equality = groupBy (=) lst
</syntaxhighlight>
</lang>
 
Implementation
<langsyntaxhighlight lang="fsharp">
let lookAndSay =
let describe (xs: char list) =
Line 1,952 ⟶ 2,119:
 
Seq.take 10 lookAndSay
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: (look-and-say) ( str -- )
unclip-slice swap [ 1 ] 2dip [
2dup = [ drop [ 1 + ] dip ] [
Line 1,964 ⟶ 2,131:
: look-and-say ( str -- str' ) [ (look-and-say) ] "" make ;
 
"1" 10 [ dup print look-and-say ] times print</langsyntaxhighlight>
 
=={{header|Fennel}}==
<langsyntaxhighlight lang="fennel">(fn look-and-say [t]
(let [lst t
ret []]
Line 1,983 ⟶ 2,150:
(for [i 1 10]
(print (table.concat lst))
(set lst (look-and-say lst)))</langsyntaxhighlight>
Alternative solution
<langsyntaxhighlight lang="fennel">(fn look-and-say [s]
(var ret [])
(var (num cnt) (values (s:sub 1 1) 1))
Line 2,002 ⟶ 2,169:
(for [i 1 10]
(print str)
(set str (look-and-say str)))</langsyntaxhighlight>
 
=={{header|FOCAL}}==
<langsyntaxhighlight lang="focal">01.10 A "HOW MANY",M
01.20 S B(0)=1;S B(1)=0
01.30 F Z=1,M;D 4;D 2
Line 2,037 ⟶ 2,204:
04.40 D 3
04.50 S X=X+1
04.60 G 4.2</langsyntaxhighlight>
{{out}}
<pre>HOW MANY:12
Line 2,054 ⟶ 2,221:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">create buf1 256 allot
create buf2 256 allot
buf1 value src
Line 2,081 ⟶ 2,248:
0 do next-look-and-say cr src count type loop ;
 
10 look-and-say</langsyntaxhighlight>
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">module LookAndSay
implicit none
 
Line 2,114 ⟶ 2,281:
end subroutine look_and_say
 
end module LookAndSay</langsyntaxhighlight>
 
<langsyntaxhighlight lang="fortran">program LookAndSayTest
use LookAndSay
implicit none
Line 2,132 ⟶ 2,299:
end do
end program LookAndSayTest</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|BASIC256}}
<langsyntaxhighlight lang="freebasic">
Dim As Integer n, j, k, k0, r
Dim As String X(2)
Line 2,162 ⟶ 2,329:
Next n
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,181 ⟶ 2,348:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
LookAndSay = "12211123"
println["Starting Value: " + LookAndSay]
Line 2,206 ⟶ 2,373:
println["$i - $LookAndSay"]
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,221 ⟶ 2,388:
10 - 11131221133112132113212221
</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn LookAndSay( testWord as CFStringRef ) as CFStringRef
NSUInteger i, length, times
CFMutableStringRef result = fn MutableStringWithCapacity(0)
unichar repeat = fn StringCharacterAtIndex( testWord, 0 )
times = 1
testWord = fn StringWithFormat( @"%@ ", fn StringSubstringFromIndex( testWord, 1 ) )
length = len(testWord)
for i = 0 to length - 1
unichar actual = fn StringCharacterAtIndex( testWord, i )
if ( actual != repeat )
MutableStringAppendFormat( result, @"%d%c", times, repeat )
times = 1
repeat = actual
else
times++
end if
next
end fn = fn StringWithString( result )
 
void local fn DoIt
NSUInteger i
CFStringRef numStr = @"1"
for i = 1 to i <= 15
NSLog( @"%@", numStr )
numStr = fn LookAndSay( numStr )
next
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211
1321132132111213122112311311222113111221131221
11131221131211131231121113112221121321132132211331222113112211
311311222113111231131112132112311321322112111312211312111322212311322113212221
</pre>
 
 
 
=={{header|Gambas}}==
Line 2,226 ⟶ 2,454:
 
'''[https://gambas-playground.proko.eu/?gist=83d63e1706fa1dc3c7468b1e9d7bcf05 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim i, j, cnt As Integer
Dim txt$, curr$, result$ As String
Line 2,250 ⟶ 2,478:
Dec i
Until i <= 0
End</langsyntaxhighlight>
Output:
<pre>
Line 2,257 ⟶ 2,485:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">LookAndSay := function(s)
local c, r, cur, ncur, v;
v := "123";
Line 2,293 ⟶ 2,521:
LookAndSay(last); # "1321132132111213122112311311222113111221131221"
LookAndSay(last); # "11131221131211131231121113112221121321132132211331222113112211"
LookAndSay(last); # "311311222113111231131112132112311321322112111312211312111322212311322113212221"</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,326 ⟶ 2,554:
fmt.Println(s)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,341 ⟶ 2,569:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def lookAndSay(sequence) {
def encoded = new StringBuilder()
(sequence.toString() =~ /(([0-9])\2*)/).each { matcher ->
Line 2,347 ⟶ 2,575:
}
encoded.toString()
}</langsyntaxhighlight>
Test Code
<langsyntaxhighlight lang="groovy">def sequence = "1"
(1..12).each {
println sequence
sequence = lookAndSay(sequence)
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,369 ⟶ 2,597:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (liftM2)
import Data.List (group)
 
Line 2,393 ⟶ 2,621:
where describe run = show (length run) ++ take 1 run
 
main = mapM_ print (iterate lookAndSay 1) -- display sequence until interrupted</langsyntaxhighlight>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">using Std;
 
class Main
Line 2,434 ⟶ 2,662:
return results;
}
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
every 1 to 10 do
write(n := nextlooknsayseq(\n | 1))
Line 2,450 ⟶ 2,678:
}
return n2
end</langsyntaxhighlight>
 
{{out}}
Line 2,463 ⟶ 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}}==
'''Solution''':
 
<langsyntaxhighlight lang="j">las=: ,@((# , {.);.1~ 1 , 2 ~:/\ ])&.(10x&#.inv)@]^:(1+i.@[)</langsyntaxhighlight>
 
'''Example''':
<langsyntaxhighlight lang="j"> 10 las 1
1 11 21 1211 111221 312211 13112221 1113213211 31131211131221 13211311123113112211 11131221133112132113212221</langsyntaxhighlight>
 
Note the result is an actual numeric sequence (cf. the textual solutions given in other languages).
Line 2,478 ⟶ 2,717:
{{trans|C#}}
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
 
Line 2,495 ⟶ 2,734:
}
return result.toString();
}</langsyntaxhighlight>
Testing:
<langsyntaxhighlight lang="java5">public static void main(String[] args){
String num = "1";
Line 2,504 ⟶ 2,743:
num = lookandsay(num);
}
}</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,520 ⟶ 2,759:
=== With RegExp ===
{{trans|Perl}}
<langsyntaxhighlight lang="javascript">function lookandsay(str) {
return str.replace(/(.)\1*/g, function(seq, p1){return seq.length.toString() + p1})
}
Line 2,528 ⟶ 2,767:
alert(num);
num = lookandsay(num);
}</langsyntaxhighlight>
 
=== Imperative version ===
<langsyntaxhighlight lang="javascript">function lookAndSay( s="" ){
var tokens=[]
var i=0, j=1
Line 2,543 ⟶ 2,782:
var phrase="1"
for(var n=0; n<10; n++ )
console.log( phrase = lookAndSay( phrase ) )</langsyntaxhighlight>
{{Output}}
<pre>11
Line 2,558 ⟶ 2,797:
=={{header|jq}}==
{{Works with|jq|1.4}}
<langsyntaxhighlight lang="jq">def look_and_say:
def head(c; n): if .[n:n+1] == c then head(c; n+1) else n end;
tostring
Line 2,572 ⟶ 2,811:
else look_and_say as $lns
| $lns, ($lns|look_and_say(n-1))
end ;</langsyntaxhighlight>
'''Example'''
1 | look_and_say(10)
Line 2,589 ⟶ 2,828:
=={{header|Julia}}==
{{Works with|Julia|1.1}}
<langsyntaxhighlight lang="julia">function lookandsay(s::String)
rst = IOBuffer()
c = 1
Line 2,613 ⟶ 2,852:
end
 
println(lookandsayseq(10))</langsyntaxhighlight>
 
{{out}}
Line 2,619 ⟶ 2,858:
 
=={{header|K}}==
<langsyntaxhighlight lang="k"> las: {x{0$,//$(#:'n),'*:'n:(&1,~=':x)_ x:0$'$x}\1}
las 8
1 11 21 1211 111221 312211 13112221 1113213211 31131211131221</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun lookAndSay(s: String): String {
Line 2,648 ⟶ 2,887:
las = lookAndSay(las)
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,671 ⟶ 2,910:
=={{header|Lasso}}==
The Look-and-say sequence is a recursive RLE, so the solution can leverage the same method as used for RLE.
<langsyntaxhighlight Lassolang="lasso">define rle(str::string)::string => {
local(orig = #str->values->asCopy,newi=array, newc=array, compiled=string)
while(#orig->size) => {
Line 2,698 ⟶ 2,937:
return #str
}
loop(15) => {^ las(1,loop_count) + '\r' ^}</langsyntaxhighlight>
{{out}}
<pre>11
Line 2,718 ⟶ 2,957:
=={{header|LiveCode}}==
This function takes a string and returns the next Look-And-Say iteration of it:
<langsyntaxhighlight Lualang="lua">function lookAndSay S
put 0 into C
put char 1 of S into lastChar
Line 2,738 ⟶ 2,977:
end repeat
put x after message
end demoLookAndSay</langsyntaxhighlight>
 
{{out}}
Line 2,754 ⟶ 2,993:
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to look.and.say.loop :in :run :c :out
if empty? :in [output (word :out :run :c)]
if equal? first :in :c [output look.and.say.loop bf :in :run+1 :c :out]
Line 2,764 ⟶ 3,003:
end
 
show cascade 10 [print ? look.and.say ?] 1</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">--returns an iterator over the first n copies of the look-and-say sequence
function lookandsayseq(n)
local t = {1}
Line 2,785 ⟶ 3,024:
end
end
for i in lookandsayseq(10) do print(i) end</langsyntaxhighlight>
 
Alternative solution, using LPeg:
<langsyntaxhighlight lang="lua">require "lpeg"
local P, C, Cf, Cc = lpeg.P, lpeg.C, lpeg.Cf, lpeg.Cc
lookandsay = Cf(Cc"" * C(P"1"^1 + P"2"^1 + P"3"^1)^1, function (a, b) return a .. #b .. string.sub(b,1,1) end)
Line 2,795 ⟶ 3,034:
print(t)
t = lookandsay:match(t)
end</langsyntaxhighlight>
 
Alternative solution, using Lua Pattern:
<langsyntaxhighlight lang="lua">function lookandsay(t)
return t:gsub("(1*)(2*)(3*)", function (...)
local ret = {}
Line 2,816 ⟶ 3,055:
print(t)
t = lookandsay(t)
end</langsyntaxhighlight>
 
<langsyntaxhighlight lang="lua">function lookandsay2(t)
return t:gsub("(1*)(2*)(3*)", function (x, y, z)
return (x == "" and x or (#x .. x:sub(1, 1))) ..
Line 2,830 ⟶ 3,069:
print(t)
t = lookandsay2(t)
end</langsyntaxhighlight>
 
{{out}}
Line 2,847 ⟶ 3,086:
Using regular expressions:
{{trans|Perl}}
<langsyntaxhighlight M4lang="m4">divert(-1)
define(`for',
`ifelse($#,0,``$0'',
Line 2,862 ⟶ 3,101:
`v
define(`v',las(v))')dnl
v</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight Maplelang="maple">generate_seq := proc(s)
local times, output, i;
times := 1;
Line 2,891 ⟶ 3,186:
 
#Test:
Look_and_Say(10);</langsyntaxhighlight>
 
{{out}}
Line 2,906 ⟶ 3,201:
"13211311123113112211"
</pre>
 
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
The function:
<langsyntaxhighlight Mathematicalang="mathematica"> LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits</langsyntaxhighlight>
 
takes as input an <em>arbitrary</em> positive integer and generates the next member of the ‘Look and Say’ sequence.
Line 2,916 ⟶ 3,210:
The first example returns the next 12 numbers of the sequence starting with 1:
 
<langsyntaxhighlight Mathematicalang="mathematica">NestList[LookAndSay, 1, 12] // Column</langsyntaxhighlight>
 
<pre style='height:15em;overflow:scroll'>1
Line 2,934 ⟶ 3,228:
The second example returns the next 12 numbers of the sequence starting with 7:
 
<langsyntaxhighlight Mathematicalang="mathematica">NestList[LookAndSay, 7, 12] // Column</langsyntaxhighlight>
 
<pre style='height:15em;overflow:scroll'>7
Line 2,951 ⟶ 3,245:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">collect(a) := block(
[n: length(a), b: [ ], x: a[1], m: 1],
for i from 2 thru n do
Line 2,970 ⟶ 3,264:
"1113213211"
"31131211131221"
"13211311123113112211" */</langsyntaxhighlight>
 
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}}==
<langsyntaxhighlight lang="maxscript">fn lookAndSay num =
(
local result = ""
Line 3,002 ⟶ 3,326:
print num
num = lookAndSay num
)</langsyntaxhighlight>
 
=={{header|Metafont}}==
<langsyntaxhighlight lang="metafont">vardef lookandsay(expr s) =
string r; r := "";
if string s:
Line 3,027 ⟶ 3,351:
endfor
 
end</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight MiniScriptlang="miniscript">// Look and Say Sequence
repeats = function(digit, string)
count = 0
Line 3,052 ⟶ 3,406:
print number
numbers = number
end for</langsyntaxhighlight>
{{out}}
<pre>
Line 3,069 ⟶ 3,423:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE LookAndSay;
FROM InOut IMPORT WriteString, WriteLn;
FROM Strings IMPORT Assign, Length;
Line 3,109 ⟶ 3,463:
Assign(buf2, buf1);
END;
END LookAndSay.</langsyntaxhighlight>
{{out}}
<pre>1
Line 3,127 ⟶ 3,481:
 
=={{header|NewLisp}}==
<syntaxhighlight lang="newlisp">
<lang NewLisp>
;;; Compute the following number in the sequence
(define (next-number s)
Line 3,149 ⟶ 3,503:
;
(go 10)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,166 ⟶ 3,520:
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">iterator lookAndSay(n: int): string =
var current = "1"
yield current
Line 3,184 ⟶ 3,538:
 
for s in lookAndSay(12):
echo s</langsyntaxhighlight>
 
{{out}}
Line 3,202 ⟶ 3,556:
=={{header|Objective-C}}==
 
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
-(NSString*)lookAndSay:(NSString *)word{
Line 3,237 ⟶ 3,591:
}
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
=== Functional ===
This function computes a see-and-say sequence from the previous one:
<langsyntaxhighlight lang="ocaml">let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)</langsyntaxhighlight>
It can be used like this:
<langsyntaxhighlight lang="ocaml">> let gen n =
let xs = Array.create n [1] in
for i=1 to n-1 do
Line 3,261 ⟶ 3,615:
[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]|]</langsyntaxhighlight>
 
=== With regular expressions in the Str library ===
<langsyntaxhighlight lang="ocaml">#load "str.cma";;
 
let lookandsay =
Line 3,277 ⟶ 3,631:
num := lookandsay !num;
print_endline !num;
done</langsyntaxhighlight>
 
=== With regular expressions in the Pcre library ===
<langsyntaxhighlight lang="ocaml">open Pcre
 
let lookandsay str =
Line 3,296 ⟶ 3,650:
num := lookandsay !num;
print_endline !num;
done</langsyntaxhighlight>
 
run this example with 'ocaml -I +pcre pcre.cma script.ml'
 
=== Imperative ===
<langsyntaxhighlight lang="ocaml">(* see http://oeis.org/A005150 *)
 
let look_and_say s =
Line 3,342 ⟶ 3,696:
*)
 
(* see http://oeis.org/A005341 *)</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">import: mapping
 
: lookAndSay ( n -- )
[ 1 ] #[ dup .cr group map( [#size, #first] ) expand ] times( n ) ;</langsyntaxhighlight>
 
{{out}} for n = 10 :
Line 3,366 ⟶ 3,720:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
%% e.g. "21" -> "1211"
fun {LookAndSayString S}
Line 3,395 ⟶ 3,749:
end
in
{ForAll {List.take {LookAndSay 1} 10} Show}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">step(n)={
my(v=eval(Vec(Str(n))),cur=v[1],ct=1,out="");
v=concat(v,99);
Line 3,412 ⟶ 3,766:
eval(out)
};
n=1;for(i=1,20,print(n);n=step(n))</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 3,418 ⟶ 3,772:
{{works with|Delphi}}
{{libheader|SysUtils}}
<langsyntaxhighlight lang="pascal">program LookAndSayDemo(input, output);
 
{$IFDEF FPC}
Line 3,460 ⟶ 3,814:
number := LookAndSay(number);
end;
end.</langsyntaxhighlight>
{{out}}
<pre>% ./LookAndSay
Line 3,487 ⟶ 3,841:
{{works with|Free_Pascal}}
{{libheader|SysUtils}}
<langsyntaxhighlight lang="pascal">
program LookAndSayDemo(input, output);
{$IFDEF FPC}
Line 3,565 ⟶ 3,919:
writeln(i:4,length(number):16,l2/l1:10:6);
end;
end.</langsyntaxhighlight>
{{out}}
<pre>1
Line 3,603 ⟶ 3,957:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
Line 3,613 ⟶ 3,967:
print "$num\n";
$num = lookandsay($num);
}</langsyntaxhighlight>
 
Using string as a cyclic buffer:
<langsyntaxhighlight lang="perl">for (local $_ = "1\n"; s/((.)\2*)//s;) {
print $1;
$_ .= ($1 ne "\n" and length($1)).$2
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">lookandsay</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
Line 3,645 ⟶ 3,999:
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,662 ⟶ 4,016:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
 
function lookAndSay($str) {
Line 3,680 ⟶ 4,034:
}
 
?></langsyntaxhighlight>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
S1 = "1",
foreach(_ in 1..11)
println(S1),
S1 := runs(S1)
end,
println(S1),
nl.
 
runs(X) = V =>
S = "",
Last = X[1],
C = 1,
foreach(I in 2..X.length)
if X[I] == Last then
C := C + 1
else
S := S ++ C.to_string() ++ [X[I-1]],
C := 1,
Last := X[I]
end
end,
V = S ++ C.to_string() ++ [Last].</syntaxhighlight>
 
{{out}}
<pre>1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de las (Lst)
(make
(while Lst
Line 3,689 ⟶ 4,082:
(while (= (setq C (pop 'Lst)) (car Lst))
(inc 'N) )
(link N C) ) ) ) )</langsyntaxhighlight>
Usage:
<langsyntaxhighlight PicoLisplang="picolisp">: (las (1))
-> (1 1)
: (las @)
Line 3,706 ⟶ 4,099:
-> (1 1 1 3 2 1 3 2 1 1)
: (las @)
-> (3 1 1 3 1 2 1 1 1 3 1 2 2 1)</langsyntaxhighlight>
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 3,761 ⟶ 4,154:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>1
Line 3,781 ⟶ 4,174:
=={{header|PowerBASIC}}==
This uses the <code>RLEncode</code> function from the [[Run-length encoding#PowerBASIC|PowerBASIC Run-length encoding entry]].
<langsyntaxhighlight lang="powerbasic">FUNCTION RLEncode (i AS STRING) AS STRING
DIM tmp1 AS STRING, tmp2 AS STRING, outP AS STRING
DIM Loop0 AS LONG, count AS LONG
Line 3,825 ⟶ 4,218:
v = VAL(INPUTBOX$("Enter a number."))
MSGBOX lookAndSay(v)
END FUNCTION</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
Line 3,848 ⟶ 4,241:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>PS> Get-MultipleLookAndSay 8
Line 3,863 ⟶ 4,256:
Works with SWI-Prolog.
 
<langsyntaxhighlight Prologlang="prolog">look_and_say(L) :-
maplist(write, L), nl,
encode(L, L1),
Line 3,907 ⟶ 4,300:
run(Var,[Other|RRest], [1,Var],[Other|RRest]):-
dif(Var,Other).
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,925 ⟶ 4,318:
 
=={{header|Pure}}==
<langsyntaxhighlight lang="pure">using system;
 
// Remove the trailing "L" from the string representation of bigints.
Line 3,938 ⟶ 4,331:
 
// This prints the entire sequence, press Ctrl-C to abort.
do (puts.str) (iterate say 1);</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()
Define i, j, cnt, txt$, curr$, result$
Print("Enter start sequence: "): txt$=Input()
Line 3,966 ⟶ 4,359:
PrintN(#CRLF$+"Press ENTER to exit."): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
 
{{out}}
Line 3,985 ⟶ 4,378:
=={{header|Python}}==
{{trans|C sharp|C#}}
<langsyntaxhighlight lang="python">def lookandsay(number):
result = ""
 
Line 4,006 ⟶ 4,399:
for i in range(10):
print num
num = lookandsay(num)</langsyntaxhighlight>
 
Functional
{{works with|Python|2.4+}}
<langsyntaxhighlight lang="python">>>> from itertools import groupby
>>> def lookandsay(number):
return ''.join( str(len(list(g))) + k
Line 4,018 ⟶ 4,411:
>>> for i in range(10):
print numberstring
numberstring = lookandsay(numberstring)</langsyntaxhighlight>
 
{{out}}
Line 4,033 ⟶ 4,426:
 
'''As a generator'''<br>
<langsyntaxhighlight lang="python">>>> from itertools import groupby, islice
>>>
>>> def lookandsay(number='1'):
Line 4,052 ⟶ 4,445:
1113213211
31131211131221
13211311123113112211</langsyntaxhighlight>
 
'''Using regular expressions'''<br>
{{trans|Perl}}
<langsyntaxhighlight lang="python">import re
 
def lookandsay(str):
Line 4,064 ⟶ 4,457:
for i in range(10):
print num
num = lookandsay(num)</langsyntaxhighlight>
 
=={{header|Q}}==
<langsyntaxhighlight lang="q">las:{{raze string[count@'x],'@'[;0]x:where[differ x]_x}\[x;1#"1"]}
las 8</langsyntaxhighlight>
{{Out}}
<pre>
Line 4,084 ⟶ 4,477:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ stack ] is instances
 
[ 1 instances put
Line 4,104 ⟶ 4,497:
[ dup echo$ cr
lookandsay ]
echo$ cr</langsyntaxhighlight>
 
{{out}}
Line 4,129 ⟶ 4,522:
Returning the value as an integer limits how long the sequence can get,
so the option for integer or character return values are provided.
<langsyntaxhighlight Rlang="r">look.and.say <- function(x, return.an.int=FALSE)
{
#convert number to character vector
Line 4,143 ⟶ 4,536:
#convert to number, if desired
if(return.an.int) as.integer(newstr) else newstr
}</langsyntaxhighlight>
Example usage:
<langsyntaxhighlight Rlang="r">x <- 1
for(i in 1:10)
{
x <- look.and.say(x)
print(x)
}</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
 
Line 4,164 ⟶ 4,557:
 
(for-each displayln (look-and-say-sequence 10))
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,186 ⟶ 4,579:
In Raku it is natural to avoid explicit loops; rather we use the sequence operator to define a lazy infinite sequence. We'll print the first 15 values here.
 
<syntaxhighlight lang="raku" perl6line>.say for ('1', *.subst(/(.)$0*/, { .chars ~ .[0] }, :g) ... *)[^15];</langsyntaxhighlight>
 
{{out}}
Line 4,212 ⟶ 4,605:
 
===simple version===
<langsyntaxhighlight lang="rexx">/*REXX program displays the sequence (and/or lengths) for the look and say series.*/
parse arg N ! . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 20 /*Not specified? Then use the default.*/
Line 4,233 ⟶ 4,626:
$= $ || _ || y /*build the "look and say" sequence. */
k= k + _ /*now, point to the next character. */
end /*k*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input values of: &nbsp; &nbsp; <tt> 20 &nbsp; 1 </tt>}}
<pre>
Line 4,347 ⟶ 4,740:
<br>it appends the sequence generated (so far) to the primary sequence, and starts with a null sequence.
<br>This avoids appending a small character string to a growing larger and larger character string.
<langsyntaxhighlight lang="rexx">/*REXX program displays the sequence (and/or lengths) for the look and say series.*/
parse arg N ! . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 20 /*Not specified? Then use the default.*/
Line 4,374 ⟶ 4,767:
chSize= chSize + 100 /*bump the chunkSize (length) counter.*/
end /*k*/
return ! || $ /*return the ! string plus the $ string*/</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight lang="ring">
number = "1"
for nr = 1 to 10
Line 4,398 ⟶ 4,826:
end
return o
</syntaxhighlight>
</lang>
 
=={{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}}==
The simplest one:
<langsyntaxhighlight lang="ruby">
class String
def look_and_say
Line 4,411 ⟶ 4,881:
ss = '1'
12.times {puts ss; ss = ss.look_and_say}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,429 ⟶ 4,899:
 
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">def lookandsay(str)
str.gsub(/(.)\1*/) {$&.length.to_s + $1}
end
Line 4,437 ⟶ 4,907:
puts num
num = lookandsay(num)
end</langsyntaxhighlight>
{{out}}
<pre>
Line 4,453 ⟶ 4,923:
 
Using Enumerable#chunk
<langsyntaxhighlight lang="ruby">def lookandsay(str)
str.chars.chunk{|c| c}.map{|c,x| [x.size, c]}.join
end
Line 4,460 ⟶ 4,930:
9.times do
puts num = lookandsay(num)
end</langsyntaxhighlight>
The '''output''' is the same above.
 
Without regular expression:
 
<langsyntaxhighlight lang="ruby"># Adding clusterization (http://apidock.com/rails/Enumerable/group_by)
module Enumerable
# clumps adjacent elements together
Line 4,481 ⟶ 4,951:
cluster
end
end</langsyntaxhighlight>
 
Using Array#cluster defined above:
 
<langsyntaxhighlight lang="ruby">def print_sequence(input_sequence, seq=10)
return unless seq > 0
puts input_sequence.join
Line 4,494 ⟶ 4,964:
end
 
print_sequence([1])</langsyntaxhighlight>
The '''output''' is the same above.
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn next_sequence(in_seq: &[i8]) -> Vec<i8> {
assert!(!in_seq.is_empty());
 
Line 4,527 ⟶ 4,997:
seq = next_sequence(&seq);
}
}</langsyntaxhighlight>
{{out}}
<pre>Sequence 0: [1]
Line 4,541 ⟶ 5,011:
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
look_and_say!: STR is
current ::= "1";
Line 4,567 ⟶ 5,037:
end;
end;
end;</langsyntaxhighlight>
{{out}}
<pre>1
Line 4,585 ⟶ 5,055:
 
===Recursive===
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
 
object LookAndSay extends App {
Line 4,611 ⟶ 5,081:
}
 
}</langsyntaxhighlight>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/V5Jn5mf/0 (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/7kn0fV3gTaqCDLIv4QGuMQ Scastie (JVM)].
===using Iterator===
{{libheader|Scala}}<langsyntaxhighlight lang="scala">def lookAndSay(seed: BigInt) = {
val s = seed.toString
( 1 until s.size).foldLeft((1, s(0), new StringBuilder)) {
Line 4,624 ⟶ 5,094:
}
 
def lookAndSayIterator(seed: BigInt) = Iterator.iterate(seed)(lookAndSay)</langsyntaxhighlight>
 
===using Stream===
<langsyntaxhighlight Scalalang="scala">object Main extends App {
 
def lookAndSay(previous: List[BigInt]): Stream[List[BigInt]] = {
Line 4,643 ⟶ 5,113:
 
(lookAndSay(1 :: Nil) take 10).foreach(s => println(s.mkString("")))
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func string: lookAndSay (in integer: level, in string: stri) is func
Line 4,673 ⟶ 5,143:
writeln(lookAndSay(level, "1"));
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,693 ⟶ 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}}
<langsyntaxhighlight lang="ruby">func lookandsay(str) {
str.gsub(/((.)\2*)/, {|a,b| a.len.to_s + b });
}
Line 4,703 ⟶ 5,208:
say num;
num = lookandsay(num);
} * 10;</langsyntaxhighlight>
 
{{out}}
Line 4,719 ⟶ 5,224:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">String extend [
lookAndSay [ |anElement nextElement counter coll newColl|
coll := (self asOrderedCollection).
Line 4,748 ⟶ 5,253:
r displayNl.
r := r lookAndSay.
]</langsyntaxhighlight>
 
{{works with|Pharo}}
<langsyntaxhighlight lang="smalltalk">String compile:
'lookAndSay |anElement nextElement counter coll newColl|
coll := (self asOrderedCollection).
Line 4,781 ⟶ 5,286:
]).
result.
</syntaxhighlight>
</lang>
 
Output:
Line 4,794 ⟶ 5,299:
This is by far the easiest solution.
 
<langsyntaxhighlight SNOBOL4lang="snobol4">* # Encode RLE
define('rle(str)c,n') :(rle_end)
rle str len(1) . c :f(return)
Line 4,809 ⟶ 5,314:
* Test and display
looksay(1,10)
end</langsyntaxhighlight>
 
{{out}}
Line 4,824 ⟶ 5,329:
 
=={{header|SQL}}==
<langsyntaxhighlight lang="sql">DROP VIEW delta;
CREATE VIEW delta AS
SELECT sequence1.v AS x,
Line 4,870 ⟶ 5,375:
DELETE FROM sequence;
INSERT INTO sequence VALUES(-1,0);
INSERT INTO sequence SELECT * FROM rle;</langsyntaxhighlight>
 
Usage:
Line 4,913 ⟶ 5,418:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<langsyntaxhighlight lang="sql pl">
SET SERVEROUTPUT ON @
 
Line 4,954 ⟶ 5,459:
END WHILE;
END @
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,976 ⟶ 5,481:
=={{header|Swift}}==
{{trans|Rust}}
<langsyntaxhighlight lang="swift">func lookAndSay(_ seq: [Int]) -> [Int] {
var result = [Int]()
var cur = seq[0]
Line 5,003 ⟶ 5,508:
print("Seq \(i): \(seq)")
seq = lookAndSay(seq)
}</langsyntaxhighlight>
 
{{out}}
Line 5,019 ⟶ 5,524:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc lookandsay n {
set new ""
while {[string length $n] > 0} {
Line 5,036 ⟶ 5,541:
puts [next_lookandsay] ;# ==> 1211
puts [next_lookandsay] ;# ==> 111221
puts [next_lookandsay] ;# ==> 312211</langsyntaxhighlight>
 
Alternatively, with coroutines:
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">proc seq_lookandsay {n {coroName next_lookandsay}} {
coroutine $coroName apply {n {
for {} {[yield $n] ne "stop"} {set n $new} {
Line 5,060 ⟶ 5,565:
puts [next_lookandsay]
puts [next_lookandsay]
puts [next_lookandsay]</langsyntaxhighlight>
 
{{out}}
Line 5,077 ⟶ 5,582:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT,{}
num=1,say=""
Line 5,090 ⟶ 5,595:
IF (look==14) EXIT
ENDLOOP
</syntaxhighlight>
</lang>
{{out}}
<pre style='height:30ex;overflow:scroll'>
Line 5,111 ⟶ 5,616:
=={{header|UNIX Shell}}==
{{works with|bash}}
<langsyntaxhighlight lang="bash">lookandsay() {
local num=$1 char seq i
for ((i=0; i<=${#num}; i++)); do
Line 5,127 ⟶ 5,632:
echo $num
num=$( lookandsay $num )
done</langsyntaxhighlight>
 
{{out}}
Line 5,144 ⟶ 5,649:
The look_and_say function returns the first n results
by iterating the function that maps a given sequence to its successor.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
 
Line 5,151 ⟶ 5,656:
#show+
 
main = look_and_say 10</langsyntaxhighlight>
{{out}}
<pre>1
Line 5,165 ⟶ 5,670:
 
=={{header|VBA}}==
<syntaxhighlight lang="vba">
<lang VBA>
Public Sub LookAndSay(Optional Niter As Integer = 10)
'generate "Niter" members of the look-and-say sequence
Line 5,215 ⟶ 5,720:
End If
End Sub
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,233 ⟶ 5,738:
=={{header|VBScript}}==
=====Implementation=====
<langsyntaxhighlight lang="vb">function looksay( n )
dim i
dim accum
Line 5,252 ⟶ 5,757:
loop
looksay = res
end function</langsyntaxhighlight>
 
=====Invocation=====
<langsyntaxhighlight lang="vb">dim m
m = 1
for i = 0 to 13
m = looksay(m)
wscript.echo m
next</langsyntaxhighlight>
 
{{out}}
Line 5,286 ⟶ 5,791:
10 sequences are created in this example.
 
<langsyntaxhighlight lang="vedit">Repeat(10) {
BOL
Reg_Empty(20)
Line 5,295 ⟶ 5,800:
}
Ins_Newline Reg_Ins(20)
}</langsyntaxhighlight>
 
{{out}}
Line 5,309 ⟶ 5,814:
13211311123113112211
11131221133112132113212221
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn lss(s string) string {
mut r := ''
mut c := s[0..1]
mut nc := 1
for i := 1; i < s.len; i++ {
d := s[i..i+1]
if d == c {
nc++
continue
}
r += nc.str() + c
c = d
nc = 1
}
return r + nc.str() + c
}
fn main() {
mut s := "1"
println(s)
for i := 0; i < 8; i++ {
s = lss(s)
println(s)
}
}</syntaxhighlight>
 
{{out}}
<pre>
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">var lookAndSay = Fn.new { |s|
var res = ""
var digit = s[0]
Line 5,332 ⟶ 5,878:
System.print(las)
las = lookAndSay.call(las)
}</langsyntaxhighlight>
 
{{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>
Line 5,354 ⟶ 5,942:
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">
<lang Yabasic>
dim X$(2)
i = 0 // índice de cadena de entrada
Line 5,379 ⟶ 5,967:
next n
print
</syntaxhighlight>
</lang>
{{out}}La salida es similar a la de [[#FreeBASIC|FreeBASIC]], mostrada arriba.
 
=={{header|Yorick}}==
 
<langsyntaxhighlight lang="yorick">func looksay(input) {
// Special case: one digit
if(strlen(input) == 1)
Line 5,413 ⟶ 6,001:
write, val;
val = looksay(val);
} while(strlen(val) < 80);</langsyntaxhighlight>
 
{{out}}
Line 5,435 ⟶ 6,023:
Treating the task as a string manipulation problem.
{{trans|Scala}}
<langsyntaxhighlight lang="zkl">fcn lookAndSay(seed){ // numeric String --> numeric String
len,c:=[1..seed.len()-1].reduce(fcn([(len,c)]lc,index,s,sb){
if(c!=s[index]) { sb.write(len); sb.write(c); lc.clear(1,s[index]) }
Line 5,442 ⟶ 6,030:
sb.write(len); sb.write(c);
sb.close();
}</langsyntaxhighlight>
 
{{out}}
2,094

edits