If we apply some simple definitions to terms, HTML is a valid language to use for this. (i.e. Program: Series of instructions to be executed. HTML thusly instructs the interpreter to write the code.)
code segment ; Start of the code
assume cs:code ; Load code segment register
org 100h ; and set program up as a .COM file
start:
assume cs:code
mov ax, 1 ; Start at 1
loop:
call outdec ; Output the AX register
inc ax ; next value
cmp ax, 10 ; Above 10 yet?
jle loop
mov al, 0
mov ah, 4Ch
int 21h ; Exit
; ******************************** ;
outdec:
push ax ; save AX
mov bx, 10 ; divisor
mov cx, 3 ; # characters
convloopd:
mov dx, 0 ; zero high word for div
div bx ; divide by 10
add dl, 30h ; convert remainder to char
push dx ; Save character
loop convloopd ; do it til cx = 0
mov cx, 3 ; start another loop
outloopd:
pop dx ; get the character
mov ah, 02h ; output character fcn
int 21h ; output it
loop outloopd
mov ah, 02h
mov dl, ' '
int 21h ; output 2 spaces
int 21h
pop ax ; restore original number
ret
code ends
end start
First, let's implement a datatype for storing integers.
> data IntType = Zero
> | Succ IntType
> | Pred IntType
> deriving Eq
Then we'll want to implement comparison for integers.
> instance Ord IntType where
> compare (Pred xt) (Pred yt) = compare xt yt
> compare (Pred _) _ = LT
> compare _ (Pred _) = GT
> compare Zero Zero = EQ
> compare (Succ xt) (Succ yt) = compare xt yt
> compare (Succ _) _ = GT
> compare _ (Succ _) = LT
Integers should be enumerable too...
> instance Enum IntType where
> toEnum 0 = Zero
> toEnum n = accume n Zero
> where accume 0 k = k
> accume n k
> | n < 0 = accume (n+1) (Pred k)
> | n > 0 = accume (n-1) (Succ k)
> fromEnum k = adder 0 k
> where adder n Zero = n
> adder n (Pred kt) = adder (n-1) kt
> adder n (Succ kt) = adder (n+1) kt
> succ (Pred kt) = kt
> succ k = Succ k
> pred (Succ kt) = kt
> pred k = Pred k
> enumFrom x = iterate succ x
> enumFromTo x y
> | x == y = [x]
> | otherwise = x : enumFromTo (succ x) y
And we need to perform arithmetic.
> instance Num IntType where
> x + Zero = x
> Zero + y = y
> (Pred xt) + y = xt + pred y
> (Succ xt) + y = xt + succ y
> x - Zero = x
> x - (Pred yt) = succ x - yt
> x - (Succ yt) = pred x - yt
> Zero * y = Zero
> (Pred xt) * y = xt * y - y
> (Succ xt) * y = xt * y + y
> abs x@(Pred _) = Zero - x
> abs x = x
> signum (Pred _) = Pred Zero
> signum (Succ _) = Succ Zero
> signum Zero = Zero
> fromInteger n = accume n Zero
> where accume 0 k = k
> accume n k
> | n < 0 = accume (n+1) (Pred k)
> | n > 0 = accume (n-1) (Succ k)
They must be an instance of Real in order to be an instance of
Integral.
> instance Real IntType where
> toRational = toRational . toInteger
Our integers are indeed integral :-)
> instance Integral IntType where
> quotRem Zero _ = (Zero, Zero)
> quotRem n d = appsig (signum n * signum d) (foo n d)
> where appsig k (q,r) = (k*q, k*r)
> foo n d = countup (abs n) (abs d) Zero
> countup n@(Pred _) d c
> = (pred c, n + d)
> countup n d c
> = countup (n - d) d (succ c)
> toInteger k = acc 0 k
> where acc n Zero = n
> acc n (Pred kt) = acc (n-1) kt
> acc n (Succ kt) = acc (n+1) kt
Finally, implement an instance of the Show class, for converting
integers to strings.
> instance Show IntType where
> show n | n == Zero = "0"
> | n < Zero = '-' : show (negate n)
> | n > Zero = onshow n ""
> where ten = Succ $ Succ $ Succ $ Succ $ Succ five
> five = Succ $ Succ $ Succ $ Succ $ Succ Zero
> onshow Zero s = s
> onshow k s = let (q,r) = quotRem k ten
> in onshow q (charate r : s)
> charate Zero = '0'
> charate (Succ kt) = succ (charate kt)
Might as well show off that it works...
> greaterroot :: IntType -> IntType
> greaterroot n = head [k | k <- [Zero ..], k * k >= n]
Finally, our main function! Make a list of perfect squares from Zero
to (Succ (Succ (Succ ... (one hundred times) ... (Succ Zero) ... ))),
then find their square roots, and output them!
> main :: IO ()
> main =
> mapM_ (putStrLn . show) $ map greaterroot squares
> where squares = map (\x -> x*x) [one .. ten]
> one = Succ Zero
> two = one + one
> five = two + two + one
> ten = two * five
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.