Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
-- 普通の足し算、かけ算、引き算
add x y = x + y
mul x y = x * y
sub x y = x - y
-- x y の値を元に計算を行い、その結果に対して関数 k を適用する。
add'cps x y k = k $ x + y
mul'cps x y k = k $ x * y
sub'cps x y k = k $ x - y
-- x y の値により、計算先(k, k')を決める
add1'cps x y k k'
| x < 10 && y < 10 = k add'
| otherwise = k' add'
where
add' = x + y
-- 述語を引数に追加
add2'cps x y p k k'
| p x y = k add'
| otherwise = k' add'
where
add' = x + y
add3'cps x y k = k x y $ x + y
-- 返す型を定義
data Value a = Return a | Raise Exception deriving Show
type Exception = String
main = do print (sub (mul (add 1 2) 3) 4)
print $ add'cps 1 2 (\x -> x)
print $ add'cps 1 2 (\x -> x * 2)
print $ add'cps 1 2 (\x -> "###" ++ show x ++"###")
add'cps 1 2 (\x -> print x)
print (add'cps 1 2 (\x ->
mul'cps x 3 (\y ->
sub'cps y 4 (\z -> z))))
print (add'cps 1 2 (\x ->
mul'cps x 3 (\y ->
sub'cps y 4 (\z ->
[x,y,z]))))
add'cps 1 2 $ \x ->
mul'cps x 3 $ \y ->
sub'cps y 4 $ \z -> print z
print $ add1'cps 1 2 (\x -> x * 10) (\x -> x)
print $ add1'cps 11 22 (\x -> x * 10) (\x -> x)
print $ add2'cps 1 2 (\x y -> x < y)
(\x -> x * 10) (\x -> x)
print $ add2'cps 1 2 (\x y -> x > y)
(\x -> x * 10) (\x -> x)
print $ add2'cps 1 2 (<)
Return (\_ -> Raise "Error")
print $ add2'cps 2 1 (<)
Return (\_ -> Raise "Error")
print $ add3'cps 1 2 $ \x y z ->
show x ++ " + " ++ show y ++ " = " ++ show z
-- 普通の足し算、かけ算、引き算
add x y = x + y
mul x y = x * y
sub x y = x - y
-- x y の値を元に計算を行い、その結果に対して関数 k を適用する。
add_cps x y k = k $ x + y
mul_cps x y k = k $ x * y
sub_cps x y k = k $ x - y
-- x y の値により、計算先(k, k')を決める
add1_cps x y k k'
| x < 10 && y < 10 = k add'
| otherwise = k' add'
where
add' = x + y
-- 述語を引数に追加
add2_cps x y p k k'
| p x y = k add'
| otherwise = k' add'
where
add' = x + y
add3_cps x y k = k x y $ x + y
-- 返す型を定義
data Value a = Return a | Raise Exception deriving Show
type Exception = String
main = do print (sub (mul (add 1 2) 3) 4)
print $ add_cps 1 2 (\x -> x)
print $ add_cps 1 2 (\x -> x * 2)
print $ add_cps 1 2 (\x -> "###" ++ show x ++"###")
add_cps 1 2 (\x -> print x)
print (add_cps 1 2 (\x ->
mul_cps x 3 (\y ->
sub_cps y 4 (\z -> z))))
print (add_cps 1 2 (\x ->
mul_cps x 3 (\y ->
sub_cps y 4 (\z ->
[x,y,z]))))
add_cps 1 2 $ \x ->
mul_cps x 3 $ \y ->
sub_cps y 4 $ \z -> print z
print $ add1_cps 1 2 (\x -> x * 10) (\x -> x)
print $ add1_cps 11 22 (\x -> x * 10) (\x -> x)
print $ add2_cps 1 2 (\x y -> x < y)
(\x -> x * 10) (\x -> x)
print $ add2_cps 1 2 (\x y -> x > y)
(\x -> x * 10) (\x -> x)
print $ add2_cps 1 2 (<)
Return (\_ -> Raise "Error")
print $ add2_cps 2 1 (<)
Return (\_ -> Raise "Error")
print $ add3_cps 1 2 $ \x y z ->
show x ++ " + " ++ show y ++ " = " ++ show z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment