Skip to content

Commit

Permalink
Correct typo and editorials in list based long division
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxinyu95 committed Dec 17, 2023
1 parent 29992b8 commit e261f5c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions others/appendix/list/list-en.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2235,7 +2235,7 @@ \section{zip and unzip}
mul1 b (a:as) = (b * a `mod` 10) : add [b * a `div` 10] (mul1 b as)
\end{Haskell}
4. Divide (with remainder). First, define how to test zero, how to compare two numbers. A number is zero if it's an empty list or all digits are 0:
4. Divide (with remainder). We are going to find $q$ and $r$, such that $as = q \cdot bs + r$, where $q$ is the quotient, $0 \leq r < bs$ is the remainder; $bs$ divides $as$ if $r = 0$. The constraint to $r$ demands us to first define how to test zero and how to compare two numbers. A number is zero if it's an empty list or all digits are 0:
\begin{Haskell}
isZero = all (== 0)
Expand Down Expand Up @@ -2267,7 +2267,7 @@ \section{zip and unzip}
\end{cases}
\]
To improve it, consider $as = q \cdot bs + r$, where $q$ is the quotient, $0 \leq r < bs$ is the remainder. Let $as = (a_m...a_2a_1)_{10} = [a_1, a_2, ..., a_m]$, we first use the brute force way to divide $a_m$ by $bs$ to get the quotient $q_m$ and the remainder $r_m = a_m - q_m \cdot bs$. Then add the next digit $a_{m-1}$ to divide $10 r_{m} + a_{m-1}$ by $bs$ to get the quotient $q_{m-1}$ and the remainder $r_{m-1}$. Finally, put all digits together. This is exactly the $foldr$ process:
To improve it, there is analogue to the long division from school math: consider $as = q \cdot bs + r$, let $as = (a_m...a_2a_1)_{10} = [a_1, a_2, ..., a_m]$, we first use the brute force way to divide $a_m$ by $bs$ to get the quotient $q_m$ and the remainder $r_m = a_m - q_m \cdot bs$. Then add the next digit $a_{m-1}$ to divide $10 r_{m} + a_{m-1}$ by $bs$ to get the quotient $q_{m-1}$ and the remainder $r_{m-1}$. Finally, put all digits together. This is exactly the $foldr$ process:
\begin{Haskell}
ldiv as bs | isZero bs = error "divide by 0"
Expand Down
4 changes: 2 additions & 2 deletions others/appendix/list/list-zh-cn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@ \section{关联和分解}
mul1 b (a:as) = (b * a `mod` 10) : add [b * a `div` 10] (mul1 b as)
\end{Haskell}
最后实现带余数的除法。首先需要定义如何判断一个数等于0,以及自然数之间的比较关系。如果列表为空或者所有位都是0则这个列表代表的数等于0:
最后实现带余数的除法。$as$除以$bs$的过程相当于寻找商$q$和余数$r$使得:$as = q \cdot bs + r$,其中余数必须小于除数,即:$0 \leq r < bs$,余数等于0则整除。所以首先需要定义如何判断一个数等于0,以及自然数之间的比较关系。如果列表为空或者所有位都是0则这个列表代表的数等于0:
\begin{Haskell}
isZero = all (== 0)
Expand Down Expand Up @@ -2288,7 +2288,7 @@ \section{关联和分解}
\end{cases}
\]
但是我们可以做得更好。考虑$as = q \cdot bs + r$其中$q$是商,$0 \leq q < bs$是余数。$as = (a_m...a_2a_1)_{10} = [a_1, a_2, ..., a_m]$,我们先用穷举法求$a_m$除以$bs$的商$q_m$和余数$r_m = a_m - q_m \cdot bs$。然后附加上下一位$a_{m-1}$,求$10 r_{m} + a_{m-1}$除以$bs$的商$q_{m-1}$和余数$r_{m-1}$。最后把商的各位连接起来。这恰好是$foldr$的过程:
但是我们可以做得更好。模仿课堂上长除法的过程:考虑$as = q \cdot bs + r$,令$as = (a_m...a_2a_1)_{10} = [a_1, a_2, ..., a_m]$,我们先用穷举法求$a_m$除以$bs$的商$q_m$和余数$r_m = a_m - q_m \cdot bs$。然后附加上下一位$a_{m-1}$,求$10 r_{m} + a_{m-1}$除以$bs$的商$q_{m-1}$和余数$r_{m-1}$。最后把商的各位连接起来。这恰好是$foldr$的过程:
\begin{Haskell}
ldiv as bs | isZero bs = error "divide by 0"
Expand Down

0 comments on commit e261f5c

Please sign in to comment.