This commit is contained in:
Ingy döt Net 2013-04-10 16:19:29 -07:00
parent e5e8880e41
commit 518da4a923
1019 changed files with 15877 additions and 0 deletions

View file

@ -0,0 +1,34 @@
# 100 doors problem
dim d(100)
# simple solution
print "simple solution"
gosub initialize
for t = 1 to 100
for j = t to 100 step t
d[j-1] = not d[j-1]
next j
next t
gosub showopen
# more optimized solution
print "more optimized solution"
gosub initialize
for t = 1 to 10
d[t^2-1] = true
next t
gosub showopen
end
initialize:
for t = 1 to d[?]
d[t-1] = false # closed
next t
return
showopen:
for t = 1 to d[?]
print d[t-1]+ " ";
if t%10 = 0 then print
next t
return

View file

@ -0,0 +1,11 @@
DIM doors%(100)
FOR pass% = 1 TO 100
FOR door% = pass% TO 100 STEP pass%
doors%(door%) = NOT doors%(door%)
NEXT door%
NEXT pass%
FOR door% = 1 TO 100
IF doors%(door%) PRINT "Door " ; door% " is open"
NEXT door%

View file

@ -0,0 +1,10 @@
@echo off
setlocal enableDelayedExpansion
:: 0 = closed
:: 1 = open
:: SET /A treats undefined variable as 0
:: Negation operator ! must be escaped because delayed expansion is enabled
for /l %%p in (1 1 100) do for /l %%d in (%%p %%p 100) do set /a "door%%d=^!door%%d"
for /l %%d in (1 1 100) do if !door%%d!==1 (
echo door %%d is open
) else echo door %%d is closed

View file

@ -0,0 +1,9 @@
@echo off
setlocal enableDelayedExpansion
set /a square=1, incr=3
for /l %%d in (1 1 100) do (
if %%d neq !square! (echo door %%d is closed) else (
echo door %%d is open
set /a square+=incr, incr+=2
)
)

View file

@ -0,0 +1,10 @@
Graphics 640,480
i=1
While ((i*i)<=100)
a$=i*i
DrawText a$,10,20*i
Print i*i
i=i+1
Wend
Flip
WaitKey

View file

@ -0,0 +1,26 @@
( 100doors-tbl
= door step
. tbl$(doors.101) { Create an array. Indexing is 0-based. Add one extra for addressing element nr. 100 }
& 0:?step
& whl
' ( 1+!step:~>100:?step { ~> means 'not greater than', i.e. 'less than or equal' }
& 0:?door
& whl
' ( !step+!door:~>100:?door
& 1+-1*!(!door$doors):?doors { <number>$<variable> sets the current index, which stays the same until explicitly changed. }
)
)
& 0:?door
& whl
' ( 1+!door:~>100:?door
& out
$ ( door
!door
is
( !(!door$doors):1&open
| closed
)
)
)
& tbl$(doors.0) { clean up the array }
)

View file

@ -0,0 +1,30 @@
( 100doors-var
= step door
. 0:?door
& whl
' ( 1+!door:~>100:?door
& closed:?!door { this creates a variable and assigns a value 'closed' to it }
)
& 0:?step
& whl
' ( 1+!step:~>100:?step
& 0:?door
& whl
' ( !step+!door:~>100:?door
& ( !!door:closed&open
| closed
)
: ?!door
)
)
& 0:?door
& whl
' ( 1+!door:~>100:?door
& out$(door !door is !!door)
)
& 0:?door
& whl
' ( 1+!door:~>100:?door
& tbl$(!door.0) { cleanup the variable }
)
)

View file

@ -0,0 +1,25 @@
( 100doors-list
= doors door doorIndex step
. :?doors
& 0:?door
& whl
' ( 1+!door:~>100:?door
& closed !doors:?doors
)
& 0:?skip
& whl
' ( :?ndoors
& whl
' ( !doors:?skipped [!skip %?door ?doors { the [<number> pattern only succeeds when the scanning cursor is at position <number> }
& !ndoors
!skipped
( !door:open&closed
| open
)
: ?ndoors
)
& !ndoors !doors:?doors
& 1+!skip:<100:?skip
)
& out$!doors
)

View file

@ -0,0 +1,22 @@
( 100doors-obj
= doors door doorIndex step
. :?doors
& 0:?door
& whl
' ( 1+!door:~>100:?door
& new$(=closed) !doors:?doors
)
& 0:?skip
& whl
' ( !doors:?tododoors
& whl
' ( !tododoors:? [!skip %?door ?tododoors
& ( !(door.):open&closed
| open
)
: ?(door.)
)
& 1+!skip:<100:?skip
)
& out$!doors
)

View file

@ -0,0 +1,4 @@
100doors-tbl$
& 100doors-var$
& 100doors-list$
& 100doors-obj$;

View file

@ -0,0 +1,2 @@
blsq ) 10ro2?^
{1 4 9 16 25 36 49 64 81 100}

View file

@ -0,0 +1,66 @@
REM Choose four random digits (1-9) with repetitions allowed:
DIM digits%(4), check%(4)
FOR choice% = 1 TO 4
digits%(choice%) = RND(9)
NEXT choice%
REM Prompt the player:
PRINT "Enter an equation (using all of, and only, the single digits ";
FOR index% = 1 TO 4
PRINT ; digits%(index%) ;
IF index%<>4 PRINT " " ;
NEXT
PRINT ")"
PRINT "which evaluates to exactly 24. Only multiplication (*), division (/),"
PRINT "addition (+) & subtraction (-) operations and parentheses are allowed:"
INPUT "24 = " equation$
REPEAT
REM Check that the correct digits are used:
check%() = 0
FOR char% = 1 TO LEN(equation$)
digit% = INSTR("0123456789", MID$(equation$, char%, 1)) - 1
IF digit% >= 0 THEN
FOR index% = 1 TO 4
IF digit% = digits%(index%) THEN
IF NOT check%(index%) check%(index%) = TRUE : EXIT FOR
ENDIF
NEXT index%
IF index% > 4 THEN
PRINT "Sorry, you used the illegal digit "; digit%
EXIT REPEAT
ENDIF
ENDIF
NEXT char%
FOR index% = 1 TO 4
IF NOT check%(index%) THEN
PRINT "Sorry, you failed to use the digit " ; digits%(index%)
EXIT REPEAT
ENDIF
NEXT index%
REM Check that no pairs of digits are used:
FOR pair% = 11 TO 99
IF INSTR(equation$, STR$(pair%)) THEN
PRINT "Sorry, you may not use a pair of digits "; pair%
EXIT REPEAT
ENDIF
NEXT pair%
REM Check whether the equation evaluates to 24:
ON ERROR LOCAL PRINT "Sorry, there was an error in the equation" : EXIT REPEAT
result = EVAL(equation$)
RESTORE ERROR
IF result = 24 THEN
PRINT "Congratulations, you succeeded in the task!"
ELSE
PRINT "Sorry, your equation evaluated to " ; result " rather than 24!"
ENDIF
UNTIL TRUE
INPUT '"Play again", answer$
IF LEFT$(answer$,1) = "y" OR LEFT$(answer$,1) = "Y" THEN CLS : RUN
QUIT

View file

@ -0,0 +1,78 @@
( 24-game
= m-w m-z 4numbers answer expr numbers
, seed get-random convertBinaryMinusToUnary
, convertDivisionToMultiplication isExpresssion reciprocal
. (seed=.!arg:(~0:~/#?m-w.~0:~/#?m-z))
& seed$!arg
& ( get-random
=
. 36969*mod$(!m-z.65536)+div$(!m-z.65536):?m-z
& 18000*mod$(!m-w.65536)+div$(!m-w.65536):?m-w
& mod$(!m-z*65536+!m-w.9)+1
)
& ( convertBinaryMinusToUnary
= a z
. @(!arg:%?a "-" ?z)
& str$(!a "+-1*" convertBinaryMinusToUnary$!z)
| !arg
)
& (reciprocal=.!arg^-1)
& ( convertDivisionToMultiplication
= a z
. @(!arg:?a "/" ?z)
& str$(!a "*reciprocal$" convertDivisionToMultiplication$!z)
| !arg
)
& ( isExpresssion
= A Z expr
. @( !arg
: ?A
("+"|"-"|"*"|"/")
( ?Z
& isExpresssion$!A
& isExpresssion$!Z
)
)
| !numbers:?A !arg ?Z
& !A !Z:?numbers
| ( @(!arg:"(" ?expr ")")
| @(!arg:(" "|\t) ?expr)
| @(!arg:?expr (" "|\t))
)
& isExpresssion$!expr
)
& out
$ "Enter an expression that evaluates to 24 by combining the following numbers."
& out$"You may only use the operators + - * /"
& out$"Parentheses and spaces are allowed."
& whl
' ( get-random$() get-random$() get-random$() get-random$
: ?4numbers
& out$!4numbers
& whl
' ( get'(,STR):?expr:~
& !4numbers:?numbers
& ~(isExpresssion$!expr&!numbers:)
& out
$ ( str
$ ( "["
!expr
"] is not a valid expression. Try another expression."
)
)
)
& !expr:~
& convertBinaryMinusToUnary$!expr:?expr
& convertDivisionToMultiplication$!expr:?expr
& get$(!expr,MEM):?answer
& out$(str$(!expr " = " !answer))
& !answer
: ( 24&out$Right!
| #&out$Wrong!
)
& out$"Try another one:"
)
& out$bye
)
& 24-game$(13.14)
& ;

