basicdoc: wip

This commit is contained in:
minjaesong
2020-12-25 13:44:45 +09:00
parent 36f2c887d3
commit 796a63f579
3 changed files with 101 additions and 37 deletions

View File

@@ -13,12 +13,28 @@ Ok
Oh \emph{boy} we just did a computation! It printed out \code{4} which is a correct answer for $2+2$ and it didn't crash!
\section[GOTO]{GOTO here and there}
\code{GOTO} is used a lot in BASIC, and so does in \tbas. \code{GOTO} is a simplest method of diverging a program flow: execute only the some part of the program conditionally and perform a loop.
Following program attempts to calculate a square root of the input value, showing how \code{GOTO} can be used in such manner.
\begin{lstlisting}
10 X = 1337
20 Y = 0.5 * X
30 Z = Y
40 Y = Y-(((Y^2)-X)/(2*Y))
50 IF Z <> Y THEN GOTO 30
100 PRINT "Square root of ";X;" is approximately ";Y
\end{lstlisting}
Here, \code{GOTO} in line 50 to perform a loop, which keep loops until \code{Z} and \code{Y} becomes equal. This is a newtonian method of approximating a square root.
\section[When GOTO Is Bad]{Severe Acute Spaghettification Syndrome}
\section[Subroutine with GOSUB]{GOSUB to the rescue!}
\section[FOR--NEXT Loop]{FOR ever loop NEXT}
So you can make a loop using \code{GOTO}s here and there, but they \emph{totally suck}. Fortunately, there's better way to go about that: the FOR--NEXT loop!
So you can make a loop using \code{GOTO}s here and there, but they \emph{totally suck}, too much spaphetti crashes your cerebral cortex faster than \emph{Crash Bandicoot 2}. Fortunately, there's better way to go about that: the FOR--NEXT loop!
\begin{lstlisting}
10 FOR I = 1 TO 20
@@ -29,6 +45,8 @@ So you can make a loop using \code{GOTO}s here and there, but they \emph{totally
60 NEXT
\end{lstlisting}
When executed, this program print out triangles of stars. Still not convinced? Try to write a same program with \code{GOTO}s.
\section[Get User INPUT]{Isn't It Nice To Have a Computer That Will Question You?}
What fun is the program if it won't talk with you? You can make that happen with \code{INPUT} statement.
@@ -60,34 +78,51 @@ Lo and behold, the \code{DEFUN}! You can define a \emph{function} in a single li
\section[Recursion]{BRB: Bad Recursion BRB: Bad Recursion BRB: Bad Recursion BRB: Bad RecursionBRB: Bad Recursion BRBRangeError: Maximum call stack size exceeded}
But don't get over-excited, as it's super-trivial to create unintentional infinite loop.
But don't get over-excited, as it's super-trivial to create unintentional infinite loop:
\begin{lstlisting}
10 DEFUN ENDLESS(SHIT)=ENDLESS(SHIT)
20 ENDLESS(1)
\end{lstlisting}
\begin{lstlisting}
10 DEFUN FAC(N)=IF N==0 THEN 1 ELSE N*FAC(N-1)
10 DEFUN FAC(N)=N*FAC(N-1)
20 FOR K=1 TO 6
30 PRINT FAC(K)
40 NEXT
\end{lstlisting}
(if you tried this and computer becomes unresponsive, hit Ctrl-C to terminate the execution)
This failed attempt is to create a function that calculates a factorial of \code{N}. It didn't work because there is no \emph{halting condition}: didn't told computer to when to escape from the loop.
$n \times 1$ is always $n$, and $0!$ is $1$, so it would be nice to break out of the loop when \code{N} reaches $0$; here is the modified program:
\begin{lstlisting}
10 DEFUN FAC(N)=IF N==0 THEN 1 ELSE N*FAC(N-1)
20 K=MAP FAC, 1 TO 10
30 PRINT K
20 FOR K=1 TO 10
30 PRINT FAC(K)
40 NEXT
\end{lstlisting}
Since \code{IF-THEN-ELSE} can be chained to make third or more conditions --- \code{IF-THEN-ELSE IF-THEN} or something --- we can write a recursive Fibonacci function:
\begin{lstlisting}
10 DEFUN FIB(N)=IF N==0 THEN 0 ELSE IF N==1 THEN 1 ELSE FIB(N-1)+FIB(N-2)
20 FOR K=1 TO 12
20 FOR K=1 TO 10
30 PRINT FIB(K);" ";
40 NEXT
\end{lstlisting}
\section[MAPping]{Map}
\code{MAP} is a \emph{higher-order}\footnote{Higher-order function is a function that takes another function as an argument.} function that takes a function (called \emph{transformation}) and an array to construct a new array that contains old array transformed with given \emph{transformation}.
Or, think about the old \code{FAC} program before: it only printed out the value of $1!$, $2!$ \ldots\ $10!$. What if we wanted to build an array that contains such values?
\begin{lstlisting}
10 DEFUN FAC(N)=IF N==0 THEN 1 ELSE N*FAC(N-1)
20 K=MAP(FAC, 1 TO 10)
30 PRINT K
\end{lstlisting}
Here, \code{K} will contain the values of $1!$, $2!$ \ldots\ $10!$. We're just printing out the array, but you can make acutual use out of the array.
\section[Currying]{Haskell Curry Wants to Know Your Location}
\label{currying101}