\part*{$BIUO?(B}

\section{$B%M%C%H%o!<%/%W%m%0%i%_%s%0$=$N(B1$B!'(BHTTP}

\begin{exercise}
\code{\#load "unix.cma"}$B!$(B\code{open Unix}$B$r$=$l$>$l<B9T$7$F!$(B
\texttt{Unix}$B%b%8%e!<%k!J%i%$%V%i%j!K$r%m!<%I$7$h$&!%(B
\end{exercise}

Unix$B$H$O%*%Z%l!<%F%#%s%0%7%9%F%`!J(BOS$B!K$NL>A0$G$"$k!%(B
$B$$$o$f$k%$%s%?!<%M%C%H$G;HMQ$5$l$F$$$k(BTCP/IP$BDL?.J}<0$O!$(BUnix$B$rCf?4$KH/E8$7$F$-$?!%(B
$B8=:_$G$O!$(BUnix$B0J30$N(BOS$B$K$b(BUnix$B$HF1MM$N(BTCP/IP$BDL?.5!G=$,$"$k!%(B

$B%$%s%?!<%M%C%H$G$O!$(B\url{www.tohoku.ac.jp}$B$N$h$&$J?M4V$K$o$+$j$d$9$$L>A0!J%[%9%HL>!K(B
$B$N$[$+$K!$(B
\texttt{208.77.188.166}$B$N$h$&$J?t;z$N%"%I%l%9!J(BIP$B%"%I%l%9!K$,$"$k!%(B

\begin{exercise}
\code{let host = gethostbyname "www.tohoku.ac.jp"}$B$*$h$S(B
\code{let addr = host.h\us{}addr\us{}list.(0)}$B$r<B9T$7$F$_$h$&!%(B
$B$5$i$K!$(B\code{string\us{}of\us{}inet\us{}addr addr}$B$rI>2A$7$F$_$h$&!%(B
$B$I$s$J7k2L$,I=<($5$l$?$+!)(B
\end{exercise}

% # let host = gethostbyname "www.tohoku.ac.jp";;
% val host : Unix.host_entry =
%   {h_name = "toppage.bureau.tohoku.ac.jp";
%    h_aliases = [|"www.tohoku.ac.jp"|]; h_addrtype = PF_INET;
%    h_addr_list = [|<abstr>|]}
% # let addr = host.h_addr_list.(0);;
% val addr : Unix.inet_addr = <abstr>
% # string_of_inet_addr addr;;
% - : string = "130.34.15.80"

$B$$$o$f$k%$%s%?!<%M%C%H$N!V%[!<%`%Z!<%8!W$O(BHTML$B$H$$$&8@8l$G5-=R$5$l$F$$$k!%(B

\begin{exercise}
WWW$B%V%i%&%6$G!$(B
\url{http://www.tohoku.ac.jp/english/}$B$r3+$-!$(B
$B1&%/%j%C%/$r$7!V%Z!<%8$N%=!<%9$rI=<(!W$7$F$_$h$&!%(B
\end{exercise}

\begin{exercise}
$B0J2<$NDj5A$d<0$r=g$K<B9T$J$$$7I>2A$7$F$_$h$&!%(B
\begin{programcode}
let (ic, oc) = open_connection (ADDR_INET(addr, 80));;
output_string oc "GET /english/ HTTP/1.0\bs{}r\bs{}n";;
output_string oc "Host: www.tohoku.ac.jp\bs{}r\bs{}n\bs{}r\bs{}n";;
flush oc;;
print_endline (input_line ic);;
print_endline (input_line ic);;
 \vdots {\rm \raisebox{2pt}{\mbox{($B$7$P$i$/>e5-%3%^%s%I$r7+$jJV$9!%%3%T!<(B\&$B%Z!<%9%H$7$h$&!%(B)}}}
print_endline (input_line ic);;
close_in ic;;
\end{programcode}
\end{exercise}

$B0lHL$K(BWWW$B%V%i%&%6$O!$$3$N$h$&$K$7$F%@%&%s%m!<%I$7$?(BHTML$BJ8=q$r2r@O!&IA2h$9$k$3$H$G!$(B
$B%[!<%`%Z!<%8$rI=<($7$F$$$k!%(B

\section{$B%M%C%H%o!<%/%W%m%0%i%_%s%0$=$N(B2$B!'(BSMTP}


\begin{exercise}
$B0J2<$NDj5A$d<0$r=g$K<B9T$J$$$7I>2A$7$F$_$h$&!%(B
$BAw?.85$OElKLBg3X$G$N<+J,$N%a!<%k%"%I%l%9!$Aw?.@h$O<+J,$N7HBS$N%a!<%k%"%I%l%9$K$7$h$&!%(B
$B$b$77HBSEEOC$N%a!<%k%"%I%l%9$r$b$C$F$J$1$l$P!$<+J,$NB>$N%a!<%k%"%I%l%9$G$b$h$$!%(B
\emph{$B7h$7$FB>?M$N%a!<%k%"%I%l%9$r;HMQ$7$J$$$3$H(B}$B!%(B
\begin{programcode}
let host = gethostbyname "primergy.kb-private";;
let addr = host.h_addr_list.(0);;
let (ic, oc) = open_connection(ADDR_INET(addr,25));;
print_endline (input_line ic);;
output_string oc "HELO localhost\bs{}r\bs{}n";;
flush oc;;
print_endline (input_line ic);;
output_string oc "MAIL FROM: \textcolor{blue}{\mbox{$BAw?.85%a!<%k%"%I%l%9(B}}\bs{}r\bs{}n";;
flush oc;;
print_endline (input_line ic);;
output_string oc "RCPT TO: \textcolor{blue}{\mbox{$BAw?.@h%a!<%k%"%I%l%9(B}}\bs{}r\bs{}n";;
flush oc;;
print_endline (input_line ic);;
output_string oc "DATA\bs{}r\bs{}n";;
flush oc;;
print_endline (input_line ic);;
output_string oc "From: \textcolor{blue}{\mbox{$BAw?.85%a!<%k%"%I%l%9(B}}\bs{}r\bs{}n";;
output_string oc "To: \textcolor{blue}{\mbox{$BAw?.@h%a!<%k%"%I%l%9(B}}\bs{}r\bs{}n";;
output_string oc "Subject: test mail\bs{}r\bs{}n\bs{}r\bs{}n";;
output_string oc "this is a test\bs{}r\bs{}n";;
output_string oc ".\bs{}r\bs{}n";;
flush oc;;
print_endline (input_line ic);;
output_string oc "quit\bs{}r\bs{}n";;
flush oc;;
print_endline (input_line ic);;
close_in ic;;
\end{programcode}
\label{ex:send-mail}
\end{exercise}

\begin{exercise}
\exref{ex:send-mail}$B$K$*$$$F!$!VAw?.85%a!<%k%"%I%l%9!W$K(B
\texttt{dummy@example.com}$B!J<B:_$7$J$$!K$r;XDj$7$?$i$I$&$J$k$+!)(B
\end{exercise}


\section{3D$B%W%m%0%i%_%s%0(B}

\begin{exercise}
\ocaml $B$r=*N;$7!$(B\texttt{ocaml}$B$N$+$o$j$K(B
\texttt{lablglut}$B$r5/F0$7$F$+$i!$(B
$B0J2<$NDj5A$d<0$r=g$K<B9T$J$$$7I>2A$7$h$&!%(B
\texttt{(*}$B$H(B\texttt{*)}$B$G0O$^$l$?ItJ,$O%3%a%s%H$J$N$GF~NO$7$J$/$F$b$h$$!%(B

\begin{note}
$B!V(B\texttt{`}$B!W$O%P%C%/%/%)!<%H$H$$$&5-9f$G!$(B
$BI8=`E*$JF|K\8l%-!<%\!<%I$G$O!$(B
\texttt{Shift}$B%-!<$r2!$7$J$,$i(B\texttt{@}$B%-!<$r2!$;$PF~NO$G$-$k!%(B
$B!V(B\texttt{'}$B!W!J%/%)!<%H!K$H$O0[$J$k$N$G5$$r$D$1$k$3$H!%(B
$B!V(B\texttt{\chartilde}$B!W$O%A%k%@$H$$$&5-9f$G!$(B
$BI8=`E*$JF|K\8l%-!<%\!<%I$G$O!$(B
\texttt{Shift}$B%-!<$r2!$7$J$,$i(B\texttt{\charhat}$B%-!<$r2!$;$PF~NO$G$-$k!%(B
\end{note}

\begin{programcode}
(* $B=i4|2==hM}(B *) 
Glut.init Sys.argv;;

(* $B%&%$%s%I%&$N=`Hw(B *) 
Glut.createWindow "souzou";;

(* $B%i%$%H$rM-8z2=(B *) 
Gl.enable `lighting;; 

(* 1$BHV$N%i%$%H$rGr?'$N4D6-8w$K@_Dj(B *) 
GlLight.light 1 (`ambient (1.0, 1.0, 1.0, 1.0));; 
(* 1 $BHV$N%i%$%H$rM-8z2=(B *) 
Gl.enable `light1;; 
(* 2 $BHV$N%i%$%H$r@V?'$NE@8w8;$K@_Dj(B *) 
GlLight.light 2 (`diffuse (1.0, 0.0, 0.0, 1.0));; 
(* 2 $BHV$N%i%$%H$r1&J}$K@_CV(B *) 
GlLight.light 2 (`position (2.0, 0.0, 0.0, 0.0));; 
(* 2 $BHV$N%i%$%H$rM-8z2=(B *) 
Gl.enable `light2;; 
(* 3 $BHV$N%i%$%H$r@D?'$NE@8w8;$K@_Dj(B *)
GlLight.light 3 (`diffuse (0.0, 0.0, 1.0, 1.0));; 
(* 3 $BHV$N%i%$%H$r:8J}$K@_CV(B *) 
GlLight.light 3 (`position (-2.0, 0.0, 0.0, 0.0));; 
(* 3 $BHV$N%i%$%H$rM-8z2=(B *)
Gl.enable `light3;;

(* $BIA2h4X?t(B *) 
let rec display () =
  (* $B2hLL$r%/%j%"$9$k$H$-$N?'$r9u$K@_Dj(B *) 
  GlClear.color (0.0, 0.0, 0.0);
  (* $B2hLL$r%/%j%"(B *) 
  GlClear.clear [`color];
  (* $B%5%$%:(B 0.5 $B$N%F%#!<%]%C%H$rIA2h(B *) 
  Glut.solidTeapot 0.5; 
  (* $B%P%C%U%!$r%U%i%C%7%e(B *) 
  Gl.flush ();;

(* $BIA2h4X?t$rEPO?(B *) 
Glut.displayFunc display;;

(* $B%-!<%\!<%I=hM}4X?t(B *) 
let keyboard ~key:k ~x:x ~y:y =
  (* $B%F%#!<%]%C%H$r2sE>(B *) 
  GlMat.rotate ~angle:2.0 ~x:0.25 ~y:0.5 ~z:1.0 (); 
  (* $BIA2h4X?t(B display $B$r8F$S=P$7$FA42hLL$r:FIA2h(B *) 
  display ();;

(* $B%-!<%\!<%I=hM}4X?t$rEPO?(B *) 
Glut.keyboardFunc keyboard;;

(* $B%a%$%s%k!<%A%s$r5/F0(B *) 
Glut.mainLoop ();;
\end{programcode}
$B0J>e$r$9$Y$F<B9T$9$k$H!$(B
$B!V(B\texttt{souzou}$B!W%&%#%s%I%%$,=P$F$/$k$N$G%9%Z!<%9%-!<$J$IE,Ev$J%-!<$r(B
$B2?EY$b2!$7$F$_$h$&!%(B
$B=*N;$9$k$H$-$O!V%3%^%s%I%W%m%s%W%H!W%&%$%s%I%&1&>e$N!_%\%?%s$G%&%$%s%I%&$rJD$8$l$P$h$$!%(B
\end{exercise}


\section{$B%^%&%9$H%-!<%\!<%I$+$i$NF~NO(B\\$B$*$h$S%l%3!<%I$H%P%j%"%s%H$K$D$$$F(B}

\begin{exercise}
\code{\#load "graphics.cma"}$B!$(B\code{open Graphics}$B!$(B\code{open\us{}graph ""}
$B$r=g$K<B9T$7$F$+$i!$(B\code{wait\us{}next\us{}event [Button\us{}down]}$B$r<B9T$7$?8e!$(B
\texttt{Caml graphics}$B%&%#%s%I%&$NCf$NE,Ev$J0lE@$r%/%j%C%/$7$F$_$h$&!%(B
$B%/%j%C%/$9$k0LCV$K$h$C$F!$JV$jCM$O$I$N$h$&$KJQ2=$9$k$@$m$&$+!)(B
\keywords{$B%l%3!<%I!$%i%Y%k(B}
\end{exercise}

\begin{exercise}
$BF1MM$K(B\code{let s = wait\us{}next\us{}event [Button\us{}down]}$B$r<B9T$7$F$+$i!$(B
$B!J(B\texttt{Caml graphics}$B%&%#%s%I%&$NCf$NE,Ev$J0lE@$r%/%j%C%/$7$?8e$G!K(B
\code{s.mouse\us{}x}$B!$(B\code{s.mouse\us{}y}$B$r$=$l$>$lI>2A$7$F$_$h$&!%(B
\keywords{$B%U%#!<%k%I!$%U%#!<%k%ICM$N<h$j=P$7(B}
\end{exercise}

\begin{exercise}
 \code{let s = wait\us{}next\us{}event [Key\us{}pressed]}$B$r<B9T$7$F$+$i!$(B
\texttt{Caml graphics}$B%&%#%s%I%&$GE,Ev$J%-!<$r2!$7!$$=$N8e$G(B
\code{s.key}$B$rI>2A$7$F$_$h$&!%(B
\keywords{$B%P%j%"%s%H!$9=@.;R(B}
\end{exercise}

\begin{exercise}
\code{let s = wait\us{}next\us{}event [Key\us{}pressed;Button\us{}down]}$B$N>l9g$O$I$&$+!)(B
$B%^%&%9$r%/%j%C%/$7$?>l9g$H%-!<$r2!$7$?>l9g$NN>J}$r;n$7$F$_$h$&!%(B
\keywords{$B%j%9%H(B}
\end{exercise}

\section{$B%j%9%H$HG[Ns(B}

$B$I$A$i$b!V!JF1$87?$N!KJ#?t$NCM$r=gHV$KJB$Y$F0l$D$K$^$H$a$?CM!W!%(B

\subsection*{$B%j%9%H$HG[Ns$N=q$-J}(B}

$B%j%9%H$O(B\texttt{[}$B$G;O$^$j!$(B\texttt{;}$B$G6h@Z$C$F!$(B\texttt{]}$B$G=*$o$k!%(B
\begin{programcode}
\system{\#} [ 1 ; 2 ; 3 ];;
\system{- : int list = [1; 2; 3] }
\system{\#} [ 3 ; 2 ; 1 ];;     (* $B=gHV$O6hJL$9$k(B *) 
\system{- : int list = [3; 2; 1] }
\system{\#} [ 1 ; 1 ; 2 ; 3 ];; (* $B=EJ#$b6hJL$9$k(B *) 
\system{- : int list = [1; 1; 2; 3]}
\end{programcode}

$BG[Ns$O(B\texttt{[|}$B$G;O$^$j!$(B\texttt{;}$B$G6h@Z$C$F!$(B\texttt{|]}$B$G=*$o$k!%(B
\begin{programcode}
\system{\#} [| 1 ; 2 ; 3 |];;
\system{- : int array = [|1; 2; 3|] }
\system{\#} [| 3 ; 2 ; 1 |];;     (* $B=gHV$O6hJL$9$k(B *) 
\system{- : int array = [|3; 2; 1|] }
\system{\#} [| 1 ; 1 ; 2 ; 3 |];; (* $B=EJ#$b6hJL$9$k(B *) 
\system{- : int array = [|1; 1; 2; 3|]}
\end{programcode}

\subsection*{$B%j%9%H$HG[Ns$KBP$9$kA`:n(B}

\subsubsection*{$B%j%9%H$KBP$9$kA`:n(B}


\begin{description}
\item[\texttt{x :: ys}]~\\
$B0l$D$N(B\emph{$BMWAG(B}\texttt{x}$B$r!$%j%9%H(B\texttt{ys}$B$N@hF,$KDI2C$7$?%j%9%H$rJV$9!%(B

\begin{programcode}
\system{\#} let ys = [ 1 ; 2 ; 3 ];;
\system{val ys : int list = [1; 2; 3] }
\system{\#} 4 :: ys;;
\system{- : int list = [4; 1; 2; 3] }
\end{programcode}

\item[\texttt{xs @ ys}]~\\
\emph{$B%j%9%H(B}\texttt{xs}$B$N8e$m$K%j%9%H(B\texttt{ys}$B$rO"7k$7$?%j%9%H$rJV$9!%(B


\begin{programcode}
\system{\#} let xs = [ 1; 2; 3; ];;
\system{val xs : int list = [1; 2; 3]}
\system{#} let ys = [ 4; 5; 6; ];;
\system{val ys : int list = [4; 5; 6]}
\system{#} xs @ ys;;
\system{- : int list = [1; 2; 3; 4; 5; 6]}
\end{programcode}

\item[\texttt{match \(\mathit{xs}\) with [] -> \({$B<0(B}\sb{1}\) | x::ys -> \($B<0(B\sb{2}\) }]~\\
  $B%j%9%H(B$\mathit{xs}$$B$rD4$Y!$(B
$B6u%j%9%H(B$\texttt{[]}$$B$G$"$C$?$J$i$P(B${$B<0(B}\sb{1}$$B$rI>2A$9$k!%(B
$B6u$G$J$1$l$P!$@hF,$NMWAG$r(B\texttt{x}$B!$$=$l0J30$N;D$j$N%j%9%H$r(B$\texttt{ys}$$B$H$7$F!$(B
${$B<0(B}\sb{2}$$B$rI>2A$9$k!%(B

\begin{programcode}
\system{#} let e = [];;
\system{val e : 'a list = []}
\system{#} match e with [] -> "empty" | x::ys -> "non-empty";;
\system{- : string = "empty"}
\system{#} let xs = [1;2;3];;
\system{val xs : int list = [1; 2; 3]}
\system{#} match xs with [] -> -1 | x::ys -> x;;
\system{- : int = 1}
\system{#} match xs with [] -> [] | x::ys -> ys;;
\system{- : int list = [2; 3]}
\end{programcode}

$B%j%9%H$,6u$G$J$$3N?.$,$"$l$P!$(B\texttt{[] -> \({$B<0(B}\sb{1}\) |}
$B$NItJ,$r>JN,$7$F$b$h$$!%!J$?$@$77Y9p$,$G$k!%!K(B

\begin{programcode}
\system{#} {match xs with x::ys -> ys};;
\system{Characters 0-27:
  match xs with x::ys -> ys;;
  ^^^^^^^^^^^^^^^^^^^^^^^^^
Warning P: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
- : int list = [2; 3]}
\end{programcode}

$B%j%9%H$,6u$N$H$-(B\texttt{[] -> \({$B<0(B}\sb{1}\) |}
$B$NItJ,$r>JN,$9$k$H%(%i!<!JNc30!K$,5/$3$j!$!JNc30=hM}$7$J$1$l$P!K%W%m%0%i%`$N<B9T$,Cf;_$5$l$k!%(B

\begin{programcode}
\system{#} match e with x::ys -> ys;; 
\system{Characters 0-26:
  match e with x::ys -> ys;;
  ^^^^^^^^^^^^^^^^^^^^^^^^
Warning P: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
Exception: Match_failure ("", 1, 0).}
\end{programcode}
\end{description}


\subsubsection*{$BG[Ns$KBP$9$kA`:n(B}
\begin{description}
\item[\texttt{a.(i)}]~\\
$BG[Ns(B\texttt{a}$B$N(B\texttt{i}$BHVL\$NMWAG$rJV$9!%(B

\begin{programcode}
\system{#} let a = [| "apple"; "orange"; "banana" |];;
\system{val a : string array = [|"apple"; "orange"; "banana"|]}
\system{#} a.(0);;
\system{- : string = "apple"}
\system{#} a.(1);;
\system{- : string = "orange"}
\system{#} a.(2);;
\system{- : string = "banana"}
\end{programcode}

\item[\texttt{a.(i) <- x}]~\\
$BG[Ns(B\texttt{a}$B$N(B\texttt{i}$BHVL\$NMWAG$r(B\texttt{x}$B$G>e=q$-$9$k!J$3$N<0<+BN$NJV$jCM$O(B\texttt{unit}$B!K!%(B

\begin{programcode}
\system{#} let a = [| "apple"; "orange"; "banana" |];;
\system{val a : string array = [|"apple"; "orange"; "banana"|]}
\system{#} a.(1) <- "melon";;
\system{- : unit = ()}
\system{#} a;;
\system{- : string array = [|"apple"; "melon"; "banana"|]}
\end{programcode}

\item[\texttt{Array.make n x}]~\\
$BMWAG?t$,(B\texttt{n}$B$G!$A4MWAG$,(B\texttt{x}$B$G$"$k$h$&$JG[Ns$r?7$?$K:n$C$FJV$9!%(B

\begin{programcode}
\system{#} let a = Array.make 8 0.5;;
\system{val a : float array = [|0.5; 0.5; 0.5; 0.5; 0.5; 0.5; 0.5; 0.5|]}
\system{#} a.(1) <- 3.;;
\system{- : unit = ()}
\system{#} a;;
\system{- : float array = [|0.5; 3.; 0.5; 0.5; 0.5; 0.5; 0.5; 0.5|]}
\end{programcode}

\end{description}


\subsection*{$B%j%9%H$HG[Ns$N0c$$!J(B\ocaml $B$N>l9g!K(B}

\paragraph{$B%j%9%H(B}
\begin{itemize}
 \item $B%j%9%H$O(B\texttt{::}$B$G!V0l$D$NMWAG$r@hF,$KDI2C$7$?%j%9%H!W$r:n$k$N$,MF0W!%(B
 \item $B%j%9%H$O(B\texttt{match}$B$G!V@hF,$NMWAG$H$=$l0J30$N;D$j$N%j%9%H!W$r<h$j=P$9$N$,MF0W!%(B
\end{itemize}


\paragraph{$BG[Ns(B}
\begin{itemize}
 \item $BG[Ns$OG$0U$N(B\texttt{i}$B$K$D$$$F!V(B\texttt{i}$BHVL\$NMWAG!W$r9bB.$K<h$j=P$9$3$H$,$G$-$k!%(B
   $B!J%j%9%H$G$b(B\texttt{List.nth xs i}$B<h$j=P$9$3$H$O2DG=$G$"$k$,!$(B\texttt{i}$B$,Bg$-$$$H;~4V$,$+$+$k!K(B
 \item $BG[Ns$OMWAG$r(B\emph{$B>e=q$-(B}$B$9$k$3$H$,$G$-$k!J%j%9%H$O$G$-$J$$!K!%(B
\end{itemize}