View file

@ -0,0 +1,33 @@
#length of querter and eight note in ms
n4 = 1000 * 60 / 80 / 4
n8 = n4 / 2
#frequency of musical notes in hz
e = 330
ef = 311
b = 247
bf = 233
f = 349
c = 262
d = 294
ds = 311
a = 220
dim notes(1)
dim lengs(1)
# redim is automatic when using a {} list to assign an array
notes = {ef, ef, ef, bf, bf, bf, ef, ef, ef, ef, f , f , f , c , c , c , f , d , d , d , d , d , d , d , bf, bf, bf, c , c , ef, ef, ef, ef, ef}
lengs = {n8, n8, n8, n8, n8, n8, n8, n8, n8, n4, n8, n8, n8, n8, n8, n8, n4, n4, n8, n8, n8, n8, n8, n4, n8, n8, n8, n8, n8, n8, n8, n8, n8, n4 }
for x = 99 to 1 step -1
for t = 0 to notes[?]-1
if t = 0 then print x + " bottles of beer on the wall"
if t = 11 then print x + " bottles of beer"
if t = 18 then print "Take one down, pass it around"
if t = 25 then print(x-1) + " bottles of beer on the wall"
sound notes[t], lengs[t]
pause .002
next t
print
next x

View file

@ -0,0 +1,32 @@
N_Bottles = 99
beer$ = " of beer"
wall$ = " on the wall"
unit$ = "99 bottles"
WHILE N_Bottles >= 0
IF N_Bottles=0 THEN
PRINT '"No more bottles" beer$ wall$ ", " unit$ beer$ "."
PRINT "Go to the store and buy some more, ";
ELSE
PRINT 'unit$ beer$ wall$ ", " unit$ beer$ "."
PRINT "Take one down and pass it around, ";
ENDIF
N_Bottles -= 1
CASE N_Bottles OF
WHEN 0:
unit$ = "no more bottles"
WHEN 1:
unit$ = "1 bottle"
OTHERWISE:
unit$ = STR$((N_Bottles + 100) MOD 100) + " bottles"
ENDCASE
PRINT unit$ beer$ wall$ "."
ENDWHILE
END

View file

@ -0,0 +1,47 @@
@echo off
setlocal
:main
for /L %%i in (99,-1,1) do (
call :verse %%i
)
echo no bottles of beer on the wall
echo no bottles of beer
echo go to the store and buy some more
echo 99 bottles of beer on the wall
echo.
set /p q="Keep drinking? "
if %q% == y goto main
if %q% == Y goto main
goto :eof
:verse
call :plural %1 res
echo %res% of beer on the wall
echo %res% of beer
call :oneit %1 res
echo take %res% down and pass it round
set /a c=%1-1
call :plural %c% res
echo %res% of beer on the wall
echo.
goto :eof
:plural
if %1 gtr 1 goto :gtr
if %1 equ 1 goto :equ
set %2=no bottles
goto :eof
:gtr
set %2=%1 bottles
goto :eof
:equ
set %2=1 bottle
goto :eof
:oneit
if %1 equ 1 (
set %2=it
) else (
set %2=one
)
goto :eof

View file

@ -0,0 +1,59 @@
{BottlesOfBeer.bra
See http://99-bottles-of-beer.net/}
X=
new
= n upper nbottles lyrics
. 99:?n
& ( upper
= .@(!arg:%@?a ?z)&str$(upp$!a !z)
)
& ( nbottles
=
. str
$ ( ( !arg:>0
& !arg
" bottle"
(!arg:1&|s)
| "no more bottles"
)
" of beer"
)
)
& ( lyrics
= (upper$(nbottles$!n:?x) " on the wall, " !x ".\n")
( !n+-1:?n:~<0
& "Take one down and pass it around, "
nbottles$!n
" on the wall.
"
!lyrics
| "Go to the store and buy some more, "
nbottles$99
" on the wall.
"
)
)
& put$(str$!lyrics);
r=
get'"BottlesOfBeer.bra"
& rmv$(str$(BottlesOfBeer ".bak"))
& ren$("BottlesOfBeer.bra".str$(BottlesOfBeer ".bak"))
& put
$ ( "{BottlesOfBeer.bra
See http://99-bottles-of-beer.net/}
"
, "BottlesOfBeer.bra"
, NEW
)
& lst'(X,"BottlesOfBeer.bra",APP)
& put'(\n,"BottlesOfBeer.bra",APP)
& lst'(r,"BottlesOfBeer.bra",APP)
& put$(str$("\nnew'" X ";\n"),"BottlesOfBeer.bra",APP);
new'X;

View file

@ -0,0 +1,44 @@
>+++++++++[<+++++++++++>-]<[>[-]>[-]<<[>+>+<<-]>>[<<+>>-]>>>
[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<
-]<<-<-]+++++++++>[<->-]>>+>[<[-]<<+>>>-]>[-]+<<[>+>-<<-]<<<
[>>+>+<<<-]>>>[<<<+>>>-]>[<+>-]<<-[>[-]<[-]]>>+<[>[-]<-]<+++
+++++[<++++++<++++++>>-]>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-
]<<<<<<.>>[-]>[-]++++[<++++++++>-]<.>++++[<++++++++>-]<++.>+
++++[<+++++++++>-]<.><+++++..--------.-------.>>[>>+>+<<<-]>
>>[<<<+>>>-]<[<<<<++++++++++++++.>>>>-]<<<<[-]>++++[<+++++++
+>-]<.>+++++++++[<+++++++++>-]<--.---------.>+++++++[<------
---->-]<.>++++++[<+++++++++++>-]<.+++..+++++++++++++.>++++++
++[<---------->-]<--.>+++++++++[<+++++++++>-]<--.-.>++++++++
[<---------->-]<++.>++++++++[<++++++++++>-]<++++.-----------
-.---.>+++++++[<---------->-]<+.>++++++++[<+++++++++++>-]<-.
>++[<----------->-]<.+++++++++++..>+++++++++[<---------->-]<
-----.---.>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>>+++
+[<++++++>-]<--.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.
><+++++..--------.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++
++++++++++++.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<++
+++++++>-]<--.---------.>+++++++[<---------->-]<.>++++++[<++
+++++++++>-]<.+++..+++++++++++++.>++++++++++[<---------->-]<
-.---.>+++++++[<++++++++++>-]<++++.+++++++++++++.++++++++++.
------.>+++++++[<---------->-]<+.>++++++++[<++++++++++>-]<-.
-.---------.>+++++++[<---------->-]<+.>+++++++[<++++++++++>-
]<--.+++++++++++.++++++++.---------.>++++++++[<---------->-]
<++.>+++++[<+++++++++++++>-]<.+++++++++++++.----------.>++++
+++[<---------->-]<++.>++++++++[<++++++++++>-]<.>+++[<----->
-]<.>+++[<++++++>-]<..>+++++++++[<--------->-]<--.>+++++++[<
++++++++++>-]<+++.+++++++++++.>++++++++[<----------->-]<++++
.>+++++[<+++++++++++++>-]<.>+++[<++++++>-]<-.---.++++++.----
---.----------.>++++++++[<----------->-]<+.---.[-]<<<->[-]>[
-]<<[>+>+<<-]>>[<<+>>-]>>>[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]
>[<+>-]>[<<++++++++++>>>+<-]<<-<-]+++++++++>[<->-]>>+>[<[-]<
<+>>>-]>[-]+<<[>+>-<<-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<>>[<+>-]<
<-[>[-]<[-]]>>+<[>[-]<-]<++++++++[<++++++<++++++>>-]>>>[>+>+
<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>[-]>[-]++++[<++++++++>
-]<.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.><+++++..---
-----.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++++++++++++++
.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<+++++++++>-]<-
-.---------.>+++++++[<---------->-]<.>++++++[<+++++++++++>-]
<.+++..+++++++++++++.>++++++++[<---------->-]<--.>+++++++++[
<+++++++++>-]<--.-.>++++++++[<---------->-]<++.>++++++++[<++
++++++++>-]<++++.------------.---.>+++++++[<---------->-]<+.
>++++++++[<+++++++++++>-]<-.>++[<----------->-]<.+++++++++++
..>+++++++++[<---------->-]<-----.---.+++.---.[-]<<<]

