Last active
December 6, 2018 20:22
-
-
Save azzgo/3582305fa766cf7f719efa75713cbc5d to your computer and use it in GitHub Desktop.
simple add function for natural number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
-- 半加器 | |
bitAdd :: String -> String -> (String, String) | |
-- 1 + n | |
bitAdd "1" "2" = ("0", "3") | |
bitAdd "1" "1" = ("0", "2") | |
bitAdd "1" "3" = ("0", "4") | |
bitAdd "1" "4" = ("0", "5") | |
bitAdd "1" "5" = ("0", "6") | |
bitAdd "1" "6" = ("0", "7") | |
bitAdd "1" "7" = ("0", "8") | |
bitAdd "1" "8" = ("0", "9") | |
bitAdd "1" "9" = ("1", "0") | |
-- 2 + | |
bitAdd "2" "2" = ("0", "4") | |
bitAdd "2" "3" = ("0", "5") | |
bitAdd "2" "4" = ("0", "6") | |
bitAdd "2" "5" = ("0", "7") | |
bitAdd "2" "6" = ("0", "8") | |
bitAdd "2" "7" = ("0", "9") | |
bitAdd "2" "8" = ("1", "0") | |
bitAdd "2" "9" = ("1", "1") | |
-- 3 + n | |
bitAdd "3" "3" = ("0", "6") | |
bitAdd "3" "4" = ("0", "7") | |
bitAdd "3" "5" = ("0", "8") | |
bitAdd "3" "6" = ("0", "9") | |
bitAdd "3" "7" = ("1", "0") | |
bitAdd "3" "8" = ("1", "1") | |
bitAdd "3" "9" = ("1", "2") | |
-- 4 + n | |
bitAdd "4" "4" = ("0", "8") | |
bitAdd "4" "5" = ("0", "9") | |
bitAdd "4" "6" = ("1", "0") | |
bitAdd "4" "7" = ("1", "1") | |
bitAdd "4" "8" = ("1", "2") | |
bitAdd "4" "9" = ("1", "3") | |
-- 5 + n | |
bitAdd "5" "5" = ("1", "0") | |
bitAdd "5" "6" = ("1", "1") | |
bitAdd "5" "7" = ("1", "2") | |
bitAdd "5" "8" = ("1", "3") | |
bitAdd "5" "9" = ("1", "4") | |
-- 6 + n | |
bitAdd "6" "6" = ("1", "2") | |
bitAdd "6" "7" = ("1", "3") | |
bitAdd "6" "8" = ("1", "4") | |
bitAdd "6" "9" = ("1", "5") | |
-- 7 + n | |
bitAdd "7" "7" = ("1", "4") | |
bitAdd "7" "8" = ("1", "5") | |
bitAdd "7" "9" = ("1", "6") | |
-- 8 + n | |
bitAdd "8" "8" = ("1", "6") | |
bitAdd "8" "9" = ("1", "7") | |
-- 9 + n | |
bitAdd "9" "9" = ("1", "8") | |
-- 0 + n | |
bitAdd "0" a = ("0", a) | |
-- 加法交换律 | |
bitAdd a b = bitAdd b a | |
fullAdd :: String -> String -> (String, String) | |
fullAdd a b | |
| (length a == 1) && (length b == 1) = a `bitAdd` b | |
| length a == 0 = ("0", b) | |
| length b == 0 = ("0", a) | |
| otherwise = | |
let (carry, sum) = [last a] `bitAdd` [last b] in | |
let (subCarry, subSum) = fullAdd (init a) (init b) | |
in let (lastCarry, totalSum) = fullAdd carry subSum in | |
( | |
case (subCarry, lastCarry) of | |
("1", "0") -> "1" | |
("0", "1") -> "1" | |
("1", "1") -> "2" | |
("0", "0") -> "0" | |
, totalSum ++ sum) | |
add :: String -> String -> String | |
add a b | |
| (length a == 0) || (length b == 0) = "NaN" | |
| a == "0" = b | |
| b == "0" = a | |
| otherwise = let (carry, sum) = fullAdd a b in case carry of | |
"1" -> "1" ++ sum | |
"0" -> sum | |
main = print ("9944" `add` "22") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment