In perl, the simple $A=$A+0; covers many cases in a simple form.
if it's unbound, if it's a string representing a number, if it's a string such as 'null'
in lisp, I end up writing a series of tests,
is it bound?
ok, it's bound, is it some string such as 'null'
ok, it's a number representation, now I can read-from-string
These questions arise from some XML parsing, sometimes the information
of interest is completely missing, sometimes it is present, but has no
value, so there is a 'null', and most of the time, a value is present.
I'm wondering if there is some simple way in lisp that I don't know to
match that simple $A=$A+0; form.
The reason this popped up is for the first time, when I have written a
lisp replacement for some perl code written years ago, the lisp
version was longer. I noticed that I had had to write
(defun sql-store() "save collected data in rdb"
(unless (or
(not (stringp (participant-team-name (line-away-tm *current-line))))
(not (stringp (participant-team-name (line-home-tm *current-line))))
(not (stringp (participant-pitcher (line-away-tm *current-line))))
(not (stringp (participant-pitcher (line-home-tm *current-line))))
(string-equal "null" (participant-team-name (line-away-tm *current-line)))
(string-equal "null" (participant-team-name (line-home-tm *current-line)))
(string-equal "null" (participant-pitcher (line-away-tm *current-line)))
(string-equal "null" (participant-pitcher (line-home-tm *current-line)))
) ; no pitchers => no useful data
where 'line and 'participant are structures. And
(if al-s ; geez what a pita, so simple in perl
(progn (setq al-s (rfs al-s))
(when (and (not (numberp al-s)) (string-equal "null" al-s)) (setq al-s 0)))
(setq al-s 0))
well, it works, and that's all that really matters....but
TIA
On 10 June, 12:16, GP lisper <········@CloudDancer.com> wrote:
> In perl, the simple $A=$A+0; covers many cases in a simple form.
When I see Perl programs which do that kind of thing I usually find
it's best to simply kill the people who wrote them.
Don't misunderstand me: I write quite a lot of Perl (more than I write
Lisp nowadays) and I like it as a language on the whole. But, I mean,
ick, that kind of code just makes me feel ill.
Tim Bradshaw <··········@tfeb.org> writes:
> On 10 June, 12:16, GP lisper <········@CloudDancer.com> wrote:
>
>> In perl, the simple $A=$A+0; covers many cases in a simple form.
>
> When I see Perl programs which do that kind of thing I usually find
> it's best to simply kill the people who wrote them.
>
> Don't misunderstand me: I write quite a lot of Perl (more than I write
> Lisp nowadays) and I like it as a language on the whole. But, I mean,
> ick, that kind of code just makes me feel ill.
Is there another way to convert "123" into 123 in perl?
--
__Pascal Bourguignon__
On 2009-06-11, Pascal J. Bourguignon <···@informatimago.com> wrote:
> Tim Bradshaw <··········@tfeb.org> writes:
>
>> On 10 June, 12:16, GP lisper <········@CloudDancer.com> wrote:
>>
>>> In perl, the simple $A=$A+0; covers many cases in a simple form.
>>
>> When I see Perl programs which do that kind of thing I usually find
>> it's best to simply kill the people who wrote them.
>>
>> Don't misunderstand me: I write quite a lot of Perl (more than I write
>> Lisp nowadays) and I like it as a language on the whole. But, I mean,
>> ick, that kind of code just makes me feel ill.
>
> Is there another way to convert "123" into 123 in perl?
That's not what the above does. Converting "123" to 123 is like this:
$num = '"123"';
$num =~ s/"//g;
print $num, "\n";
Output:
123
:)
Now this 123 does not appear to require further conversion to anything. You can
use it as a number: $num + 1 yields 124. You can use it as a string:
$num ~= s/3/9/ turns it into 129.
Kaz Kylheku <········@gmail.com> writes:
> On 2009-06-11, Pascal J. Bourguignon <···@informatimago.com> wrote:
>> Tim Bradshaw <··········@tfeb.org> writes:
>>
>>> On 10 June, 12:16, GP lisper <········@CloudDancer.com> wrote:
>>>
>>>> In perl, the simple $A=$A+0; covers many cases in a simple form.
>>>
>>> When I see Perl programs which do that kind of thing I usually find
>>> it's best to simply kill the people who wrote them.
>>>
>>> Don't misunderstand me: I write quite a lot of Perl (more than I write
>>> Lisp nowadays) and I like it as a language on the whole. But, I mean,
>>> ick, that kind of code just makes me feel ill.
>>
>> Is there another way to convert "123" into 123 in perl?
>
> That's not what the above does. Converting "123" to 123 is like this:
>
> $num = '"123"';
> $num =~ s/"//g;
>
> print $num, "\n";
>
> Output:
>
> 123
>
> :)
>
> Now this 123 does not appear to require further conversion to anything. You can
> use it as a number: $num + 1 yields 124. You can use it as a string:
> $num ~= s/3/9/ turns it into 129.
Yes, I misremembered. It seems there's no way to distinguish a number
from a string in perl...
What $A=$A+0 does is to remove the prefix 0*:
$A="00123";
$A=$A+0;
print $A
prints:
123
--
__Pascal Bourguignon__
On Thu, 11 Jun 2009 17:26:40 +0000 (UTC), <········@gmail.com> wrote:
>
> That's not what the above does. Converting "123" to 123 is like this:
>
> $num = '"123"';
> $num =~ s/"//g;
>
> print $num, "\n";
Ah, the joys of being paid by lines of code and not by the schedule.
On 2009-06-10 11:56:13 -0400, Tim Bradshaw <··········@tfeb.org> said:
> When I see Perl programs which do that kind of thing I usually find
> it's best to simply kill the people who wrote them.
Tentacles getting twitchy again?
;^)
--
Raffael Cavallaro
GP lisper <········@CloudDancer.com> writes:
> In perl, the simple $A=$A+0; covers many cases in a simple form.
>
> if it's unbound, if it's a string representing a number, if it's a string such as 'null'
Well, usually one would encapsulate that particular test into a single
function. You don't provide an example that shows something unbound, so
I'm not sure what you mean by that. About the only things that can be
unbound are slots or global variables. If you don't need the bound
value test (and setting a default value of NIL can solve that problem),
then you can write a simple
(defun validp (value)
"Tests whether this is a valid value."
(and (stringp value)
(not (string= value "null"))))
[Optionally use string-equal if you want a case-insensitive test]
> in lisp, I end up writing a series of tests,
>
> is it bound?
> ok, it's bound, is it some string such as 'null'
> ok, it's a number representation, now I can read-from-string
Again, a function can encapsulate this.
(defun get-integer-value (raw-value)
"Return an integer value from RAW-VALUE if it is valid."
(when (validp raw-value)
(parse-integer raw-value :junk-allowed t)))
> These questions arise from some XML parsing, sometimes the information
> of interest is completely missing, sometimes it is present, but has no
> value, so there is a 'null', and most of the time, a value is present.
>
> I'm wondering if there is some simple way in lisp that I don't know to
> match that simple $A=$A+0; form.
Um, when the string value in Perl is "null", do you want to collect it?
It seems you need some test for that as well. Unless perl treats the
string "null" specially. (I'm no Perl expert). But you handle that by
using NIL in lisp instead of the special Perl string.
> The reason this popped up is for the first time, when I have written a
> lisp replacement for some perl code written years ago, the lisp
> version was longer. I noticed that I had had to write
>
> (defun sql-store() "save collected data in rdb"
> (unless (or
> (not (stringp (participant-team-name (line-away-tm *current-line))))
> (not (stringp (participant-team-name (line-home-tm *current-line))))
> (not (stringp (participant-pitcher (line-away-tm *current-line))))
> (not (stringp (participant-pitcher (line-home-tm *current-line))))
>
> (string-equal "null" (participant-team-name (line-away-tm *current-line)))
> (string-equal "null" (participant-team-name (line-home-tm *current-line)))
> (string-equal "null" (participant-pitcher (line-away-tm *current-line)))
> (string-equal "null" (participant-pitcher (line-home-tm *current-line)))
> ) ; no pitchers => no useful data
>
>
> where 'line and 'participant are structures.
You could pass the accessor functions to a (mildly) higher-order
function that checks for the validity of the structure slots:
(defun check-validity (value &rest accessors)
(loop for accessor in accessors
always (and (funcall accessor (line-away-tm value))
(funcall accessor (line-home-tm value)))))
And then write your test as
(when (check-validity *current-line*
#'participant-team-name
#'participant-pitcher)
...)
There is also a macro solution possible.
> And
>
> (if al-s ; geez what a pita, so simple in perl
> (progn (setq al-s (rfs al-s))
> (when (and (not (numberp al-s)) (string-equal "null" al-s)) (setq al-s 0)))
> (setq al-s 0))
>
> well, it works, and that's all that really matters....but
(setq al-s (or (get-integer-value al-s) 0))
--
Thomas A. Russ, USC/Information Sciences Institute
i'm not so impressed that you can use something like
$A=$A+0
in perl to do something somewhat useful. but i WOULD be impressed
if could name the idiom then use the name instead of the obscure
syntax.
something like the following.
change_to_zero_if_undefined(A)
jimka wrote:
> i'm not so impressed that you can use something like
> $A=$A+0
> in perl to do something somewhat useful. but i WOULD be impressed
> if could name the idiom then use the name instead of the obscure
> syntax.
> something like the following.
>
> change_to_zero_if_undefined(A)
>
perl 5.10 has a "set unless defined" operator for this purpose:
$A //= 0;
Steve C <·········@juno.com> writes:
> jimka wrote:
>> i'm not so impressed that you can use something like
>> $A=$A+0
>> in perl to do something somewhat useful. but i WOULD be impressed
>> if could name the idiom then use the name instead of the obscure
>> syntax.
>> something like the following.
>> change_to_zero_if_undefined(A)
>>
>
> perl 5.10 has a "set unless defined" operator for this purpose:
>
> $A //= 0;
Great. But has it a "set unless my granma awoke on the right foot and
it's full moon" operator?
--
__Pascal Bourguignon__
Pascal J. Bourguignon wrote:
> Steve C <·········@juno.com> writes:
>
>> jimka wrote:
>>> i'm not so impressed that you can use something like
>>> $A=$A+0
>>> in perl to do something somewhat useful. but i WOULD be impressed
>>> if could name the idiom then use the name instead of the obscure
>>> syntax.
>>> something like the following.
>>> change_to_zero_if_undefined(A)
>>>
>> perl 5.10 has a "set unless defined" operator for this purpose:
>>
>> $A //= 0;
>
> Great. But has it a "set unless my granma awoke on the right foot and
> it's full moon" operator?
>
>
I believe that's in Perl 6.
Steve C <·········@juno.com> writes:
> perl 5.10 has a "set unless defined" operator for this purpose:
>
> $A //= 0;
Hmmm, so does Common Lisp:
(defvar *a* 0)
--
Thomas A. Russ, USC/Information Sciences Institute
GP lisper <········@CloudDancer.com> writes:
> In perl, the simple $A=$A+0; covers many cases in a simple form.
>
> if it's unbound, if it's a string representing a number, if it's a string such as 'null'
>
> in lisp, I end up writing a series of tests,
>
> is it bound?
> ok, it's bound, is it some string such as 'null'
> ok, it's a number representation, now I can read-from-string
>
> These questions arise from some XML parsing, sometimes the information
> of interest is completely missing, sometimes it is present, but has no
> value, so there is a 'null', and most of the time, a value is present.
>
> I'm wondering if there is some simple way in lisp that I don't know to
> match that simple $A=$A+0; form.
(defgeneric plus (a b)
(:method ((a t) (b t)) (error "cannot add a ~A to a ~A" (type-of a) (type-of b)))
(:method ((a number) (b t)) (plus b a))
(:method ((a null) (b t)) (plus b 0))
(:method ((a symbol) (b t)) (plus b (read-from-string (string a))))
(:method ((a string) (b t)) (plus b (read-from-string a)))
(:method ((a number) (b number)) (cl:+ a b))
;; ...
)
(let (($a "24")) (plus $a 1)) --> 25
(let (($a "24")) (plus 'nil $a)) --> 24
If you want to write (+ $a 1) then:
(shadow '+)
(defgeneric + ...)
--
__Pascal Bourguignon__
GP lisper <········@CloudDancer.com> writes:
> In perl, the simple $A=$A+0; covers many cases in a simple form.
Lisp can be quick&dirty too: If you want a cheap way to get an integer
from a string or return 0, consider:
(or (ignore-errors (parse-integer a)) 0)
IMHO much less obfuscated than the perl code.
--
(espen)
On Wed, 10 Jun 2009 14:30:26 +0200, <·····@vestre.net> wrote:
> GP lisper <········@CloudDancer.com> writes:
>
>> In perl, the simple $A=$A+0; covers many cases in a simple form.
>
> Lisp can be quick&dirty too: If you want a cheap way to get an integer
> from a string or return 0, consider:
>
> (or (ignore-errors (parse-integer a)) 0)
>
> IMHO much less obfuscated than the perl code.
It would be great, if real numbers weren't present. I will try it on
about 2/3s of the data in this problem. Thanks, naturally I'll name
the macro for your form appropriately ;-)
$A=$A+0; is far too simple to ever be part of the
Obfuscated Perl Contest. But thinking about the lisp aspects raised in
this posting certainly improved my understanding of types in Common
Lisp, which is something the OPC never did for me.
--
Humans are allergic to change. "We've always done it that way" is not
a good reason to continue to do so. That's why I have a clock on my
office wall that runs backwards. It forces visitors to think.
They hate me for that. - Admiral Hopper.
GP lisper <········@CloudDancer.com> writes:
> It would be great, if real numbers weren't present. I will try it on
> about 2/3s of the data in this problem. Thanks, naturally I'll name
> the macro for your form appropriately ;-)
Well, you can always use something like:
(defun number-or-zero (x)
(let* ((*read-eval* nil)
(my-num (ignore-errors (read-from-string x))))
(typecase my-num
(number my-num)
(otherwise 0))))
But what solution to choose here, both in perl and in lisp, depends on
the answers to a lot of questions which the programmer should ask
himself:
- is this throw-away WORN code (Write Once Read Never) or code that
should be maintainable?
- can you trust the input's integrity? (e.g. does it have predictable
types?)
- and as a special case of that: can you trust the input's security?
(e.g. could someone have messed with it and e.g. inserted
#.(call-unix-shell "rm -rf /")?)
- is it really sensible to ignore non-numeric input and treat it as
zero?
- if not, what is the appropriate action to take?
- and so on....
--
(espen)
On 2009-06-10, Espen Vestre <·····@vestre.net> wrote:
> GP lisper <········@CloudDancer.com> writes:
>
>> In perl, the simple $A=$A+0; covers many cases in a simple form.
>
> Lisp can be quick&dirty too: If you want a cheap way to get an integer
> from a string or return 0, consider:
>
> (or (ignore-errors (parse-integer a)) 0)
Shorter:
(parse-integer a :junk-allowed t)
and you get a second value which tells you where in the string the junk begins.
Kaz Kylheku <········@gmail.com> writes:
> Shorter:
>
> (parse-integer a :junk-allowed t)
>
> and you get a second value which tells you where in the string the junk begins.
I know, but I wanted a "foolish perl programmer's version" :-) (your
version will signal an error if a is not a string)
--
(espen)
On 2009-06-10, GP lisper <········@CloudDancer.com> wrote:
> In perl, the simple $A=$A+0; covers many cases in a simple form.
>
> if it's unbound, if it's a string representing a number, if it's a string such as 'null'
Sweeping unbound variable errors under the rug isn't such a hot idea:
$must_shut_down_reactor=$must_shut_down_raector+0
In article <·······················@phoenix.clouddancer.com>,
GP lisper <········@CloudDancer.com> wrote:
> In perl, the simple $A=$A+0; covers many cases in a simple form.
>
> if it's unbound, if it's a string representing a number, if it's a string
> such as 'null'
If it's unbound, perl complains "Use of uninitialized value in addition
(+)".
You *do* have warnings enabled in your perl scripts, don't you?
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
On Wed, 10 Jun 2009 19:21:33 -0400, <······@alum.mit.edu> wrote:
>
> You *do* have warnings enabled in your perl scripts, don't you?
If I wanted a seatbelt, I wouldn't use perl.
GP lisper wrote:
> On Wed, 10 Jun 2009 19:21:33 -0400, <······@alum.mit.edu> wrote:
>> You *do* have warnings enabled in your perl scripts, don't you?
>
> If I wanted a seatbelt, I wouldn't use perl.
Ah, but if you wanted to use Perl would you wear an airbag? That may not
make sense.....
kt
Gl> in lisp, I end up writing a series of tests, Gl> is it bound?
Gl> ok, it's bound, is it some string such as 'null'
Gl> ok, it's a number representation, now I can read-from-string
wtf, are you a moron? write a fucking function or a macro for this.
lisp has all functionality to avoid boilerplate code.
Gl> (defun sql-store() "save collected data in rdb"
Gl> (unless (or
Gl> (not (stringp (participant-team-name (line-away-tm *current-line))))
Gl> (not (stringp (participant-team-name (line-home-tm *current-line))))
Gl> (not (stringp (participant-pitcher (line-away-tm *current-line))))
Gl> (not (stringp (participant-pitcher (line-home-tm *current-line))))
Gl> (string-equal "null" (participant-team-name (line-away-tm
Gl> *current-line))) (string-equal "null" (participant-team-name
Gl> (line-home-tm *current-line))) (string-equal "null"
Gl> (participant-pitcher (line-away-tm *current-line))) (string-equal
Gl> "null" (participant-pitcher (line-home-tm *current-line))) ) ; no
Gl> pitchers => no useful data
i have no idea what does this braindamaged code does, but you could write
it as a loop:
(defun sql-store ()
(unless (loop for fn in (list #'participant-team-name
#'participant-pitcher)
thereis (or (not (stringp (line-away-tm *current-line)))
(not (stringp (line-home-tm *current-line)))))
.......the second part is so messed up that i do not even have a fucking
.......idea what it could mean, but definitely you can write it as a loop
you should really read some book about basics of programming, like loops
and functions, do some excersises... if you learn a good style, i'm pretty
sure you won't even need monstrosities like this. otherwise, use Perl --
it is perfectly optimized for people who do not care about quality of their
code, cannot write functions etc.