View file

@ -0,0 +1,7 @@
99.to 2 { n |
p "#{n} bottles of beer on the wall, #{n} bottles of beer!"
p "Take one down, pass it around, #{n - 1} bottle#{true? n > 2 's' ''} of beer on the wall."
}
p "One bottle of beer on the wall, one bottle of beer!"
p "Take one down, pass it around, no more bottles of beer on the wall."

View file

@ -0,0 +1,4 @@
dim a(2)
input "Enter two numbers seperated by a space?", t$
a = explode(t$," ")
print t$ + " " + (a[0] + a[1])

View file

@ -0,0 +1,14 @@
REPEAT
hereY% = VPOS
INPUT LINE "" q$
hereX% = LEN(q$) + 1
WHILE LEFT$(q$, 1) = " "
q$ = MID$(q$, 2)
ENDWHILE
space% = INSTR(q$, " ")
IF space% THEN
a = VAL(LEFT$(q$, space% - 1))
b = VAL(MID$(q$, space% + 1))
PRINT TAB(hereX%, hereY%) ; a + b
ENDIF
UNTIL FALSE

View file

@ -0,0 +1,5 @@
REPEAT
INPUT LINE "" q$
space% = INSTR(q$," ")
PRINT VAL LEFT$(q$,space%-1) + VAL MID$(q$,space%+1)
UNTIL FALSE

View file

@ -0,0 +1,8 @@
::aplusb.cmd
@echo off
setlocal
set /p a="A: "
set /p b="B: "
set /a c=a+b
echo %c%
endlocal

View file

@ -0,0 +1,8 @@
::aplusb.cmd
@echo off
setlocal
set a=%1
set b=%2
set /a c=a+b
echo %c%
endlocal

View file

@ -0,0 +1,6 @@
::aplusb.cmd
@echo off
setlocal
set /a c=%~1
echo %c%
endlocal

View file

@ -0,0 +1,13 @@
::aplusb.cmd
@echo off
setlocal
set /p a="Input stream: "
call :add %a%
echo %res%
endlocal
goto :eof
:add
set /a res=res+%1
shift
if "%1" neq "" goto :add

View file

@ -0,0 +1,8 @@
( out
$ ( put$"Enter two integer numbers between -1000 and 1000:"
& (filter=~/#%:~<-1000:~>1000)
& get':(!filter:?a) (!filter:?b)
& !a+!b
| "Invalid input. Try again"
)
);

View file

@ -0,0 +1,6 @@
, Read first number
>,, Eat separator and read second number
[<+>-] Add ASCII values
,++ Use newline to get a 12
[<---->-] Subtract 48 to get back to ASCII
<. and print

2
Task/A+B/Brat/a+b.brat Normal file
View file

@ -0,0 +1,2 @@
numbers = g.split[0,1].map(:to_i)
p numbers[0] + numbers[1] #Prints the sum of the input

View file

@ -0,0 +1 @@
ps++

View file

@ -0,0 +1,19 @@
INSTALL @lib$+"CLASSLIB"
REM Declare a class with no implementation:
DIM abstract{method}
PROC_class(abstract{})
REM Inherit from the abstract class:
DIM derived{member%}
PROC_inherit(derived{}, abstract{})
PROC_class(derived{})
REM Provide an implementation for the derived class:
DEF derived.method : PRINT "Hello world!" : ENDPROC
REM Instantiate the derived class:
PROC_new(instance{}, derived{})
REM Test by calling the method:
PROC(instance.method)

View file

@ -0,0 +1,17 @@
x = FNaccumulator(1)
dummy = FN(x)(5)
dummy = FNaccumulator(3)
PRINT FN(x)(2.3)
END
DEF FNaccumulator(sum)
LOCAL I%, P%, Q%
DIM P% 53 : Q% = !^FNdummy()
FOR I% = 0 TO 49 : P%?I% = Q%?I% : NEXT
P%!I% = P% : sum = FN(P%+I%)(sum)
= P%+I%
DEF FNdummy(n)
PRIVATE sum
sum += n
= sum

View file

@ -0,0 +1,8 @@
accumulator = { sum |
{ n | sum = sum + n }
}
x = accumulator 1
x 5
accumulator 3 #Does not affect x
p x 2.3 #Prints 8.3 (1 + 5 + 2.3)

View file

@ -0,0 +1,33 @@
dim stack(5000, 3) # BASIC-256 lacks functions (as of ver. 0.9.6.66)
stack[0,0] = 3 # M
stack[0,1] = 7 # N
lev = 0
gosub ackermann
print "A("+stack[0,0]+","+stack[0,1]+") = "+stack[0,2]
end
ackermann:
if stack[lev,0]=0 then
stack[lev,2] = stack[lev,1]+1
return
end if
if stack[lev,1]=0 then
lev = lev+1
stack[lev,0] = stack[lev-1,0]-1
stack[lev,1] = 1
gosub ackermann
stack[lev-1,2] = stack[lev,2]
lev = lev-1
return
end if
lev = lev+1
stack[lev,0] = stack[lev-1,0]
stack[lev,1] = stack[lev-1,1]-1
gosub ackermann
stack[lev,0] = stack[lev-1,0]-1
stack[lev,1] = stack[lev,2]
gosub ackermann
stack[lev-1,2] = stack[lev,2]
lev = lev-1
return

View file

@ -0,0 +1,19 @@
# BASIC256 since 0.9.9.1 supports functions
for m = 0 to 3
for n = 0 to 4
print m + " " + n + " " + ackermann(m,n)
next n
next m
end
function ackermann(m,n)
if m = 0 then
ackermann = n+1
else
if n = 0 then
ackermann = ackermann(m-1,1)
else
ackermann = ackermann(m-1,ackermann(m,n-1))
endif
end if
end function

View file

@ -0,0 +1,7 @@
PRINT FNackermann(3, 7)
END
DEF FNackermann(M%, N%)
IF M% = 0 THEN = N% + 1
IF N% = 0 THEN = FNackermann(M% - 1, 1)
= FNackermann(M% - 1, FNackermann(M%, N%-1))

View file

@ -0,0 +1,11 @@
GET "libhdr"
LET ack(m, n) = m=0 -> n+1,
n=0 -> ack(m-1, 1),
ack(m-1, ack(m, n-1))
LET start() = VALOF
{ FOR i = 0 TO 6 FOR m = 0 TO 3 DO
writef("ack(%n, %n) = %n*n", m, n, ack(m,n))
RESULTIS 0
}

View file

@ -0,0 +1,31 @@
::Ackermann.cmd
@echo off
set depth=0
:ack
if %1==0 goto m0
if %2==0 goto n0
:else
set /a n=%2-1
set /a depth+=1
call :ack %1 %n%
set t=%errorlevel%
set /a depth-=1
set /a m=%1-1
set /a depth+=1
call :ack %m% %t%
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )
:m0
set/a n=%2+1
if %depth%==0 ( exit %n% ) else ( exit /b %n% )
:n0
set /a m=%1-1
set /a depth+=1
call :ack %m% 1
set t=%errorlevel%
set /a depth-=1
if %depth%==0 ( exit %t% ) else ( exit /b %t% )

View file

@ -0,0 +1,4 @@
::Ack.cmd
@echo off
cmd/c ackermann.cmd %1 %2
echo Ackermann(%1, %2)=%errorlevel%

View file

@ -0,0 +1,8 @@
( Ack
= m n
. !arg:(?m,?n)
& ( !m:0&!n+1
| !n:0&Ack$(!m+-1,1)
| Ack$(!m+-1,Ack$(!m,!n+-1))
)
);

View file

@ -0,0 +1,61 @@
( A
= m n value key eq chain
, find insert future stack va val
. ( chain
= key future skey
. !arg:(?key.?future)
& str$!key:?skey
& (cache..insert)$(!skey..!future)
&
)
& (find=.(cache..find)$(str$!arg))
& ( insert
= key value future v futureeq futurem skey
. !arg:(?key.?value)
& str$!key:?skey
& ( (cache..find)$!skey:(?key.?v.?future)
& (cache..remove)$!skey
& (cache..insert)$(!skey.!value.)
& ( !future:(?futurem.?futureeq)
& (!futurem,!value.!futureeq)
|
)
| (cache..insert)$(!skey.!value.)&
)
)
& !arg:(?m,?n)
& !n+1:?value
& :?eq:?stack
& whl
' ( (!m,!n):?key
& ( find$!key:(?.#%?value.?future)
& insert$(!eq.!value) !future
| !m:0
& !n+1:?value
& ( !eq:&insert$(!key.!value)
| insert$(!key.!value) !stack:?stack
& insert$(!eq.!value)
)
| !n:0
& (!m+-1,1.!key)
(!eq:|(!key.!eq))
| find$(!m,!n+-1):(?.?val.?)
& ( !val:#%
& ( find$(!m+-1,!val):(?.?va.?)
& !va:#%
& insert$(!key.!va)
| (!m+-1,!val.!eq)
(!m,!n.!eq)
)
|
)
| chain$(!m,!n+-1.!m+-1.!key)
& (!m,!n+-1.)
(!eq:|(!key.!eq))
)
!stack
: (?m,?n.?eq) ?stack
)
& !value
)
& new$hash:?cache

