Doug Coleman writes:
>
> How would you do this in C? Would you take the return address off the
> stack frame, then start calculating the division-by-2s and pushing
> them to the local stack frame in a loop, and then walk back up
> accumulating them in an array until you hit the return address, a
Actually, here's how you can do this in Factor using the with-datastack word:
USING: kernel math prettyprint sequences ;
{ 1337 } [ [ dup 1 > ] [ 2/ dup ] while ] with-datastack
It's clean looking, but it's pretty slow.
Doug
On Tue, Jun 19, 2012 at 8:33 PM, Doug Coleman wrote:
> How would you
How would you do this in C? Would you take the return address off the
stack frame, then start calculating the division-by-2s and pushing
them to the local stack frame in a loop, and then walk back up
accumulating them in an array until you hit the return address, at
which point you return the array
John Benediktsson writes:
>
>
> I want to divide an integer by 2 until it gets to 1.
> I have this snippet of code that goes into a loop.
> dup 1 > [ dup 2 /i ] [ ] if
> When it gets to 1 I want to push the elements into a sequence.
>
>
> Take a look at the "produce" word:
>
> : divide-unti
Marshall Lochbaum writes:
>
>
> I figured there would be a word designed for that already... Here's my take on
this anyway.
> I would append the items to a sequence as the algorithm goes along:
> { } swap [ dup 1 > ] [ dup 2 /i [ suffix ] dip ] while suffixaccomplishes the
task.
>
> Marshall
If you are concerned about performance ("suffix" makes a new array each
time), then you can use vectors and "push" onto them, or use the "make"
vocabulary which provides some convenience mechanisms for that. Or look
into "collector-for" which the produce word uses...
On Tue, Jun 19, 2012 at 8:09
The example for the produce word is basically your solution already.
\ produce help
click the example
It should look like this:
USING: kernel math prettyprint sequences ;
1337 [ dup 0 > ] [ 2/ dup ] produce nip .
{ 668 334 167 83 41 20 10 5 2 1 0 }
This is basically John's solution, too.
Chee
I figured there would be a word designed for that already... Here's my take
on this anyway.
I would append the items to a sequence as the algorithm goes along:
{ } swap [ dup 1 > ] [ dup 2 /i [ suffix ] dip ] while suffix
accomplishes the task.
Marshall
On Tue, Jun 19, 2012 at 10:50 PM, Graham
>
> I want to divide an integer by 2 until it gets to 1.
>
> I have this snippet of code that goes into a loop.
>
> dup 1 > [ dup 2 /i ] [ ] if
>
> When it gets to 1 I want to push the elements into a sequence.
>
Take a look at the "produce" word:
: divide-until-2 ( n -- seq )
[ dup 1 > ] [
Doug Coleman writes:
>
> In general, you shouldn't want to do this, as all Factor words
> (besides the one I'm about to show you) need a fixed number of
> parameters at compile-time. Macros expand at compile-time, so that's
> one way to get around the restriction.
>
> The other way it to use a
In general, you shouldn't want to do this, as all Factor words
(besides the one I'm about to show you) need a fixed number of
parameters at compile-time. Macros expand at compile-time, so that's
one way to get around the restriction.
The other way it to use a slow word called with-datastack:
{ 1
The word narray constructs a sequence but needs an integer to tell it how many
elements to push. If you want to push all the stack elements how do you find
the stack's depth?
--
I've managed by installing sqlite from the software center.
thanks.
On Tue, Jun 19, 2012 at 2:05 PM, Grisha Freilikhman wrote:
> I am using ubuntu x86.I've compilted sqlite3 and checked it, it works.
>
> Now, in foctor I'm running:
> "my.db" db-open
>
> db-open fails with:
> The image refers to
I am using ubuntu x86.I've compilted sqlite3 and checked it, it works.
Now, in foctor I'm running:
"my.db" db-open
db-open fails with:
The image refers to a libray or symbol that was not found at load time.
Any suggestions?
On Tue, Jun 19, 2012 at 12:34 AM, Jon Harper wrote:
> On debian-ish d
14 matches
Mail list logo