L-system: Difference between revisions

5,875 bytes added ,  17 days ago
Added various BASIC dialects (Applesoft BASIC, BASIC256, Chipmunk Basic, GW-BASIC, MSX Basic, QBasic, QB64, XBasic and Yabasic)
m (Quackery solution moved into correct alphabetical order)
(Added various BASIC dialects (Applesoft BASIC, BASIC256, Chipmunk Basic, GW-BASIC, MSX Basic, QBasic, QB64, XBasic and Yabasic))
Line 115:
After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
<syntaxhighlight lang="vbnet">rabbit_population$ = "I"
young = 0
old = 0
 
for i = 1 to 5
new_population$ = ""
for j = 1 to length(rabbit_population$)
t$ = mid(rabbit_population$, j, 1)
begin case
case t$ = "I"
new_population$ += "M"
case t$ = "M"
new_population$ += "MI"
end case
next j
rabbit_population$ = new_population$
next i
 
for i = 1 to length(rabbit_population$)
t$ = mid(rabbit_population$, i, 1)
begin case
case t$ = "I"
young += 1
case t$ = "M"
old += 1
end case
next i
 
print "After 5 iterations there are "; old; " old rabbits and "; young; " young ones ("; rabbit_population$; ")"</syntaxhighlight>
{{out}}
<pre>After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 rabbit_population$ = "I"
120 young = 0
130 old = 0
140 for i = 1 to 5
150 new_population$ = ""
160 for j = 1 to len(rabbit_population$)
170 select case mid$(rabbit_population$,j,1)
180 case "I"
190 new_population$ = new_population$+"M"
200 case "M"
210 new_population$ = new_population$+"MI"
220 end select
230 next j
240 rabbit_population$ = new_population$
250 next i
260 for i = 1 to len(rabbit_population$)
270 select case mid$(rabbit_population$,i,1)
280 case "I"
290 young = young+1
300 case "M"
310 old = old+1
320 end select
330 next i
340 print "After 5 iterations there are ";old;"old rabbits and ";young;"young ones (";rabbit_population$;")"
350 end</syntaxhighlight>
{{out}}
<pre>After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)</pre>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">110 R$ = "I"
120 Y = 0
130 O = 0
140 FOR I = 1 TO 5
150 P$ = ""
160 FOR J = 1 TO LEN(R$)
170 IF MID$(R$,J,1) = "I" THEN P$ = P$ + "M"
180 IF MID$(R$,J,1) = "M" THEN P$ = P$ + "MI"
190 NEXT J
200 R$ = P$
210 NEXT I
220 FOR I = 1 TO LEN(R$)
230 IF MID$(R$,I,1) = "I" THEN Y = Y+1
240 IF MID$(R$,I,1) = "M" THEN O = O+1
250 NEXT I
260 PRINT "After 5 iterations there are"; O; " old rabbits and"; Y; " young ones ("; R$; ")"
270 END</syntaxhighlight>
{{out}}
<pre>After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
{{works with|QB64}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">DECLARE SUB Lindenmayer (s AS STRING, rules() AS STRING, count AS INTEGER)
DIM rules(3) AS STRING
rules(0) = "I"
rules(1) = "M"
rules(2) = "M"
rules(3) = "MI"
 
CALL Lindenmayer("I", rules(), 5)
END
 
SUB Lindenmayer (s AS STRING, rules() AS STRING, count AS INTEGER)
DIM i AS INTEGER, j AS INTEGER, k AS INTEGER, found AS INTEGER
DIM t AS STRING, nxt AS STRING, c AS STRING, rep AS STRING
 
FOR i = 0 TO count
PRINT s
nxt = ""
FOR j = 1 TO LEN(s)
c = MID$(s, j, 1)
found = 0
FOR k = LBOUND(rules) TO UBOUND(rules) STEP 2
IF c = rules(k) THEN
rep = rules(k + 1)
found = -1
EXIT FOR
END IF
NEXT k
IF found = -1 THEN t = rep ELSE t = c
nxt = nxt + t
NEXT j
s = nxt
NEXT i
END SUB</syntaxhighlight>
{{out}}
<pre>After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)</pre>
 
==={{header|QB64}}===
The [[#QBasic|QBasic]] solution works without any changes.
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "L-system"
VERSION "0.0001"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
rabbit_population$ = "I"
young = 0
old = 0
 
FOR i = 1 TO 5
new_population$ = ""
FOR j = 1 TO LEN(rabbit_population$)
SELECT CASE MID$(rabbit_population$, j, 1)
CASE "I"
new_population$ = new_population$ + "M"
CASE "M"
new_population$ = new_population$ + "MI"
END SELECT
NEXT j
rabbit_population$ = new_population$
NEXT i
 
FOR i = 1 TO LEN(rabbit_population$)
SELECT CASE MID$(rabbit_population$, i, 1)
CASE "I"
INC young
CASE "M"
INC old
END SELECT
NEXT i
 
PRINT "After 5 iterations there are "; old; " old rabbits and "; young; " young ones ("; rabbit_population$; ")"
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">rabbit_population$ = "I"
young = 0
old = 0
 
for i = 1 to 5
new_population$ = ""
for j = 1 to len(rabbit_population$)
switch mid$(rabbit_population$, j, 1)
case "I"
new_population$ = new_population$ + "M"
case "M"
new_population$ = new_population$ + "MI"
end switch
next j
rabbit_population$ = new_population$
next i
 
for i = 1 to len(rabbit_population$)
switch mid$(rabbit_population$, i, 1)
case "I"
young = young + 1
case "M"
old = old + 1
end switch
next i
 
print "After 5 iterations there are", old, " old rabbits and", young, " young ones (", rabbit_population$, ")"</syntaxhighlight>
{{out}}
<pre>After 5 iterations there are 5 old rabbits and 3 young ones (MIMMIMIM)</pre>
 
=={{header|FreeBASIC}}==
2,169

edits