Re: I want to convert a list of integers to their negative self
Re: I want to convert a list of integers to their negative self
- Subject: Re: I want to convert a list of integers to their negative self
- From: Michelle Steiner <email@hidden>
- Date: Wed, 28 Feb 2001 23:18:45 -0800
On 2/28/01 8:38 PM, email@hidden <email@hidden> wrote:
>
> set a to {"1", "3", "7"}
>
>
>
> repeat with i in a
>
> set a to rest of a & i div -1
>
> end repeat
>
>
This is a great one! I'm not sure I understand exactly how it does it, but
>
thanks.
"Rest of" returns the all but the first element of a list (e.g., rest of
{"1", "3", "7"} returns {"3", "7"}.
"i div -1" returns -1. If i is a string representation of a number
(e.g., "1"), it is coerced to a number first.
So, set a to rest of a & "1" div -1 returns {"3", "7", -1}
Running it through the the loop uses the original list, and not the
modified list, for the iterations.
Logging each iteration
set a to {"1", "3", "7"}
log a
repeat with i in a
set a to rest of a & i div -1
log a
end repeat
(*1, 3, 7*)
(*3, 7, -1*)
(*7, -1, -3*)
(*-1, -3, -7*)
What is curious to me is that the log doesn't show the quotes around any
strings.
>
One question though, only one person has even made reference to this
>
and even that person didn't try this, but why not:
>
>
set a to {1, 3, 7}
>
>
in the first place. Does the original list have to be a list of strings or
>
can you just make it a list of integers to begin with?
Yes, you can use integers to begin with, or even reals; I used strings in
my solution because that's how the problem was presented.
--Michelle
----------------------------------------------------------------------
| Michelle Steiner | We're not human beings having a spiritual |
| | experience. We're spiritual beings |
| email@hidden | having a human experience. |
----------------------------------------------------------------------