<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">Op 11-8-2015 om 5:50 schreef Richard A.
O'Keefe:<br>
</div>
<blockquote
cite="mid:98778534-09CB-4D17-B6A7-44E48BDA22C9@cs.otago.ac.nz"
type="cite">
<pre wrap="">
On 11/08/2015, at 3:46 am, Roelof Wobben <a class="moz-txt-link-rfc2396E" href="mailto:r.wobben@home.nl"><r.wobben@home.nl></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">As a working programm I have wrote this :
-module(dates).
-export([date_parts/1]).
date_parts(Date)->
[Year, Month, Day] = re:split(Date, "-", [{return, list}] ),
[element(1, string:to_integer(Year)), element(1,string:to_integer(Month)), element(1,string:to_integer(Day))].
</pre>
</blockquote>
<pre wrap="">
There are two things about this that I don't like.
One is the repeated element(1, string:to_integer(___)).
As you probably know,
> string:to_integer("roelof").
{error,no_integer}
so the element(1,...) thing might return 'error', not an integer.
It would be better to use list_to_integer/1 so that you either
get an integer or an exception.
The other is that I'd rather the list_to_integer/1 function
_didn't_ raise an exception. You're using regular expressions.
So why not check for digits?
So I'd be looking for a match to
"^([1-9][0-9][0-9][0-9])-([1-9][0-9]?)-([1-9][0-9]?)$"
so that once the match succeeds you *know* there are
vaguely plausible integers there.
I'm also unhappy that the date is not validated, so that
with your code, "0-0-0" and "42-137-999999" are accepted.
There are at least two kinds of beauty for code:
- INTERFACE beauty
- IMPLEMENTATION beauty.
For me, interface beauty means that the function either
gives me clean answers I don't have to worry about any
further or crashes with a useful report, and there are
no nasty glitches I have to worry about.
There is a glitch in your function.
My reading of ISO 8601 is that -0054-8-8 should be a
valid date. (Using the "retrospective" or "proleptic"
Gregorian calendar.) But your function will reject it.
If that is what you WANT, that's fine, but you should SAY.
A comment like
% This accepts YYYY-MM-DD dates for 1900 < YYYY < 2100.
would do.
For the purposes of interface beauty, to the *user* of
this function there is no real difference between a
date that is invalid because it looks like "1-2-3-4"
and a date that is invalid because it looks like
"2015-02-29". So interface beauty would seem to require
% is_valid(Y, M, D) is true when 1900 < Y < 2100 and
% Y-M-D is a valid date.
is_valid(Y, M, D) when
is_integer(Y), 1900 < Y, Y < 2100,
is_integer(M), 0 < M, M < 13,
is_integer(D), 0 < D, D < 32
-> ... in this range, is_leap_year(Y) <=> Y mod 4 = 0
</pre>
</blockquote>
<br>
<br>
Thanks, <br>
<br>
Error checking is not needed here.<br>
<br>
Here is the text of the etude I try to solve :<br>
<br>
<p id="write_a_module__id2">Write a module named <code
class="literal">dates</code> that contains a function <code
class="literal">date_parts/1</code>, which takes a string in ISO
date format (<code class="literal">"yyyy-mm-dd"</code>) and
returns a list of integers in the form <code class="literal">[yyyy,
mm, dd]</code>. This function does not need to do any error
checking.</p>
<p id="youll_use_the_">You’ll use the <code class="literal">re:split/3</code>
function from Erlang’s regular expression (<code class="literal">re</code>)
module to accomplish the task. How, you may ask, does that
function work? Ask Erlang! The command <code class="literal">erl
-man re</code> will give you the online documentation for the <code
class="literal">re</code> module.<br>
</p>
<p>Roelof<br>
<br>
</p>
<br>
<br /><br />
<hr style='border:none; color:#909090; background-color:#B0B0B0; height: 1px; width: 99%;' />
<table style='border-collapse:collapse;border:none;'>
<tr>
<td style='border:none;padding:0px 15px 0px 8px'>
<a href="https://www.avast.com/antivirus">
<img border=0 src="http://static.avast.com/emails/avast-mail-stamp.png" alt="Avast logo" />
</a>
</td>
<td>
<p style='color:#3d4d5a; font-family:"Calibri","Verdana","Arial","Helvetica"; font-size:12pt;'>
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
<br><a href="https://www.avast.com/antivirus">www.avast.com</a>
</p>
</td>
</tr>
</table>
<br />
</body>
</html>