Commit 0b3c4094 authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen

Merge branch 'master' of vo20.nl:/git/uva

parents 6c6d35c3 e37f59f9
...@@ -16,5 +16,6 @@ robotica/ ...@@ -16,5 +16,6 @@ robotica/
*.toc *.toc
*.cmi *.cmi
*.cmo *.cmo
*.exe
*# *#
*~ *~
...@@ -2,3 +2,88 @@ ...@@ -2,3 +2,88 @@
let isLeapYear y = let isLeapYear y =
y > 1582 && y mod 4 = 0 && (y mod 100 != 0 || y mod 400 = 0) y > 1582 && y mod 4 = 0 && (y mod 100 != 0 || y mod 400 = 0)
;; ;;
(* Test the isLeapYear function with a number of known leap years *)
let test_isLeapYear y expect =
let yields = isLeapYear y in
Printf.printf "%d -> %s, (should be %b, yields %b)\n"
y (if yields = expect then "success" else "failure") expect yields
;;
test_isLeapYear 1500 false;;
test_isLeapYear 1900 false;;
test_isLeapYear 1904 true;;
(* Give a proper English character string representation of a date *)
let date2str day month year =
if day < 1 || day > 31 || month < 1 || month > 12 || year < 0 then
raise (Failure "invalid date")
else
(* Get the textual postfix of a day number *)
let getDayPostfix day =
match day with
1 | 21 | 31 -> "st"
| 2 | 22 -> "nd"
| 3 | 23 -> "rd"
| _ -> "th" in
let months_str = ["January"; "February"; "March"; "April"; "May";
"June"; "July"; "August"; "September"; "October"; "November";
"December"] in
let month_str = List.nth months_str (month - 1) in
Printf.sprintf "%s %d%s, %4d" month_str day (getDayPostfix day) year
;;
(* Test the date2str function with a few common and exceptional cases *)
let test_date2str d m y expect =
let str = (date2str d m y) in
Printf.printf "%d %d %d -> %s, (should be %s, yields %s)\n"
d m y (if str = expect then "success" else "failure") expect str
;;
test_date2str 1 1 2010 "January 1st, 2010";;
test_date2str 9 2 2010 "February 9th, 2010";;
test_date2str 3 3 2011 "March 3rd, 2011";;
test_date2str 22 12 2012 "December 22nd, 2012";;
(* Sum all digits of a natural number *)
let rec sum_digits n =
if n < 0 then raise (Failure "cannot sum digits in integers below 0") else
let str = string_of_int n in
let l = String.length str in
if l = 1 then
int_of_string str
else
int_of_char str.[0] - 48
+ sum_digits (int_of_string (String.sub str 1 (l - 1)))
;;
(* Get the digital root of a natural number n *)
let rec digitRoot n =
let sum = sum_digits n in
if (String.length (string_of_int sum) = 1) then sum else (digitRoot sum)
;;
(* Test the digitRoot function for a few known solutions *)
let test_digitRoot n expect =
let root = digitRoot n in
Printf.printf "%d -> %s, (should be %d, yields %d)\n"
n (if root = expect then "success" else "failure") expect root
;;
test_digitRoot 123 6;;
test_digitRoot 65536 7;;
(* Check if a given string is a palindrome *)
let rec isPalindrome str =
let l = (String.length str) in
l < 2 ||
(str.[0] = str.[l - 1] && (isPalindrome (String.sub str 1 (l - 2))))
;;
(* Test the isPalindrome function with a few known palindromes *)
let test_isPalindrome str expect =
let result = (isPalindrome str) in
Printf.printf "%s -> %s, (should be %b, yields %b)\n"
str (if result = expect then "success" else "failure") expect result
;;
test_isPalindrome "foo" false;; (* non-palindrome of odd length *)
test_isPalindrome "foobar" false;; (* non-palindrome of even length *)
test_isPalindrome "lepel" true;; (* palindrome of odd length *)
test_isPalindrome "foof" true;; (* palindrome of even length *)
test_isPalindrome "" true;; (* The empty string is a palindrome *)
\documentclass[10pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,hyperref,graphicx,booktabs,float,listings}
% Paragraph indentation
\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}
\title{Functional Languages - Assignment series 2}
\author{Tadde\"us Kroes (6054129)}
\begin{document}
\maketitle
\section{Assignment 4}
Assignment: \emph{Define the Boolean functions negation, disjunction and
conjunction as $\lambda$-terms.}
\newcommand{\s}{\hspace{2mm}}
\newcommand{\true}{\lambda a.\lambda b.a}
\newcommand{\truea}{\lambda u.\lambda v.u}
\newcommand{\false}{\lambda a.\lambda b.b}
\newcommand{\falsea}{\lambda u.\lambda v.v}
\subsection{Negation}
Negation flips the value of a boolean (TRUE becomes FALSE and vice versa).
Consider $\neg = \lambda b.\lambda x.\lambda y.((b \s y) \s x)$.
Given that $TRUE = \true$ and $FALSE = \false$, we can make the following
derivations:
\begin{table}[H]
\begin{tabular}{rl}
$\neg TRUE$ & $= (\lambda b.\lambda x.\lambda y.((b \s y) \s x) \s \true)$ \\
& $\rightarrow_{\beta} \lambda x.\lambda y.((\true \s y) \s x)$ \\
& $\rightarrow_{\beta} \lambda x.\lambda y.(\lambda b.y \s x)$ \\
& $\rightarrow_{\beta} \lambda x.\lambda y.y$ \\
& $\equiv_{\alpha} FALSE$ \\
& \\
$\neg FALSE$ & $= (\lambda b.\lambda x.\lambda y.((b \s y) \s x) \s \false)$ \\
& $\rightarrow_{\beta} \lambda x.\lambda y.((\false \s y) \s x)$ \\
& $\rightarrow_{\beta} \lambda x.\lambda y.(\lambda b.b \s x)$ \\
& $\rightarrow_{\beta} \lambda x.\lambda y.x$ \\
& $\equiv_{\alpha} TRUE$ \\
\end{tabular}
\end{table}
TRUE and FALSE are both flipped, so the function $\neg$ is correct.
\pagebreak
\subsection{Disjunction}
The disjunction function $\vee$ should have the following properties: \\
TRUE $\vee$ TRUE = TRUE \\
TRUE $\vee$ FALSE = TRUE \\
FALSE $\vee$ TRUE = TRUE \\
FALSE $\vee$ FALSE = FALSE
Consider $\vee = \lambda p.\lambda q.((p \s p) \s q)$. Given the functions for
TRUE and FALSE, the combination of the following derivations proves that the
function $\vee$ is correct:
\begin{table}[H]
\begin{tabular}{rl}
$TRUE \s\vee\s TRUE$ & $= (\lambda p.(\lambda q.((p \s p) \s q) \s \true) \s \true)$ \\
& $\rightarrow_{\beta} (\lambda q.((\true \s \true) \s q) \s \true)$ \\
& $\rightarrow_{\beta} ((\true \s \true) \s \true)$ \\
& $\rightarrow_{\alpha,\beta} (\lambda b.\truea \s \true)$ \\
& $\rightarrow_{\beta} \truea$ \\
& $\equiv_{\alpha} TRUE$ \\
& \\
$TRUE \s\vee\s FALSE$ & $= (\lambda p.(\lambda q.((p \s p) \s q) \s \false) \s \true)$ \\
& $\rightarrow_{\beta} (\lambda q.((\true \s \true) \s q) \s \false)$ \\
& $\rightarrow_{\beta} ((\true \s \true) \s \false)$ \\
& $\rightarrow_{\alpha,\beta} (\lambda b.\truea \s \false)$ \\
& $\rightarrow_{\beta} \truea$ \\
& $\equiv_{\alpha} TRUE$ \\
& \\
$FALSE \s\vee\s TRUE$ & $= (\lambda p.(\lambda q.((p \s p) \s q) \s \true) \s \false)$ \\
& $\rightarrow_{\beta} (\lambda q.((\false \s \false) \s q) \s \true)$ \\
& $\rightarrow_{\beta} ((\false \s \false) \s \true)$ \\
& $\rightarrow_{\beta} (\lambda b.b \s \true)$ \\
& $\rightarrow_{\beta} \true$ \\
& $\equiv_{\alpha} TRUE$ \\
& \\
$FALSE \s\vee\s FALSE$ & $= (\lambda p.(\lambda q.((p \s p) \s q) \s \false) \s \false)$ \\
& $\rightarrow_{\beta} (\lambda q.((\false \s \false) \s q) \s \false)$ \\
& $\rightarrow_{\beta} ((\false \s \false) \s \false)$ \\
& $\rightarrow_{\beta} (\lambda b.b \s \false)$ \\
& $\rightarrow_{\beta} \false$ \\
& $\equiv_{\alpha} FALSE$ \\
\end{tabular}
\end{table}
\pagebreak
\subsection{Conjunction}
The disjunction function $\wedge$ should have the following properties: \\
TRUE $\wedge$ TRUE = TRUE \\
TRUE $\wedge$ FALSE = FALSE \\
FALSE $\wedge$ TRUE = FALSE \\
FALSE $\wedge$ FALSE = FALSE
Consider $\wedge = \lambda p.\lambda q.((p \s q) \s p)$. Given the functions for
TRUE and FALSE, the combination of the following derivations proves that the
function $\wedge$ is correct:
\begin{table}[H]
\begin{tabular}{rl}
$TRUE \s\wedge\s TRUE$ & $= (\lambda p.(\lambda q.((p \s q) \s p) \s \true) \s \true)$ \\
& $\rightarrow_{\beta} (\lambda q.((\true \s q) \s \true) \s \true)$ \\
& $\rightarrow_{\beta} ((\true \s \true) \s \true)$ \\
& $\rightarrow_{\alpha,\beta} (\lambda b.\truea \s \true)$ \\
& $\rightarrow_{\beta} \truea$ \\
& $\equiv_{\alpha} TRUE$ \\
& \\
$TRUE \s\wedge\s FALSE$ & $= (\lambda p.(\lambda q.((p \s q) \s p) \s \false) \s \true)$ \\
& $\rightarrow_{\beta} (\lambda q.((\true \s q) \s \true) \s \false)$ \\
& $\rightarrow_{\beta} ((\true \s \false) \s \true)$ \\
& $\rightarrow_{\alpha,\beta} (\lambda b.\falsea \s \true)$ \\
& $\rightarrow_{\beta} \falsea$ \\
& $\equiv_{\alpha} FALSE$ \\
& \\
$FALSE \s\wedge\s TRUE$ & $= (\lambda p.(\lambda q.((p \s q) \s p) \s \true) \s \false)$ \\
& $\rightarrow_{\beta} (\lambda q.((\false \s q) \s \false) \s \true)$ \\
& $\rightarrow_{\beta} ((\false \s \true) \s \false)$ \\
& $\rightarrow_{\beta} (\lambda b.b \s \false)$ \\
& $\rightarrow_{\beta} \false$ \\
& $\equiv_{\alpha} FALSE$ \\
& \\
$FALSE \s\wedge\s FALSE$ & $= (\lambda p.(\lambda q.((p \s q) \s p) \s \false) \s \false)$ \\
& $\rightarrow_{\beta} (\lambda q.((\false \s q) \s \false) \s \false)$ \\
& $\rightarrow_{\beta} ((\false \s \false) \s \false)$ \\
& $\rightarrow_{\beta} (\lambda b.b \s \false)$ \\
& $\rightarrow_{\beta} \false$ \\
& $\equiv_{\alpha} FALSE$ \\
\end{tabular}
\end{table}
\section{Assignment 5}
See appendix \ref{appendix:ass5} (file \emph{ass5.ml}) for my implementation
of the functions \texttt{isLeapYear}, \texttt{date2str}, \texttt{digitRoot}
and \texttt{isPalindrome}.
\pagebreak
\appendix
\section{ass5.ml}
\label{appendix:ass5}
\lstinputlisting{ass5.ml}
\end{document}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment