Re: [Chicken-hackers] substring function and bounds checks

2013-02-08 Thread Michele La Monaca
On Fri, Feb 8, 2013 at 8:41 AM, Alex Shinn alexsh...@gmail.com wrote:
 On Fri, Feb 8, 2013 at 4:21 PM, Michele La Monaca
 mikele.chic...@lamonaca.net wrote:

 On Fri, Feb 8, 2013 at 5:43 AM, Alex Shinn alexsh...@gmail.com wrote:
  operations.  The only example provided was fairly obscure and it was
  indeed a composition, even if you don't think it's worth separating them
  for logging or other handling.

 At the prompt of your shell type:

 # ls ?a_well_thought_out_substring_function_would_do_this_job*


 I'm not sure I understand, but it sounds like you're just mocking me?

Not at all! Sorry if I gave you that impression. Ok, there was a bit
of humor, but, well, take it easy. Rember Jim calling Sussman on the
phone only to be
redirected to voicemail? I think this kind of jokes are accettable in
a heated technical discussion and no one should get offended for
those.

Anyway, mine wasn't only a joke. I used to be a C programmer so I have
the bad habit to condense too much information in one line. Sorry for
that.  If you want I can expand on it and explain my case.
Unfortunately I think I've already abused enough this list, so feel
free to contact me privately if you want to continue this discussion
with me.

Regards,
Michele

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-08 Thread Felix
From: Alex Shinn alexsh...@gmail.com
Subject: Re: [Chicken-hackers] substring function and bounds checks
Date: Fri, 8 Feb 2013 10:33:33 +0900

 On Fri, Feb 8, 2013 at 10:14 AM, Michele La Monaca 
 mikele.chic...@lamonaca.net wrote:
 

 starting from a certain position give me chars up to the Nth position
 or up to the end of the string whatever the first

 
 Can you provide a real-world example where you'd
 want to use this function?

Ugh. Whenever a language/API/system design discussion goes into
this stage, it's best to stop.

Can't we just allow Michele to have his own opinion? Let's leave him
alone now, and let's start getting productive.


cheers,
felix


___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Michele La Monaca
On Thu, Feb 7, 2013 at 12:20 AM, Alex Shinn alexsh...@gmail.com wrote:
 Hi,

 On Thu, Feb 7, 2013 at 3:20 AM, Michele La Monaca
 mikele.chic...@lamonaca.net wrote:

 [..] I don't think

 (substring2 foo 0 10) - foo

 is conceptually wrong or sloppy as long as you know exactly what the
 semantic of the function is (give me at most N chars - perl or
 give me chars up to the Nth position or up to the end of the string
 whatever the first - python, ruby).


 Truncating a string to a maximum length is a common
 operation for formatting - I was surprised there was no
 easy way to do this with SRFI-13.  But in this case the
 start argument isn't needed and distracting.  You probably
 want something like:

Well, no. If I wanted a truncate function I would have asked for it. I
used 0 as START to simplify the exposition (in that case the END
parameter can be interpreted as length or position indifferently).
Anyway, thanks for raising the point. Maybe the reason there is no
string-truncate around is that a sane substring function would just do
the job?

Regards,
Michele


   (string-truncate foo 10) = foo
   (string-truncate foofoofoofoo 10) = foofoofoof

 In the fmt egg:

   (fmt #f (trim 10 foo)) = foo

 Conflating this with substring is confusing and hides
 potential errors, as others have pointed out.

 --
 Alex


___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Michele La Monaca
On Wed, Feb 6, 2013 at 12:47 AM, Felix
fe...@call-with-current-continuation.org wrote:
 # perl -e 'print substr(ciao,0,10);'
 ciao
 # ruby -e 'puts ciao[0..10]'
 ciao
 # python -c 'print ciao[0:10];'
 ciao
 # csi -e '(print (substring ciao 0 10))'
 Error: (substring) out of range 0 10

 Call history:

 syntax  (print (substring ciao 0 10))
 syntax  (substring ciao 0 10)
 eval(print (substring ciao 0 10))
 eval(substring ciao 0 10)   --


 The string is shorter than the limit you gave to substring
 in the third argument. Of course you know that, but why did
 you pass an incorrect length in the first place?

 This looks like it does what you want:

 csi -e '(print (substring ciao 0))'

Well, no. I was looking for a python-like substring function. Anyway,
thanks for the (substring ciao 0) trick, it is a very handy method
to trim strings on the left (not documented in the wiki, btw):

(substring ciao 1) = iao

In my ideal world, there would room also for that:

(substring ciao 0 -1) = cia

but I know, I know... it will never happen :-(

Regards,
Michele

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread John Cowan
Michele La Monaca scripsit:

 Well, no. I was looking for a python-like substring function. 

If that's all you really wanted, the slice egg (to which you have already
been referred; docs at http://wiki.call-cc.org/eggref/4/slice) does
everything you are asking for, negative and past-the-end indices and all.
It even slices lists and vectors as a bonus.  But no, what you are
showing you actually want is to complain about the meaning of the name
substring.  That's not a discussion anyone else thinks is worth having.

-- 
You are a child of the universe no less John Cowan
than the trees and all other acyclichttp://www.ccil.org/~cowan
graphs; you have a right to be here.co...@ccil.org
  --DeXiderata by Sean McGrath

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Alex Shinn
On Fri, Feb 8, 2013 at 2:23 AM, Michele La Monaca 
mikele.chic...@lamonaca.net wrote:

 On Thu, Feb 7, 2013 at 12:20 AM, Alex Shinn alexsh...@gmail.com wrote:
  Hi,
 
  On Thu, Feb 7, 2013 at 3:20 AM, Michele La Monaca
  mikele.chic...@lamonaca.net wrote:
 
  [..] I don't think
 
  (substring2 foo 0 10) - foo
 
  is conceptually wrong or sloppy as long as you know exactly what the
  semantic of the function is (give me at most N chars - perl or
  give me chars up to the Nth position or up to the end of the string
  whatever the first - python, ruby).
 
 
  Truncating a string to a maximum length is a common
  operation for formatting - I was surprised there was no
  easy way to do this with SRFI-13.  But in this case the
  start argument isn't needed and distracting.  You probably
  want something like:

 Well, no. If I wanted a truncate function I would have asked for it. I
 used 0 as START to simplify the exposition (in that case the END
 parameter can be interpreted as length or position indifferently).


You didn't use it just to simplify, because the task
you described in English was give me at most N chars.
This is a common task, and always has a START of 0.

There may be cases in Python where you first want to
take the substring from START to the end of the string,
and then want to truncate the result to END-START chars.
This becomes an idiom in Python, so it seems natural to
you, but there is always a specific reason you want to
truncate separate from the substring you are taking.

In Scheme this becomes

  (string-truncate (string-copy str start) max-len)

You could combine these two functions

  (slice str start (- max-len start))

but this hides what you're actually doing and likely not as
common, so is probably a bad idiom in Scheme.  It doesn't
make sense to provide such artificially combined functions.
For example, you may notice sometimes you want to take
a substring and then lowercase the result, but that doesn't
mean it's worth defining a substring-downcase function.

To add to the earlier rants abouts the dangers of DWIM
behavior, I had a nasty bug in one of my Python programs
the other day.  We have an API for tasks which produce
file outputs, and for convenience if there's only a single
output we just return that, otherwise we return a tuple of
strings.  I had a task where I wanted to copy the first
result to a separate directory, and this was a task that
returned a tuple, so I used outputs[0].  Unfortunately, after
some refactoring this became a task which only returned
one output, so outputs became a string and outputs[0] was
/!  The program was still perfectly valid, and gave me the
command-line:

  cp -R / /path/to/destination

Fortunately we have lots of extra checks and I caught this
before ever executing it, but it was scary to even just see
that.

-- 
Alex
___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Michele La Monaca
How strange, I thought no one was interested in the discussion.
Anyway, with John's permission just a few inline clarifications.

On Thu, Feb 7, 2013 at 11:59 PM, Alex Shinn alexsh...@gmail.com wrote:

 [...]
 You didn't use it just to simplify, because the task
 you described in English was give me at most N chars.

http://lists.nongnu.org/archive/html/chicken-hackers/2013-02/msg00058.html

give me at most N chars starting from a certain position in the string

Ok, in another mail I gave a shortened version but, well, I think the
context was clear.

 This is a common task, and always has a START of 0.

What? what? what?

 There may be cases in Python where you first want to
 take the substring from START to the end of the string,
 and then want to truncate the result to END-START chars.
 This becomes an idiom in Python, so it seems natural to

Sorry, never used Python in my life. I used it as an example because I
think it's a commonly known language. Not by me, unfortunately, so I
don't believe I completely understand what you are trying to say.

 you, but there is always a specific reason you want to
 truncate separate from the substring you are taking.

 In Scheme this becomes

   (string-truncate (string-copy str start) max-len)

 You could combine these two functions

   (slice str start (- max-len start))

 but this hides what you're actually doing

Don't think so. Really don't think so.

 and likely not as
 common, so is probably a bad idiom in Scheme.  It doesn't
 make sense to provide such artificially combined functions.

Again (last time I promise), I don't think that:

starting from a certain position give me at most N chars

or

starting from a certain position give me chars up to the Nth position
or up to the end of the string whatever the first

are artificially combined functions. They are exactly what they mean.

Anyway, I think I had enough of that. I respect your opinions, please
try to respect mine and let's stop this discussion.

Regards,
Michele

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Alex Shinn
On Fri, Feb 8, 2013 at 10:14 AM, Michele La Monaca 
mikele.chic...@lamonaca.net wrote:


 starting from a certain position give me chars up to the Nth position
 or up to the end of the string whatever the first


Can you provide a real-world example where you'd
want to use this function?

-- 
Alex
___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread John Cowan
Alex Shinn scripsit:

 Can you provide a real-world example where you'd want to use this
 function?

I used to use it all the time in Perl, where it is the normal behavior
of the substr() function, to pull a fixed-width field out of a line of
text without having to worry about short lines.  If the field was at the
end of the line, the result was short; if the field was not present, the
result was the empty string.  In some contexts, I would then concatenate
spaces to it and truncate the result to the desired width.

-- 
Barry thirteen gules and argent on a canton azure   John Cowan
fifty mullets of five points of the second, co...@ccil.org
six, five, six, five, six, five, six, five, and six.
--blazoning the U.S. flag   http://www.ccil.org/~cowan

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Alex Shinn
On Fri, Feb 8, 2013 at 10:45 AM, John Cowan co...@mercury.ccil.org wrote:

 Alex Shinn scripsit:

  Can you provide a real-world example where you'd want to use this
  function?

 I used to use it all the time in Perl, where it is the normal behavior
 of the substr() function, to pull a fixed-width field out of a line of
 text without having to worry about short lines.  If the field was at the
 end of the line, the result was short;


This is where you have a contradiction - the field is fixed width
but the full width is not available, which means you have an invalid
format.

Could you be more specific in your example?  If the goal is to
extract the last field, then you use substr with no end param.
If the goal is to take a fixed field you should be taking that.  If
the format allows either then the format itself is ambiguous, which
means you have a _much_ bigger problem than whatever language
idioms you use.

-- 
Alex
___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread John Cowan
Alex Shinn scripsit:

 This is where you have a contradiction - the field is fixed width
 but the full width is not available, which means you have an invalid
 format.

Yes, in one sense.  However, when transferring textual data between
heterogeneous systems it is very common for trailing spaces to be
discarded willy-nilly, and the approach described recovers from that
nicely.  Be liberal in what you accept.

 If the goal is to take a fixed field you should be taking that.  If
 the format allows either then the format itself is ambiguous, which
 means you have a _much_ bigger problem than whatever language
 idioms you use.

Purism has its place, but is not always the appropriate response
to Real World issues.  In this case, kicking back such records
as invalid/ambiguous format simply would not have served the
organization's goals.

-- 
In my last lifetime,John Cowan
I believed in reincarnation;http://www.ccil.org/~cowan
in this lifetime,   co...@ccil.org
I don't.  --Thiagi

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Alex Shinn
On Fri, Feb 8, 2013 at 11:50 AM, John Cowan co...@mercury.ccil.org wrote:

 Alex Shinn scripsit:

  If the goal is to take a fixed field you should be taking that.  If
  the format allows either then the format itself is ambiguous, which
  means you have a _much_ bigger problem than whatever language
  idioms you use.

 Purism has its place, but is not always the appropriate response
 to Real World issues.  In this case, kicking back such records
 as invalid/ambiguous format simply would not have served the
 organization's goals.


I was not suggesting kicking it back.  I was suggesting if it
was the last field then just use substr($line, $n).  That sounds
like the definition of the format you're describing, though you're
not giving a concrete example so I can't be sure.

If there are additional restraints on this field then you should
handle them as well, and if you want to handle those restraints
liberally you can do so.  For example, if the length of the last
field should be no longer than $limit, you can check this:

  if (len($field)  $limit) {
print STDERR found trailing junk at end of line: $field;
$field = substr($field, 0, $limit);
  }

The warning is important, and this again emphasizes that there
are two _unrelated_ functions - extraction and truncation - and
that combining them is a bad idea.

-- 
Alex
___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread John Cowan
Alex Shinn scripsit:

 The warning is important, and this again emphasizes that there
 are two _unrelated_ functions - extraction and truncation - and
 that combining them is a bad idea.

The warning is important -- when it's important.  In my case, it was
important to avoid cluttering the exception log with spurious complaints
about the results of a process outside anyone's control.

In any case, that has zero to do with whether it makes sense to package
this behavior as a procedural abstraction.  Please note that I am *not*
arguing that Chicken's substring should behave this way, just that a
convenient implementation of the loose behavior does have use cases.

-- 
The Imperials are decadent, 300 pound   John Cowan co...@ccil.org
free-range chickens (except they have   http://www.ccil.org/~cowan
teeth, arms instead of wings, and
dinosaurlike tails).--Elyse Grasso

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Alex Shinn
On Fri, Feb 8, 2013 at 12:26 PM, John Cowan co...@mercury.ccil.org wrote:

 Alex Shinn scripsit:

  The warning is important, and this again emphasizes that there
  are two _unrelated_ functions - extraction and truncation - and
  that combining them is a bad idea.

 The warning is important -- when it's important.  In my case, it was
 important to avoid cluttering the exception log with spurious complaints
 about the results of a process outside anyone's control.


Then comment out the logging, or use a verbosity level.  The most
important thing with a hacky language and hacky formats is to be precise
about your parsing.

In any case, that has zero to do with whether it makes sense to package
 this behavior as a procedural abstraction.  Please note that I am *not*
 arguing that Chicken's substring should behave this way, just that a
 convenient implementation of the loose behavior does have use cases.


I think none of the Schemers are arguing that substring should be changed.

I'm arguing further that the slice function is an artifact of languages that
have slicing as syntax, but here is actually a confused composition of two
operations.  The only example provided was fairly obscure and it was
indeed a composition, even if you don't think it's worth separating them
for logging or other handling.

-- 
Alex
___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-07 Thread Michele La Monaca
On Fri, Feb 8, 2013 at 5:43 AM, Alex Shinn alexsh...@gmail.com wrote:
 operations.  The only example provided was fairly obscure and it was
 indeed a composition, even if you don't think it's worth separating them
 for logging or other handling.

At the prompt of your shell type:

# ls ?a_well_thought_out_substring_function_would_do_this_job*

Regards,
Michele

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-06 Thread Michele La Monaca
Hi Peter,

wow! What a long and articulate response! Thanks for that, I really
appreciate the time and effort you put in it. I also understand (and
appreciate again) your attempt to inculcate in me a different way of
thinking, but unfortunately, at least for now, my antibodies are at
work. Maybe one day... for now I am just comfortable with my few
little beliefs. Excuse me if I have been somehow rude but, you know,
we both have strong ideas.

Today is my boring gym day, so I have to condense my reply somehow
and, anyway, I don't want to repeat myself over and over. As I said:

(substring1 foo 0 10) - bump
(substring2 foo 0 10) - foo

are both legitimate scheme substring functions from what I can tell.
You say the first is the non-sloppy, safer one, I just say they
have a different semantic and I do notice that we have a rather
different concept of safety. Indeed, I don't think

(substring2 foo 0 10) - foo

is conceptually wrong or sloppy as long as you know exactly what the
semantic of the function is (give me at most N chars - perl or
give me chars up to the Nth position or up to the end of the string
whatever the first - python, ruby).

Regarding the practical aspects of the issue, I hope you might agree
with me that substring2 if far superior to substring1. Having to catch
exceptions, or having to write a wrapper function, as Jim suggested,
to achieve a minimal sane usage of substring1 (and hey we are not
talking about the apollo11-land-on-the-moon-button function), should
give you a sense of the inadequacy of the substring function currently
shipped with chicken.

Anyway, as you suggested, maybe it is just too early for me... let's
see. (I hope) I am the kind of person which periodically reconsider
without preconceptions his beliefs and principles, though, admittedly,
I am rather defensive at the beginning.

Thanks to the community for all the kind responses.

Regards,
Michele


On Wed, Feb 6, 2013 at 10:21 AM, Peter Bex peter@xs4all.nl wrote:
 On Wed, Feb 06, 2013 at 12:55:16AM +0100, Michele La Monaca wrote:
 What I can say... Well, maybe one day I will see the light, in the
 meanwhile I would just have preferred a more useful substring
 function. I really think that the one provided by chicken is simply
 not on par with other languages, sorry.

 This is not a question of being on par or not.  Some other languages
 choose to have nonsensical requests like give me characters 0
 through 10 from this 3-character string to return the original string.

 Substring is a silly and trivial example but it clearly illustrates the
 deep fundamental differences in philosophy between the different
 cultures of these languages.

 The most valuable gift a computer programming language can give you is
 the ability to reason about a piece of code without extra information.
 For example, if you see (substring s 0 10) you immediately *know* in
 any code that follows:

 - The variable s is a string
 - The string s is at least 10 characters long
 - The returned value is a string
 - The returned string is exactly 10 characters long

 If none of the above are true, you'll have an error situation and
 the code following the substring call will not be executed.
 In sloppy languages, you lose several of these important footholds,
 which means you can't reason so well about your code's correctness
 anymore, except by reading back and dragging in more context.

 The above guarantees mean, for example, that if later you see
 (string-ref s 8) this will *always* return a character.  In your
 other languages, you won't know what exactly it'll do.  You'll have two
 lines of possible future traces through your program: one where there
 was a character and one where there wasn't.  Multiply this by every
 sloppy operator you use and you end up with a tangled web of possible
 futures.  Often, only one of those futures is what you had in mind when
 writing code.  The other possibilities produce wrong computations, which
 may result in data corruption or security problems.

 Sloppiness promotes muddled thinking and imprecise code.  Imprecise
 code is prone to bugs, which may lead to security problems.  This may
 not be fair, but I think a particularly good example is the 2 recent
 extremely dangerous vulnerabilities in Ruby on Rails which allowed for
 remote code execution.

 What caused these problems to happen?  Misplaced convenience and
 sloppiness: Rails allowed Yaml data to be embedded in XML and JSON,
 which automatically got parsed.  Of course, XML and JSON are their own
 formats and their specifications don't mention anything about Yaml.
 This means a Yaml parser has nothing, and I mean *nothing* to do in
 an XML parser.  It may be convenient in a small set of cases, but
 it's just another case of adding more magic: it's not something you
 asked for, so it shouldn't happen.

 For another good example of the deep confusion caused by code that
 is sloppy by design, read https://bugs.php.net/bug.php?id=54547
 Read it, and also 

Re: [Chicken-hackers] substring function and bounds checks

2013-02-06 Thread John Cowan
Michele La Monaca scripsit:

 Regarding the practical aspects of the issue, I hope you might agree
 with me that substring2 if far superior to substring1. 

All you're arguing now is that it's substring2 that deserves the name
substring, since the slice function already does what you want.
That's plain silly.  Go ahead and use slice if you think it's more
practical.

 give you a sense of the inadequacy of the substring function currently
 shipped with chicken.

It's inadequate to your purposes, but it's adequate to other people's
purposes, and vice versa for the function you want.

-- 
Take two turkeys, one goose, four   John Cowan
cabbages, but no duck, and mix them http://www.ccil.org/~cowan
together. After one taste, you'll duck  co...@ccil.org
soup the rest of your life.
--Groucho

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-06 Thread Alex Shinn
Hi,

On Thu, Feb 7, 2013 at 3:20 AM, Michele La Monaca 
mikele.chic...@lamonaca.net wrote:

 [..] I don't think

 (substring2 foo 0 10) - foo

 is conceptually wrong or sloppy as long as you know exactly what the
 semantic of the function is (give me at most N chars - perl or
 give me chars up to the Nth position or up to the end of the string
 whatever the first - python, ruby).


Truncating a string to a maximum length is a common
operation for formatting - I was surprised there was no
easy way to do this with SRFI-13.  But in this case the
start argument isn't needed and distracting.  You probably
want something like:

  (string-truncate foo 10) = foo
  (string-truncate foofoofoofoo 10) = foofoofoof

In the fmt egg:

  (fmt #f (trim 10 foo)) = foo

Conflating this with substring is confusing and hides
potential errors, as others have pointed out.

-- 
Alex
___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


[Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Michele La Monaca
Maybe this is not the right list for that, sorry. But is there a good
reason for this behavior?

# perl -e 'print substr(ciao,0,10);'
ciao
# ruby -e 'puts ciao[0..10]'
ciao
# python -c 'print ciao[0:10];'
ciao
# csi -e '(print (substring ciao 0 10))'
Error: (substring) out of range 0 10

Call history:

syntax  (print (substring ciao 0 10))
syntax  (substring ciao 0 10)
eval(print (substring ciao 0 10))
eval(substring ciao 0 10)   --


I find it rather inconvenient and I can't really see any good reason
to do that.  In fact, it makes the usage of substring unsafe and the
countermeasures I can imagine (manual bounds checks, padding, writing
my-own-substring-function, whatever) are quite unsatisfactory... to me
at least.

Regards,
Michele

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Peter Bex
On Tue, Feb 05, 2013 at 11:11:51PM +0100, Michele La Monaca wrote:
 Maybe this is not the right list for that, sorry. But is there a good
 reason for this behavior?

Yes, sanity.

 # perl -e 'print substr(ciao,0,10);'
 ciao

Perl is extremely sloppy.

 # ruby -e 'puts ciao[0..10]'
 ciao

Ruby is just Perl in OOP clothing.

 # python -c 'print ciao[0:10];'
 ciao

Python should know better.

 # csi -e '(print (substring ciao 0 10))'
 Error: (substring) out of range 0 10
 
 Call history:
 
 syntax  (print (substring ciao 0 10))
 syntax  (substring ciao 0 10)
 eval(print (substring ciao 0 10))
 eval(substring ciao 0 10)   --
 
 
 I find it rather inconvenient and I can't really see any good reason
 to do that.  In fact, it makes the usage of substring unsafe and the
 countermeasures I can imagine (manual bounds checks, padding, writing
 my-own-substring-function, whatever) are quite unsatisfactory... to me
 at least.

If you prefer braindead bug-ridden do what I mean-ish behaviour of
other, more sloppy languages, use PHP.

Scheme is about correctness.  If you provide invalid indices, you get
errors.  This will help you detect bugs early on instead of just keep
going on with a bad result of an incorrect computation until some other
thing fails much farther along.  This kind of thing also tends to sneak
in vulnerabilities, as you never *really* know what your code will do
in the face of inconsistencies.  fail early and noisily is good design.

More seriously, if you think you know what you're doing and really
want this kind of broken behaviour, we have an egg that emulates this
kind of nonsense: http://wiki.call-cc.org/eggref/4/slice

#;1 (use slice)
#;2 (slice foo 0 10)
foo

Cheers,
Peter
-- 
http://sjamaan.ath.cx

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Moritz Heidkamp
Hi Michele,

Michele La Monaca mikele.chic...@lamonaca.net writes:

 Maybe this is not the right list for that, sorry.

yeah, this one probably better fits chicken-users as it's not really
about Chicken's implementation, so you might be losing some broader
audience by posting it here. No problemo, though!


 In fact, it makes the usage of substring unsafe and the
 countermeasures I can imagine (manual bounds checks, padding, writing
 my-own-substring-function, whatever) are quite unsatisfactory... to me
 at least.

I felt the same when coming to Scheme (from Ruby in my case). But after
having been immersed in the langauge and community surrounding it for a
while now I came to see it in a different light: What might look like
impracticality at the beginning turns out to be a virtue in its own
right. All languages you cite (well, apart from Python perhaps, as Peter
noted) are from the DWIM-most-of-the-time spectrum which give a lot of
leeway. This often leads to what Olin Shivers vividly describes as 80%
solutions in the preamble of the SRE announcement (see
http://www.scsh.net/docu/post/sre.html). I recommend you to read that
one if you don't already know it. Scheme as a whole tries to aim for the
100% solutions (whether it's successful in that endeavor is open for
debate, of course). That's why you'll often see functions reduced to
their bare minimum purpose and correct behavior even in edge cases. This
is especially true for functions defined in the standard core such as
the one you are referring to. The nice thing is that from that solid
core as a foundation we can build libraries which allow us to be a bit
more sloppy when we want to whereas it's much harder to go the other way
around.

It may sound a bit like just drink the Kool-Aid but what I'm trying to
say is: Scheme, as any language, has its own culture and you will be
much happier when you try to embrace it rather than trying to impose
concepts from other languages on it. But you probably already know this,
so I hope my comment on the specific case of your substring issue is of
help, anyway :-)


Moritz

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Felix
 # perl -e 'print substr(ciao,0,10);'
 ciao
 # ruby -e 'puts ciao[0..10]'
 ciao
 # python -c 'print ciao[0:10];'
 ciao
 # csi -e '(print (substring ciao 0 10))'
 Error: (substring) out of range 0 10
 
 Call history:
 
 syntax  (print (substring ciao 0 10))
 syntax  (substring ciao 0 10)
 eval(print (substring ciao 0 10))
 eval(substring ciao 0 10)   --
 

The string is shorter than the limit you gave to substring
in the third argument. Of course you know that, but why did
you pass an incorrect length in the first place? 

This looks like it does what you want:

csi -e '(print (substring ciao 0))'


cheers,
felix

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Michele La Monaca
On Wed, Feb 6, 2013 at 12:18 AM, Jim Ursetto zbignie...@gmail.com wrote:
 On Feb 5, 2013, at 4:11 PM, Michele La Monaca wrote:

 Maybe this is not the right list for that, sorry. But is there a good
 reason for this [error on out-of-bounds substring access] behavior?

 I think it's along the lines of (cdr '()) being an error in
 Scheme instead of NIL as in Lisp.

 Jim

Hi Peter, Morizt, Jim,

thanks for your answers.

What I can say... Well, maybe one day I will see the light, in the
meanwhile I would just have preferred a more useful substring
function. I really think that the one provided by chicken is simply
not on par with other languages, sorry.  The semantic of a
commonly-found substring function give me at most N chars starting
from a certain position in the string is the most useful according
me. I don't see any evil in that. The chicken (scheme?) alternative
give me exactly N chars or blow up is rather limited in scope to me.

 Scheme is about correctness.  If you provide invalid indices, you get errors.

Well, in the real world you can't always predict input and therefore
you must do checks.  Either you leave this burden to the user or you
kindly provide this even-cobol-has-it feature.

This will help you detect bugs early on instead of just keep going on with a
bad result of an incorrect computation until some other thing fails much
farther along.  This kind of thing also tends to sneak in vulnerabilities, as
you never *really* know what your code will do in the face of inconsistencies.
fail early and noisily is good design.

To me, this only means more bloated, unreadable code in the best case,
unfriendly crashes in the worst one.

Regards,
Michele

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread John Cowan
Peter Bex scripsit:

 Scheme is about correctness.  If you provide invalid indices, you get
 errors.  This will help you detect bugs early on instead of just keep
 going on with a bad result of an incorrect computation until some other
 thing fails much farther along.  This kind of thing also tends to sneak
 in vulnerabilities, as you never *really* know what your code will do in
 the face of inconsistencies.  fail early and noisily is good design.

In fact, R5RS and R7RS in no way require this kind of correctness.
(substring foo 0 10) is the equivalent of a domain error at the
value level, and both standards say that domain errors are up to the
implementation to handle as they see fit.  Chicken is definitely a safe
implementation as most are, but unsafe ones that return foo in this
case, or even crash, are entirely within the remit of the standards.
(R6RS is a different matter, but Chicken doesn't do R6RS, and even
implementations that do attempt compliance often aren't fully safe in
their error behavior.)

What Scheme is really about, if it has to be said to be about something,
is implementation choice.  Schemes provide and extend the language in
different ways, and you pick the one that provides the levels of speed,
safety, size, compilation speed, debuggability, or whatever other factors
you want.

-- 
Take two turkeys, one goose, four   John Cowan
cabbages, but no duck, and mix them http://www.ccil.org/~cowan
together. After one taste, you'll duck  co...@ccil.org
soup the rest of your life.
--Groucho

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Jim Ursetto
On Feb 5, 2013, at 5:55 PM, Michele La Monaca wrote:

 What I can say... Well, maybe one day I will see the light, in the
 meanwhile I would just have preferred a more useful substring
 function. I really think that the one provided by chicken is simply
 not on par with other languages, sorry.

I'll get Sussman on the phone.

 The semantic of a
 commonly-found substring function give me at most N chars starting
 from a certain position in the string is the most useful according
 me.

Well, your Perl example did that, but the Ruby, Python and Chicken
ones all give you chars between START and END, not N chars from START.
So you would have to write some shim code anyway.

So while you're in there, just add a couple 'min' expressions.

(define (substring/n s start n)
  (let* ((start (min start (string-length s)))
 (end (min (string-length s) (+ start n
(substring s start end)))

 To me, this only means more bloated, unreadable code in the best case,
 unfriendly crashes in the worst one.


 (substring/n ciao 1 10)
  iao

Presto, now your code isn't bloated, and you have up to N
chars from START.

Jim

P.S. He sent me to voicemail.



___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Jim Ursetto
On Feb 5, 2013, at 4:11 PM, Michele La Monaca wrote:

 Maybe this is not the right list for that, sorry. But is there a good
 reason for this [error on out-of-bounds substring access] behavior?

I think it's along the lines of (cdr '()) being an error in
Scheme instead of NIL as in Lisp.

Jim

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread John Cowan
Michele La Monaca scripsit:

 The funny thing looking at the implementation is that it actually does
 the check (so no speed) only to have a better opportunity to crash
 (so no safety).

Raising an exception is not crashing.  Crashing means that no recovery
is possible within the Scheme process, which is not the case here.

-- 
Evolutionary psychology is the theory   John Cowan
that men are nothing but horn-dogs, http://www.ccil.org/~cowan
and that women only want them for their money.  co...@ccil.org
--Susan McCarthy (adapted)

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread .alyn.post.
On Wed, Feb 06, 2013 at 12:10:09AM +0100, Moritz Heidkamp wrote:
 Hi Michele,
 
 Michele La Monaca mikele.chic...@lamonaca.net writes:
 
  Maybe this is not the right list for that, sorry.
 
 yeah, this one probably better fits chicken-users as it's not really
 about Chicken's implementation, so you might be losing some broader
 audience by posting it here. No problemo, though!
 
 
  In fact, it makes the usage of substring unsafe and the
  countermeasures I can imagine (manual bounds checks, padding, writing
  my-own-substring-function, whatever) are quite unsatisfactory... to me
  at least.
 
 I felt the same when coming to Scheme (from Ruby in my case). But after
 having been immersed in the langauge and community surrounding it for a
 while now I came to see it in a different light: What might look like
 impracticality at the beginning turns out to be a virtue in its own
 right. All languages you cite (well, apart from Python perhaps, as Peter
 noted) are from the DWIM-most-of-the-time spectrum which give a lot of
 leeway. This often leads to what Olin Shivers vividly describes as 80%
 solutions in the preamble of the SRE announcement (see
 http://www.scsh.net/docu/post/sre.html). I recommend you to read that
 one if you don't already know it. Scheme as a whole tries to aim for the
 100% solutions (whether it's successful in that endeavor is open for
 debate, of course). That's why you'll often see functions reduced to
 their bare minimum purpose and correct behavior even in edge cases. This
 is especially true for functions defined in the standard core such as
 the one you are referring to. The nice thing is that from that solid
 core as a foundation we can build libraries which allow us to be a bit
 more sloppy when we want to whereas it's much harder to go the other way
 around.
 
 It may sound a bit like just drink the Kool-Aid but what I'm trying to
 say is: Scheme, as any language, has its own culture and you will be
 much happier when you try to embrace it rather than trying to impose
 concepts from other languages on it. But you probably already know this,
 so I hope my comment on the specific case of your substring issue is of
 help, anyway :-)
 
 
 Moritz
 

I certainly appreciate this design principle, but there is just no
explaining DSSSL using the above philosophy.  I find the interaction
between #!rest and #!key much more sensible in Python, which forbids
every potentially ambiguous case and therefor has much more
consistent behavior with the various combinations available.  

-Alan
-- 
my personal website: http://c0redump.org/

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread John Cowan
.alyn.post. scripsit:

 I certainly appreciate this design principle, but there is just no
 explaining DSSSL using the above philosophy.  I find the interaction
 between #!rest and #!key much more sensible in Python, which forbids
 every potentially ambiguous case and therefor has much more
 consistent behavior with the various combinations available.  

DSSSL was cloning Common Lisp here, and Common Lisp has never given
a single, double, short, or long damn for consistency.

-- 
John Cowan  co...@ccil.org
http://www.ccil.org/~cowan
.e'osai ko sarji la lojban.
Please support Lojban!  http://www.lojban.org

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread John Cowan
Michele La Monaca scripsit:

 I never read them (I promise I'll do, at least R5RS) but I was kind of
 suspecting that no standard would ever tell you if the input is such
 or such... then crash!.

Well, no.  But certainly the C and C++ standards, for example, give
*permission* to crash by using the phrase undefined behavior.
In R6RS, it's the opposite story: essentially all behaviors are defined,
either to produce a certain result or to raise an exception of a certain
type, both of which the user can rely on.

 The way it is, is the chicken way not the scheme way.

To be fair, it is also the way of the great majority of Scheme
implementations.  And most Scheme systems are quite strict, simply
because it's easier to build lax code on top of strict code than the
other way around.  Given Scheme's extreme extensibility of both syntax
and procedures, this is perfectly reasonable: if you want lax behavior,
import a lax library instead of the base (strict) library.

-- 
John Cowanco...@ccil.orghttp://ccil.org/~cowan
Half the lies they tell about me are true.
--Tallulah Bankhead, American actress

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers


Re: [Chicken-hackers] substring function and bounds checks

2013-02-05 Thread Michele La Monaca
On Wed, Feb 6, 2013 at 1:35 AM, John Cowan co...@mercury.ccil.org wrote:

 What Scheme is really about, if it has to be said to be about something,
 is implementation choice.  Schemes provide and extend the language in
 different ways, and you pick the one that provides the levels of speed,
 safety, size, compilation speed, debuggability, or whatever other factors
 you want.

The funny thing looking at the implementation is that it actually does
the check (so no speed) only to have a better opportunity to crash (so
no safety).

Regards,
Michele

___
Chicken-hackers mailing list
Chicken-hackers@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-hackers