Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

演習問題の解答(null合体演算子 ?? の優先順位と結合規則)

Posted at

本稿は,拙著 null合体演算子 ?? の優先順位と結合規則 の最後に出題した演習問題の解答である.

問題

(a ?? b) ?? ca ?? (b ?? c) の演算結果は同じか.同じであれば証明し,そうでなければ反例を挙げよ.

解答

(a ?? b) ?? ca ?? (b ?? c) の演算結果は同じである.以下,その証明を述べる.

方針

ab のそれぞれがnullか否かで場合分けして考える.

証明

a がnullでないとき

\begin{align}
(a\ ??\ b)\ ??\ c
&=a\ ??\ c & (\because a\neq\text{null}よりa\ ??\ b=a) \\
&=a & (\because a\neq\text{null})
\end{align}

であり,さらに

\begin{align}
a\ ??\ (b\ ??\ c)&=a & (\because a\neq\text{null})
\end{align}

となる.

a がnullであり,かつ b がnullであるとき

\begin{align}
(a\ ??\ b)\ ??\ c
&=b\ ??\ c & (\because a=\text{null}よりa\ ??\ b=b) \\
&=c & (\because b=\text{null})
\end{align}

であり,さらに

\begin{align}
a\ ??\ (b\ ??\ c)
&=a\ ??\ c & (\because b=\text{null}よりb\ ??\ c=c) \\
&=c & (\because a=\text{null})
\end{align}

となる.

a がnullであり,かつ b がnullでないとき

\begin{align}
(a\ ??\ b)\ ??\ c
&=b\ ??\ c & (\because a=\text{null}よりa\ ??\ b=b) \\
&=b & (\because b\neq\text{null})
\end{align}

であり,さらに

\begin{align}
a\ ??\ (b\ ??\ c)
&=a\ ??\ b & (\because b\neq\text{null}よりb\ ??\ c=b) \\
&=b & (\because a=\text{null})
\end{align}

となる.

以上より,(a ?? b) ?? ca ?? (b ?? c) の演算結果は常に同じである.

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?