View file

@ -0,0 +1,11 @@
( AckFormula
= m n
. !arg:(?m,?n)
& ( !m:0&!n+1
| !m:1&!n+2
| !m:2&2*!n+3
| !m:3&2^(!n+3)+-3
| !n:0&AckFormula$(!m+-1,1)
| AckFormula$(!m+-1,AckFormula$(!m,!n+-1))
)
)

View file

@ -0,0 +1,7 @@
ackermann = { m, n |
when { m == 0 } { n + 1 }
{ m > 0 && n == 0 } { ackermann(m - 1, 1) }
{ m > 0 && n > 0 } { ackermann(m - 1, ackermann(m, n - 1)) }
}
p ackermann 3, 4 #Prints 125

View file

@ -0,0 +1,23 @@
INSTALL @lib$+"CLASSLIB"
INSTALL @lib$+"TIMERLIB"
INSTALL @lib$+"NOWAIT"
REM Integrator class:
DIM integ{f$, t#, v#, tid%, @init, @@exit, input, output, tick}
PROC_class(integ{})
REM Methods:
DEF integ.@init integ.f$ = "0" : integ.tid% = FN_ontimer(10, PROC(integ.tick), 1) : ENDPROC
DEF integ.@@exit PROC_killtimer(integ.tid%) : ENDPROC
DEF integ.input (f$) integ.f$ = f$ : ENDPROC
DEF integ.output = integ.v#
DEF integ.tick integ.t# += 0.01 : integ.v# += EVAL(integ.f$) : ENDPROC
REM Test:
PROC_new(myinteg{}, integ{})
PROC(myinteg.input) ("SIN(2*PI*0.5*myinteg.t#)")
PROCwait(200)
PROC(myinteg.input) ("0")
PROCwait(50)
PRINT "Final value = " FN(myinteg.output)
PROC_discard(myinteg{})

View file

@ -0,0 +1,31 @@
INSTALL @lib$+"CLASSLIB"
REM Create a base class with no members:
DIM class{method}
PROC_class(class{})
REM Instantiate the class:
PROC_new(myobject{}, class{})
REM Add a member at run-time:
member$ = "mymember#"
PROCaddmember(myobject{}, member$, 8)
REM Test that the member can be accessed:
PROCassign("myobject." + member$, "PI")
PRINT EVAL("myobject." + member$)
END
DEF PROCaddmember(RETURN obj{}, mem$, size%)
LOCAL D%, F%, P%
DIM D% DIM(obj{}) + size% - 1, F% LEN(mem$) + 8
P% = !^obj{} + 4
WHILE !P% : P% = !P% : ENDWHILE : !P% = F%
$$(F%+4) = mem$ : F%!(LEN(mem$) + 5) = DIM(obj{})
!(^obj{} + 4) = D%
ENDPROC
DEF PROCassign(v$, n$)
IF EVAL("FNassign(" + v$ + "," + n$ + ")")
ENDPROC
DEF FNassign(RETURN n, v) : n = v : = 0

View file

@ -0,0 +1,26 @@
( ( struktuur
= (aMember=) (aMethod=.!(its.aMember))
)
& new$struktuur:?object
& out$"Object as originally created:"
& lst$object
& A value:?(object..aMember)
& !object:(=?originalMembersAndMethods)
& new
$ (
' ( (anotherMember=)
(anotherMethod=.!(its.anotherMember))
()$originalMembersAndMethods
)
)
: ?object
& out
$ "
Object with additional member and method and with 'aMember' already set to some interesting value:"
& lst$object
& some other value:?(object..anotherMember)
& out$"
Call both methods and output their return values."
& out$("aMember contains:" (object..aMethod)$)
& out$("anotherMember contains:" (object..anotherMethod)$)
&);

View file

@ -0,0 +1,15 @@
Object as originally created:
(object=
=(aMember=) (aMethod=.!(its.aMember)));
Object with additional member and method and with 'aMember' already set to some interesting value:
(object=
= (anotherMember=)
(anotherMethod=.!(its.anotherMember))
(aMember=A value)
(aMethod=.!(its.aMember)));
Call both methods and output their return values.
aMember contains: A value
anotherMember contains: some other value

View file

@ -0,0 +1,5 @@
REM get a variable's address:
y% = ^x%
REM can't set a variable's address, but can access a given memory location (4 bytes):
x% = !y%

View file

@ -0,0 +1,56 @@
DATA 6
DATA "Given$a$text$file$of$many$lines,$where$fields$within$a$line$"
DATA "are$delineated$by$a$single$'dollar'$character,$write$a$program"
DATA "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$"
DATA "column$are$separated$by$at$least$one$space."
DATA "Further,$allow$for$each$word$in$a$column$to$be$either$left$"
DATA "justified,$right$justified,$or$center$justified$within$its$column."
REM First find the maximum length of a 'word':
max% = 0
READ nlines%
FOR Line% = 1 TO nlines%
READ text$
REPEAT
word$ = FNword(text$, "$")
IF LEN(word$) > max% THEN max% = LEN(word$)
UNTIL word$ = ""
NEXT Line%
@% = max% : REM set column width
REM Now display the aligned text:
RESTORE
READ nlines%
FOR Line% = 1 TO nlines%
READ text$
REPEAT
word$ = FNword(text$, "$")
PRINT FNjustify(word$, max%, "left"),;
UNTIL word$ = ""
PRINT
NEXT Line%
END
DEF FNword(text$, delim$)
PRIVATE delim%
LOCAL previous%
IF delim% = 0 THEN
previous% = 1
ELSE
previous% = delim% + LEN(delim$)
ENDIF
delim% = INSTR(text$+delim$, delim$, previous%)
IF delim% = 0 THEN
= ""
ELSE
= MID$(text$, previous%, delim%-previous%) + " "
ENDIF
DEF FNjustify(word$, field%, mode$)
IF word$ = "" THEN = ""
CASE mode$ OF
WHEN "center": = STRING$((field%-LEN(word$)) DIV 2, " ") + word$
WHEN "right": = STRING$(field%-LEN(word$), " ") + word$
ENDCASE
= word$

View file

@ -0,0 +1,53 @@
INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0,0)
DIM dict$(26000), sort$(26000), indx%(26000)
REM Load the dictionary:
dict% = OPENIN("C:\unixdict.txt")
IF dict%=0 ERROR 100, "No dictionary file"
index% = 0
REPEAT
index% += 1
dict$(index%) = GET$#dict%
indx%(index%) = index%
UNTIL EOF#dict%
CLOSE #dict%
Total% = index%
TIME = 0
REM Sort the letters in each word:
FOR index% = 1 TO Total%
sort$(index%) = FNsortstring(dict$(index%))
NEXT
REM Sort the sorted words:
C% = Total%
CALL Sort%, sort$(1), indx%(1)
REM Find anagrams and deranged anagrams:
maxlen% = 0
maxidx% = 0
FOR index% = 1 TO Total%-1
IF sort$(index%) = sort$(index%+1) THEN
One$ = dict$(indx%(index%))
Two$ = dict$(indx%(index%+1))
FOR c% = 1 TO LEN(One$)
IF MID$(One$,c%,1) = MID$(Two$,c%,1) EXIT FOR
NEXT
IF c%>LEN(One$) IF c%>maxlen% maxlen% = c% : maxidx% = index%
ENDIF
NEXT
PRINT "The longest deranged anagrams are '" dict$(indx%(maxidx%));
PRINT "' and '" dict$(indx%(maxidx%+1)) "'"
PRINT "(taking " ; TIME/100 " seconds)"
END
DEF FNsortstring(A$)
LOCAL C%, a&()
C% = LEN(A$)
DIM a&(C%)
$$^a&(0) = A$
CALL Sort%, a&(0)
= $$^a&(0)

View file

