Dear Racketeers!

I would like to write the function divide in racket, for performing the long 
division, which would print the whole computational process, as the elementary 
school pupils do. For example, the call (divide 5432, 12) should print this:

 5432 : 12 = 452
-48
 --
  63
 -60
  --
   32
  -24
   --
    8
    =


Finally, I wrote this function in python, but I'm not happy about it because 
I've been "tinkering around" and I feel that it can be much better written by 
following the principles described in HtDP. The problem is I don't know how to 
do that, though I read the book and solved a lot of problems from it. I simply 
don't know how to "derive" clean and nice program for this function.

My (ugly) python code looks like this:

def divide(x, y):
    if x < y:
        return 0, x
    x = str(x)
    q = ""
    r = ""
    workings = ""
    firsttime = True
    for i in range(1, len(x) + 1):
        r = r + "0"
        num = int(r) + int(x[i-1])
        sum = 0
        m = 0
        while sum + y <= num:
            m += 1
            sum += y
        r = str(num - sum)
        q = q + str(m)
        ls = len(str(sum))
        if sum == 0 and firsttime:
            continue
        firsttime = False
        workings += (i-ls)* " " + "-" + str(sum) + "\n"
        workings += (i + 1 -ls) * " " + ls * "-" + "\n"
        workings += (i + 1 - len(r)) * " " + r + ("" if i >= len(x) else x[i]) 
+ "\n"

    workings += (len(x) - len(r) + 1) * " " + len(r) * "=" + "\n"
    print(" " + x + " : " + str(y) + " = " + str(int(q)) + "\n" + workings)


I'm wondering how to write nice and clean version of the above program in the 
proper HtDP way. Can someone here show how to solve this problem strictly 
following the HtDP methodology?

Nice regards,
Jaroslaw Modry

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to