@ -0,0 +1,63 @@
get$("unixdict.txt",STR):?wordList
& 1:?product
& :?unsorted
& whl
' ( @(!wordList:(%?word:?letterString) \n ?wordList)
& :?letterSum
& whl
' ( @(!letterString:%?letter ?letterString)
& (!letter:~#|str$(N !letter))+!letterSum
: ?letterSum
)
& !letterSum^!word !unsorted:?unsorted
)
& ( mergeSort
= newL L first second
. !arg:?L
& whl
' ( !L:% %
& :?newL
& whl
' ( !L:%?first %?second ?L
& !first*!second !newL:?newL
)
& !L !newL:?L
)
& !L
)
& mergeSort$!unsorted:?product
& 0:?maxLength:?oldMaxLength
& :?derangedAnagrams
& ( deranged
= nextLetter Atail Btail
. !arg
: ( (.)
| ( @(?:%@?nextLetter ?Atail)
. @(?:(%@:~!nextLetter) ?Btail)
)
& deranged$(!Atail.!Btail)
)
)
& ( !product
: ?
* ?
^ ( %+%
: @(%:? ([~<!maxLength:[?maxLength))+?
: ?
+ %@?anagramA
+ ?
+ %@?anagramB
+ ( ?
& deranged$(!anagramA.!anagramB)
& (!anagramA.!anagramB)
( !maxLength:>!oldMaxLength:?oldMaxLength
&
| !derangedAnagrams
)
: ?derangedAnagrams
& ~
)
)
* ?
| out$!derangedAnagrams
);

View file

@ -0,0 +1,64 @@
INSTALL @lib$+"SORTLIB"
sort% = FN_sortinit(0,0)
REM Count number of words in dictionary:
nwords% = 0
dict% = OPENIN("unixdict.txt")
WHILE NOT EOF#dict%
word$ = GET$#dict%
nwords% += 1
ENDWHILE
CLOSE #dict%
REM Create arrays big enough to contain the dictionary:
DIM dict$(nwords%), sort$(nwords%)
REM Load the dictionary and sort the characters in the words:
dict% = OPENIN("unixdict.txt")
FOR word% = 1 TO nwords%
word$ = GET$#dict%
dict$(word%) = word$
sort$(word%) = FNsortchars(word$)
NEXT word%
CLOSE #dict%
REM Sort arrays using the 'sorted character' words as a key:
C% = nwords%
CALL sort%, sort$(1), dict$(1)
REM Count the longest sets of anagrams:
max% = 0
set% = 1
FOR word% = 1 TO nwords%-1
IF sort$(word%) = sort$(word%+1) THEN
set% += 1
ELSE
IF set% > max% THEN max% = set%
set% = 1
ENDIF
NEXT word%
REM Output the results:
set% = 1
FOR word% = 1 TO nwords%-1
IF sort$(word%) = sort$(word%+1) THEN
set% += 1
ELSE
IF set% = max% THEN
FOR anagram% = word%-max%+1 TO word%
PRINT dict$(anagram%),;
NEXT
PRINT
ENDIF
set% = 1
ENDIF
NEXT word%
END
DEF FNsortchars(word$)
LOCAL C%, char&()
DIM char&(LEN(word$))
$$^char&(0) = word$
C% = LEN(word$)
CALL sort%, char&(0)
= $$^char&(0)

View file

@ -0,0 +1,28 @@
( get$("unixdict.txt",STR):?list
& 1:?product
& whl
' ( @(!list:(%?word:?w) \n ?list)
& :?sum
& whl
' ( @(!w:%?let ?w)
& (!let:~#|str$(N !let))+!sum:?sum
)
& !sum^!word*!product:?product
)
& lst$(product,"product.txt",NEW)
& 0:?max
& :?group
& ( !product
: ?
* ?^(%+%:?exp)
* ( ?
& !exp
: ?
+ ( [>!max:[?max&!exp:?group
| [~<!max&!group !exp:?group
)
& ~
)
| out$!group
)
);

View file

@ -0,0 +1,29 @@
MODE 8
*FLOAT 64
VDU 23,23,4;0;0;0; : REM Set line thickness
theta = RAD(40) : REM initial displacement
g = 9.81 : REM acceleration due to gravity
l = 0.50 : REM length of pendulum in metres
REPEAT
PROCpendulum(theta, l)
WAIT 1
PROCpendulum(theta, l)
accel = - g * SIN(theta) / l / 100
speed += accel / 100
theta += speed
UNTIL FALSE
END
DEF PROCpendulum(a, l)
LOCAL pivotX, pivotY, bobX, bobY
pivotX = 640
pivotY = 800
bobX = pivotX + l * 1000 * SIN(a)
bobY = pivotY - l * 1000 * COS(a)
GCOL 3,6
LINE pivotX, pivotY, bobX, bobY
GCOL 3,11
CIRCLE FILL bobX + 24 * SIN(a), bobY - 24 * COS(a), 24
ENDPROC

View file

@ -0,0 +1,15 @@
VDU 23,22,212;40;16,32,16,128
txt$ = "Hello World! "
direction% = TRUE
ON MOUSE direction% = NOT direction% : RETURN
OFF
REPEAT
CLS
PRINT txt$;
IF direction% THEN
txt$ = RIGHT$(txt$) + LEFT$(txt$)
ELSE
txt$ = MID$(txt$,2) + LEFT$(txt$,1)
ENDIF
WAIT 20
UNTIL FALSE

View file

@ -0,0 +1,6 @@
PRINT FNfib(10)
END
DEF FNfib(n%) IF n%<0 THEN ERROR 100, "Must not be negative"
LOCAL P% : P% = !384 + LEN$!384 + 4 : REM Function pointer
(n%) IF n%<2 THEN = n% ELSE = FN(^P%)(n%-1) + FN(^P%)(n%-2)

View file

@ -0,0 +1,18 @@
( (
=
. !arg:#:~<0
& ( (=.!arg$!arg)
$ (
=
.
' (
. !arg:<2
| (($arg)$($arg))$(!arg+-2)
+ (($arg)$($arg))$(!arg+-1)
)
)
)
$ !arg
)
$ 30
)

View file

@ -0,0 +1,21 @@
( /(
' ( x
. $x:#:~<0
& ( /('(f.($f)$($f)))
$ /(
' ( r
. /(
' ( n
. $n:<2
| (($r)$($r))$($n+-2)
+ (($r)$($r))$($n+-1)
)
)
)
)
)
$ ($x)
)
)
$ 30
)

View file

@ -0,0 +1,16 @@
DIM a(4)
a() = 1, 2, 3, 4, 5
PROCmap(a(), FNsqrt())
FOR i = 0 TO 4
PRINT a(i)
NEXT
END
DEF FNsqrt(n) = SQR(n)
DEF PROCmap(array(), RETURN func%)
LOCAL I%
FOR I% = 0 TO DIM(array(),1)
array(I%) = FN(^func%)(array(I%))
NEXT
ENDPROC

View file

@ -0,0 +1,4 @@
#Print out each element in array
[:a :b :c :d :e].each { element |
p element
}

View file

@ -0,0 +1 @@
[:a :b :c :d :e].each ->p

View file

@ -0,0 +1,4 @@
{?} @(5^4^3^2:?first [20 ? [-21 ?last [?length)&str$(!first "..." !last "\nlength " !length)
{!} 62060698786608744707...92256259918212890625
length 183231
S 2,46 sec

View file

@ -0,0 +1,40 @@
DIM Complex{r, i}
DIM a{} = Complex{} : a.r = 1.0 : a.i = 1.0
DIM b{} = Complex{} : b.r = PI# : b.i = 1.2
DIM o{} = Complex{}
PROCcomplexadd(o{}, a{}, b{})
PRINT "Result of addition is " FNcomplexshow(o{})
PROCcomplexmul(o{}, a{}, b{})
PRINT "Result of multiplication is " ; FNcomplexshow(o{})
PROCcomplexneg(o{}, a{})
PRINT "Result of negation is " ; FNcomplexshow(o{})
PROCcomplexinv(o{}, a{})
PRINT "Result of inversion is " ; FNcomplexshow(o{})
END
DEF PROCcomplexadd(dst{}, one{}, two{})
dst.r = one.r + two.r
dst.i = one.i + two.i
ENDPROC
DEF PROCcomplexmul(dst{}, one{}, two{})
dst.r = one.r*two.r - one.i*two.i
dst.i = one.i*two.r + one.r*two.i
ENDPROC
DEF PROCcomplexneg(dst{}, src{})
dst.r = -src.r
dst.i = -src.i
ENDPROC
DEF PROCcomplexinv(dst{}, src{})
LOCAL denom : denom = src.r^2 + src.i^ 2
dst.r = src.r / denom
dst.i = -src.i / denom
ENDPROC
DEF FNcomplexshow(src{})
IF src.i >= 0 THEN = STR$(src.r) + " + " +STR$(src.i) + "i"
= STR$(src.r) + " - " + STR$(-src.i) + "i"

View file

@ -0,0 +1,9 @@
input "enter a number ?", a
input "enter another number ?", b
print "addition " + a + " + " + b + " = " + (a + b)
print "subtraction " + a + " - " + b + " = " + (a - b)
print "multiplication " + a + " * " + b + " = " + (a * b)
print "integer division " + a + " \ " + b + " = " + (a \ b)
print "remainder or modulo " + a + " % " + b + " = " + (a % b)
print "power " + a + " ^ " + b + " = " + (a ^ b)

View file

@ -0,0 +1,9 @@
INPUT "Enter the first integer: " first%
INPUT "Enter the second integer: " second%
PRINT "The sum is " ; first% + second%
PRINT "The difference is " ; first% - second%
PRINT "The product is " ; first% * second%
PRINT "The integer quotient is " ; first% DIV second% " (rounds towards 0)"
PRINT "The remainder is " ; first% MOD second% " (sign matches first operand)"
PRINT "The first raised to the power of the second is " ; first% ^ second%

View file

@ -0,0 +1,13 @@
@echo off
set /P A=Enter 1st Number :
set /P B=Enter 2nd Number :
set D=%A% + %B% & call :printC
set D=%A% - %B% & call :printC
set D=%A% * %B% & call :printC
set D=%A% / %B% & call :printC & rem truncates toward 0
set D=%A% %% %B% & call :printC & rem matches sign of 1st operand
exit /b
:printC
set /A C=%D%
echo %D% = %C%

View file

@ -0,0 +1,7 @@
x = ask("First number: ").to_i
y = ask("Second number: ").to_i
#Division uses floating point
#Remainder uses sign of right hand side
[:+ :- :* :/ :% :^].each { op |
p "#{x} #{op} #{y} = #{x.call_method op, y}"

View file

@ -0,0 +1,62 @@
*FLOAT64
DIM frac{num, den}
DIM Sum{} = frac{}, Kf{} = frac{}, One{} = frac{}
One.num = 1 : One.den = 1
FOR n% = 2 TO 2^19-1
Sum.num = 1 : Sum.den = n%
FOR k% = 2 TO SQR(n%)
IF (n% MOD k%) = 0 THEN
Kf.num = 1 : Kf.den = k%
PROCadd(Sum{}, Kf{})
PROCnormalise(Sum{})
Kf.den = n% DIV k%
PROCadd(Sum{}, Kf{})
PROCnormalise(Sum{})
ENDIF
NEXT
IF FNeq(Sum{}, One{}) PRINT n% " is perfect"
NEXT n%
END
DEF PROCabs(a{}) : a.num = ABS(a.num) : ENDPROC
DEF PROCneg(a{}) : a.num = -a.num : ENDPROC
DEF PROCadd(a{}, b{})
LOCAL t : t = a.den * b.den
a.num = a.num * b.den + b.num * a.den
a.den = t
ENDPROC
DEF PROCsub(a{}, b{})
LOCAL t : t = a.den * b.den
a.num = a.num * b.den - b.num * a.den
a.den = t
ENDPROC
DEF PROCmul(a{}, b{})
a.num *= b.num : a.den *= b.den
ENDPROC
DEF PROCdiv(a{}, b{})
a.num *= b.den : a.den *= b.num
ENDPROC
DEF FNeq(a{}, b{}) = a.num * b.den = b.num * a.den
DEF FNlt(a{}, b{}) = a.num * b.den < b.num * a.den
DEF FNgt(a{}, b{}) = a.num * b.den > b.num * a.den
DEF FNne(a{}, b{}) = a.num * b.den <> b.num * a.den
DEF FNle(a{}, b{}) = a.num * b.den <= b.num * a.den
DEF FNge(a{}, b{}) = a.num * b.den >= b.num * a.den
DEF PROCnormalise(a{})
LOCAL a, b, t
a = a.num : b = a.den
WHILE b <> 0
t = a
a = b
b = t - b * INT(t / b)
ENDWHILE
a.num /= a : a.den /= a
IF a.den < 0 a.num *= -1 : a.den *= -1
ENDPROC

View file

@ -0,0 +1,58 @@
Expr$ = "1 + 2 * (3 + (4 * 5 + 6 * 7 * 8) - 9) / 10"
PRINT "Input = " Expr$
AST$ = FNast(Expr$)
PRINT "AST = " AST$
PRINT "Value = " ;EVAL(AST$)
END
DEF FNast(RETURN in$)
LOCAL ast$, oper$
REPEAT
ast$ += FNast1(in$)
WHILE ASC(in$)=32 in$ = MID$(in$,2) : ENDWHILE
oper$ = LEFT$(in$,1)
IF oper$="+" OR oper$="-" THEN
ast$ += oper$
in$ = MID$(in$,2)
ELSE
EXIT REPEAT
ENDIF
UNTIL FALSE
= "(" + ast$ + ")"
DEF FNast1(RETURN in$)
LOCAL ast$, oper$
REPEAT
ast$ += FNast2(in$)
WHILE ASC(in$)=32 in$ = MID$(in$,2) : ENDWHILE
oper$ = LEFT$(in$,1)
IF oper$="*" OR oper$="/" THEN
ast$ += oper$
in$ = MID$(in$,2)
ELSE
EXIT REPEAT
ENDIF
UNTIL FALSE
= "(" + ast$ + ")"
DEF FNast2(RETURN in$)
LOCAL ast$
WHILE ASC(in$)=32 in$ = MID$(in$,2) : ENDWHILE
IF ASC(in$)<>40 THEN = FNnumber(in$)
in$ = MID$(in$,2)
ast$ = FNast(in$)
in$ = MID$(in$,2)
= ast$
DEF FNnumber(RETURN in$)
LOCAL ch$, num$
REPEAT
ch$ = LEFT$(in$,1)
IF INSTR("0123456789.", ch$) THEN
num$ += ch$
in$ = MID$(in$,2)
ELSE
EXIT REPEAT
ENDIF
UNTIL FALSE
= num$

View file

@ -0,0 +1,13 @@
*FLOAT 64
@% = &1010
PRINT FNagm(1, 1/SQR(2))
END
DEF FNagm(a,g)
LOCAL ta
REPEAT
ta = a
a = (a+g)/2
g = SQR(ta*g)
UNTIL a = ta
= a

View file

@ -0,0 +1,19 @@
DIM a(3), b(4)
a() = 1, 2, 3, 4
b() = 5, 6, 7, 8, 9
PROCconcat(a(), b(), c())
FOR i% = 0 TO DIM(c(),1)
PRINT c(i%)
NEXT
END
DEF PROCconcat(a(), b(), RETURN c())
LOCAL s%, na%, nb%
s% = ^a(1) - ^a(0) : REM Size of each array element
na% = DIM(a(),1)+1 : REM Number of elements in a()
nb% = DIM(b(),1)+1 : REM Number of elements in b()
DIM c(na%+nb%-1)
SYS "RtlMoveMemory", ^c(0), ^a(0), s%*na%
SYS "RtlMoveMemory", ^c(na%), ^b(0), s%*nb%
ENDPROC

View file

@ -0,0 +1,2 @@
blsq ) {1 2 3}{4 5 6}_+
{1 2 3 4 5 6}

View file

@ -0,0 +1,29 @@
# numeric array
dim numbers(10)
for t = 0 to 9
numbers[t] = t + 1
next t
# string array
dim words$(10)
# assigning an array with a list
words$ = {"one","two","three","four","five","six","seven","eight","nine","ten"}
gosub display
# resize arrays (always preserves values if larger)
redim numbers(11)
redim words$(11)
numbers[10] = 11
words$[10] = "eleven"
gosub display
end
display:
# display arrays
# using ? to get size of array
for t = 0 to numbers[?]-1
print numbers[t] + "=" + words$[t]
next t
return

View file

@ -0,0 +1,17 @@
REM Declare arrays, dimension is maximum index:
DIM array(6), array%(6), array$(6)
REM Entire arrays may be assigned in one statement:
array() = 0.1, 1.2, 2.3, 3.4, 4.5, 5.6, 6.7
array%() = 0, 1, 2, 3, 4, 5, 6
array$() = "Zero", "One", "Two", "Three", "Four", "Five", "Six"
REM Or individual elements may be assigned:
array(2) = PI
array%(3) = RND
array$(4) = "Hello world!"
REM Print out sample array elements:
PRINT array(2) TAB(16) array(3) TAB(32) array(4)
PRINT array%(2) TAB(16) array%(3) TAB(32) array%(4)
PRINT array$(2) TAB(16) array$(3) TAB(32) array$(4)

View file

@ -0,0 +1,24 @@
::arrays.cmd
@echo off
setlocal ENABLEDELAYEDEXPANSION
set array.1=1
set array.2=2
set array.3=3
set array.4=4
for /L %%i in (1,1,4) do call :showit array.%%i !array.%%i!
set c=-27
call :mkarray marry 5 6 7 8
for /L %%i in (-27,1,-24) do call :showit "marry^&%%i" !marry^&%%i!
endlocal
goto :eof
:mkarray
set %1^&%c%=%2
set /a c += 1
shift /2
if "%2" neq "" goto :mkarray
goto :eof
:showit
echo %1 = %2
goto :eof

View file

@ -0,0 +1,6 @@
PROCassert(a% = 42)
END
DEF PROCassert(bool%)
IF NOT bool% THEN ERROR 100, "Assertion failed"
ENDPROC

View file

@ -0,0 +1,4 @@
squish import :assert :assertions
assert_equal 42 42
assert_equal 13 42 #Raises an exception

View file

@ -0,0 +1,106 @@
global values$, keys$
dim values$[1]
dim keys$[1]
call updateKey("a","apple")
call updateKey("b","banana")
call updateKey("c","cucumber")
gosub show
print "I like to eat a " + getValue$("c") + " on my salad."
call deleteKey("b")
call updateKey("c","carrot")
call updateKey("e","endive")
gosub show
end
show:
for t = 0 to countKeys()-1
print getKeyByIndex$(t) + " " + getValueByIndex$(t)
next t
print
return
subroutine updateKey(key$, value$)
# update or add an item
i=findKey(key$)
if i=-1 then
i = freeKey()
keys$[i] = key$
end if
values$[i] = value$
end subroutine
subroutine deleteKey(key$)
# delete by clearing the key
i=findKey(key$)
if i<>-1 then
keys$[i] = ""
end if
end subroutine
function freeKey()
# find index of a free element in the array
for n = 0 to keys$[?]-1
if keys$[n]="" then return n
next n
redim keys$[n+1]
redim values$[n+1]
return n
end function
function findKey(key$)
# return index or -1 if not found
for n = 0 to keys$[?]-1
if key$=keys$[n] then return n
next n
return -1
end function
function getValue$(key$)
# return a value by the key or "" if not existing
i=findKey(key$)
if i=-1 then
return ""
end if
return values$[i]
end function
function countKeys()
# return number of items
# remember to skip the empty keys (deleted ones)
k = 0
for n = 0 to keys$[?] -1
if keys$[n]<>"" then k++
next n
return k
end function
function getValueByIndex$(i)
# get a value by the index
# remember to skip the empty keys (deleted ones)
k = 0
for n = 0 to keys$[?] -1
if keys$[n]<>"" then
if k=i then return values$[k]
k++
endif
next n
return ""
end function
function getKeyByIndex$(i)
# get a key by the index
# remember to skip the empty keys (deleted ones)
k = 0
for n = 0 to keys$[?] -1
if keys$[n]<>"" then
if k=i then return keys$[k]
k++
endif
next n
return ""
end function

View file

@ -0,0 +1,21 @@
REM Store some values with their keys:
PROCputdict(mydict$, "FF0000", "red")
PROCputdict(mydict$, "00FF00", "green")
PROCputdict(mydict$, "0000FF", "blue")
REM Retrieve some values using their keys:
PRINT FNgetdict(mydict$, "green")
PRINT FNgetdict(mydict$, "red")
END
DEF PROCputdict(RETURN dict$, value$, key$)
IF dict$ = "" dict$ = CHR$(0)
dict$ += key$ + CHR$(1) + value$ + CHR$(0)
ENDPROC
DEF FNgetdict(dict$, key$)
LOCAL I%, J%
I% = INSTR(dict$, CHR$(0) + key$ + CHR$(1))
IF I% = 0 THEN = "" ELSE I% += LEN(key$) + 2
J% = INSTR(dict$, CHR$(0), I%)
= MID$(dict$, I%, J% - I%)

View file

@ -0,0 +1,24 @@
::assocarrays.cmd
@echo off
setlocal ENABLEDELAYEDEXPANSION
set array.dog=1
set array.cat=2
set array.wolf=3
set array.cow=4
for %%i in (dog cat wolf cow) do call :showit array.%%i !array.%%i!
set c=-27
call :mkarray sicko flu 5 measles 6 mumps 7 bromodrosis 8
for %%i in (flu measles mumps bromodrosis) do call :showit "sicko^&%%i" !sicko^&%%i!
endlocal
goto :eof
:mkarray
set %1^&%2=%3
shift /2
shift /2
if "%2" neq "" goto :mkarray
goto :eof
:showit
echo %1 = %2
goto :eof

View file

@ -0,0 +1,8 @@
h = [:] #Empty hash
h[:a] = 1 #Assign value
h[:b] = [1 2 3] #Assign another value
h2 = [a: 1, b: [1 2 3], 10 : "ten"] #Initialized hash
h2[:b][2] #Returns 3

View file

@ -0,0 +1,26 @@
REM Store some values with their keys:
PROCputdict(mydict$, "FF0000", "red")
PROCputdict(mydict$, "00FF00", "green")
PROCputdict(mydict$, "0000FF", "blue")
REM Iterate through the dictionary:
i% = 1
REPEAT
i% = FNdict(mydict$, i%, v$, k$)
PRINT v$, k$
UNTIL i% = 0
END
DEF PROCputdict(RETURN dict$, value$, key$)
IF dict$ = "" dict$ = CHR$(0)
dict$ += key$ + CHR$(1) + value$ + CHR$(0)
ENDPROC
DEF FNdict(dict$, I%, RETURN value$, RETURN key$)
LOCAL J%, K%
J% = INSTR(dict$, CHR$(1), I%)
K% = INSTR(dict$, CHR$(0), J%)
value$ = MID$(dict$, I%+1, J%-I%-1)
key$ = MID$(dict$, J%+1, K%-J%-1)
IF K% >= LEN(dict$) THEN K% = 0
= K%

View file

@ -0,0 +1,16 @@
h = [ hello: 1 world: 2 :! : 3]
#Iterate over key, value pairs
h.each { k, v |
p "Key: #{k} Value: #{v}"
}
#Iterate over keys
h.each_key { k |
p "Key: #{k}"
}
#Iterate over values
h.each_value { v |
p "Value: #{v}"
}

View file

@ -0,0 +1,52 @@
INSTALL @lib$+"TIMERLIB"
DIM Buckets%(100)
FOR i% = 1 TO 100 : Buckets%(i%) = RND(10) : NEXT
tid0% = FN_ontimer(10, PROCdisplay, 1)
tid1% = FN_ontimer(11, PROCflatten, 1)
tid2% = FN_ontimer(12, PROCroughen, 1)
ON ERROR PROCcleanup : REPORT : PRINT : END
ON CLOSE PROCcleanup : QUIT
REPEAT
WAIT 0
UNTIL FALSE
END
DEF PROCdisplay
PRINT SUM(Buckets%()) " ", MOD(Buckets%())
ENDPROC
DEF PROCflatten
LOCAL d%, i%, j%
REPEAT
i% = RND(100)
j% = RND(100)
UNTIL i%<>j%
d% = Buckets%(i%) - Buckets%(j%)
PROCatomicupdate(Buckets%(i%), Buckets%(j%), d% DIV 4)
ENDPROC
DEF PROCroughen
LOCAL i%, j%
REPEAT
i% = RND(100)
j% = RND(100)
UNTIL i%<>j%
PROCatomicupdate(Buckets%(i%), Buckets%(j%), RND(10))
ENDPROC
DEF PROCatomicupdate(RETURN src%, RETURN dst%, amt%)
IF amt% > src% amt% = src%
IF amt% < -dst% amt% = -dst%
src% -= amt%
dst% += amt%
ENDPROC
DEF PROCcleanup
PROC_killtimer(tid0%)
PROC_killtimer(tid1%)
PROC_killtimer(tid2%)
ENDPROC

View file

@ -0,0 +1,33 @@
@% = &2040A
MAX_N = 20
TIMES = 1000000
FOR n = 1 TO MAX_N
avg = FNtest(n, TIMES)
theory = FNanalytical(n)
diff = (avg / theory - 1) * 100
PRINT STR$(n), avg, theory, diff "%"
NEXT
END
DEF FNanalytical(n)
LOCAL i, s
FOR i = 1 TO n
s += FNfactorial(n) / n^i / FNfactorial(n-i)
NEXT
= s
DEF FNtest(n, times)
LOCAL i, b, c, x
FOR i = 1 TO times
x = 1 : b = 0
WHILE (b AND x) = 0
c += 1
b OR= x
x = 1 << (RND(n) - 1)
ENDWHILE
NEXT
= c / times
DEF FNfactorial(n)
IF n=1 OR n=0 THEN =1 ELSE = n * FNfactorial(n-1)

View file

@ -0,0 +1,28 @@
REM specific functions for the array/vector types
REM Byte Array
DEF FN_Mean_Arithmetic&(n&())
= SUM(n&()) / (DIM(n&(),1)+1)
REM Integer Array
DEF FN_Mean_Arithmetic%(n%())
= SUM(n%()) / (DIM(n%(),1)+1)
REM Float 40 array
DEF FN_Mean_Arithmetic(n())
= SUM(n()) / (DIM(n(),1)+1)
REM A String array
DEF FN_Mean_Arithmetic$(n$())
LOCAL I%, S%, sum, Q%
S% = DIM(n$(),1)
FOR I% = 0 TO S%
Q% = TRUE
ON ERROR LOCAL Q% = FALSE
IF Q% sum += EVAL(n$(I%))
NEXT
= sum / (S%+1)
REM Float 64 array
DEF FN_Mean_Arithmetic#(n#())
= SUM(n#()) / (DIM(n#(),1)+1)

View file

@ -0,0 +1,24 @@
(mean1=
sum length n
. 0:?sum:?length
& whl
' ( !arg:%?n ?arg
& 1+!length:?length
& !n+!sum:?sum
)
& !sum*!length^-1
);
(mean2=
sum length n
. 0:?sum:?length
& !arg
: ?
( #%@?n
& 1+!length:?length
& !n+!sum:?sum
& ~
)
?
| !sum*!length^-1
);

View file

@ -0,0 +1,6 @@
( :?test
& 1000000:?Length
& whl'(!Length+-1:?Length:>0&!Length !test:?test)
& out$mean1$!test
& out$mean2$!test
)

View file

@ -0,0 +1 @@
>,[-<+>>+<],<[->-<]>[-->+<]>.

View file

@ -0,0 +1,5 @@
mean = { list |
true? list.empty?, 0, { list.reduce(0, :+) / list.length }
}
p mean 1.to 10 #Prints 5.5

View file

@ -0,0 +1,4 @@
blsq ) {1 2 2.718 3 3.142}av
2.372
blsq ) {}av
NaN

View file

@ -0,0 +1,20 @@
*FLOAT 64
DIM angles(3)
angles() = 350,10
PRINT FNmeanangle(angles(), 2)
angles() = 90,180,270,360
PRINT FNmeanangle(angles(), 4)
angles() = 10,20,30
PRINT FNmeanangle(angles(), 3)
END
DEF FNmeanangle(angles(), N%)
LOCAL I%, sumsin, sumcos
FOR I% = 0 TO N%-1
sumsin += SINRADangles(I%)
sumcos += COSRADangles(I%)
NEXT
= DEGFNatan2(sumsin, sumcos)
DEF FNatan2(y,x) : ON ERROR LOCAL = SGN(y)*PI/2
IF x>0 THEN = ATN(y/x) ELSE IF y>0 THEN = ATN(y/x)+PI ELSE = ATN(y/x)-PI

View file

@ -0,0 +1,16 @@
INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0,0)
DIM a(6), b(5)
a() = 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2
b() = 4.1, 7.2, 1.7, 9.3, 4.4, 3.2
PRINT "Median of a() is " ; FNmedian(a())
PRINT "Median of b() is " ; FNmedian(b())
END
DEF FNmedian(a())
LOCAL C%
C% = DIM(a(),1) + 1
CALL Sort%, a(0)
= (a(C% DIV 2) + a((C%-1) DIV 2)) / 2

View file

@ -0,0 +1,25 @@
(median=
begin decimals end int list med med1 med2 num number
. 0:?list
& whl
' ( @( !arg
: ?
((%@:~" ":~",") ?:?number)
((" "|",") ?arg|:?arg)
)
& @( !number
: ( #?int "." [?begin #?decimals [?end
& !int+!decimals*10^(!begin+-1*!end):?num
| ?num
)
)
& (!num.)+!list:?list
)
& !list:?+[?end
& ( !end*1/2:~/
& !list:?+[!(=1/2*!end+-1)+(?med1.)+(?med2.)+?
& !med1*1/2+!med2*1/2:?med
| !list:?+[(div$(1/2*!end,1))+(?med.)+?
)
& !med
);

View file

@ -0,0 +1,33 @@
DIM a(10), b(4)
a() = 1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17
b() = 1, 2, 4, 4, 1
DIM modes(10)
PRINT "Mode(s) of a() = " ;
FOR i% = 1 TO FNmodes(a(), modes())
PRINT ; modes(i%) " " ;
NEXT
PRINT
PRINT "Mode(s) of b() = " ;
FOR i% = 1 TO FNmodes(b(), modes())
PRINT ; modes(i%) " " ;
NEXT
PRINT
END
DEF FNmodes(a(), m())
LOCAL I%, J%, N%, c%(), max%
N% = DIM(a(),1)
IF N% = 0 THEN m(1) = a(0) : = 1
DIM c%(N%)
FOR I% = 0 TO N%-1
FOR J% = I%+1 TO N%
IF a(I%) = a(J%) c%(I%) += 1
NEXT
IF c%(I%) > max% max% = c%(I%)
NEXT I%
J% = 0
FOR I% = 0 TO N%
IF c%(I%) = max% J% += 1 : m(J%) = a(I%)
NEXT
= J%

View file

@ -0,0 +1,23 @@
DIM a(9)
a() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
PRINT "Arithmetic mean = " ; FNarithmeticmean(a())
PRINT "Geometric mean = " ; FNgeometricmean(a())
PRINT "Harmonic mean = " ; FNharmonicmean(a())
END
DEF FNarithmeticmean(a())
= SUM(a()) / (DIM(a(),1)+1)
DEF FNgeometricmean(a())
LOCAL a, I%
a = 1
FOR I% = 0 TO DIM(a(),1)
a *= a(I%)
NEXT
= a ^ (1/(DIM(a(),1)+1))
DEF FNharmonicmean(a())
LOCAL b()
DIM b(DIM(a(),1))
b() = 1/a()
= (DIM(a(),1)+1) / SUM(b())

View file

@ -0,0 +1,7 @@
DIM array(9)
array() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
PRINT FNrms(array())
END
DEF FNrms(a()) = MOD(a()) / SQR(DIM(a(),1)+1)

View file

@ -0,0 +1,18 @@
MAXPERIOD = 10
FOR n = 1 TO 5
PRINT "Number = ";n TAB(12) " SMA3 = ";FNsma(n,3) TAB(30) " SMA5 = ";FNsma(n,5)
NEXT
FOR n = 5 TO 1 STEP -1
PRINT "Number = ";n TAB(12) " SMA3 = ";FNsma(n,3) TAB(30) " SMA5 = ";FNsma(n,5)
NEXT
END
DEF FNsma(number, period%)
PRIVATE nums(), accum(), index%(), window%()
DIM nums(MAXPERIOD,MAXPERIOD), accum(MAXPERIOD)
DIM index%(MAXPERIOD), window%(MAXPERIOD)
accum(period%) += number - nums(period%,index%(period%))
nums(period%,index%(period%)) = number
index%(period%) = (index%(period%) + 1) MOD period%
IF window%(period%)<period% window%(period%) += 1
= accum(period%) / window%(period%)

View file

@ -0,0 +1,29 @@
FOR x%=1 TO 10
test$=FNgenerate(RND(10))
PRINT "Bracket string ";test$;" is ";FNvalid(test$)
NEXT x%
END
:
DEFFNgenerate(n%)
LOCAL l%,r%,t%,output$
WHILE l%<n% AND r%<n%
CASE RND(2) OF
WHEN 1:
l%+=1
output$+="["
WHEN 2:
r%+=1
output$+="]"
ENDCASE
ENDWHILE
IF l%=n% THEN output$+=STRING$(n%-r%,"]") ELSE output$+=STRING$(n%-l%,"[")
=output$
:
DEFFNvalid(q$)
LOCAL x%,count%
IF LEN(q$)=0 THEN ="OK."
FOR x%=1 TO LEN(q$)
IF MID$(q$,x%,1)="[" THEN count%+=1 ELSE count%-=1
IF count%<0 THEN ="not OK."
NEXT x%
="OK."

View file

@ -0,0 +1,27 @@
( (bal=|"[" !bal "]" !bal)
& ( generate
= a j m n z N S someNumber
. !arg:<1&
| 11^123+13^666+17^321:?someNumber
& (!arg:?n)+1:?N
& :?S
& whl
' (!n+-1:~<0:?n&"[" "]" !S:?S)
& whl
' ( !someNumber:>0
& mod$(!someNumber.!N):?j
& div$(!someNumber.!N):?someNumber
& !S:?a [!j ?m [!N ?z
& !z !m !a:?S
)
& !S
)
& 0:?L
& whl
' ( generate$!L:?S
& put$(str$(!S ":"))
& out
$ (!S:!bal&Balanced|"Not balanced")
& !L+1:<11:?L
)
);

Some files were not shown because too many files have changed in this diff Show more