Hi,
here's my modest implementation of the mighty bubble-sort :
http://paste.factorcode.org/paste?id=1858

If you want to look at it, I think it's a more idiomatic solution. But
it can surely be improved.
Cheers,
Jon

2010/8/11 Vladimir Darmanian <vladimir.darman...@gmail.com>:
> Hi Ander
>
> Programming in factor is best done in a bottom-up approach - break down that
> long bubble-sort  word into the most elementary operations each assigned to
> their own word, then combine them.
> Try to at least replace every quotation with a word eg. [ drop drop ] is a
> standard word in factor = [ 2drop ].
> Once you do that the answer/problem will become obvious.  Basically, the two
> quotations passed to the if word consume a different number of stack
> objects.
>
> I'm a bit of a newbie myself so can't quickly point you at the exact spot of
> the problem, and do not have time right now to go through it.
> Finally, try using the walker (Ctrl-W) to inspect your code and you'll see
> exactly where/why factor complains.
> Vlad.
> On Wed, Aug 11, 2010 at 2:53 PM, Андрей Полищук <ander-skir...@mail.ru>
> wrote:
>>
>> Hello,
>> Sorry for bad english,
>> I am a newbie in factor,
>> came from common-lisp fields.
>>
>> I've tried to implement bubble-sort, and got error i cannot deal with for
>> few hours.
>> Can someone please help?
>>
>> Sorry for maybe weird and not true-factor-way code:
>> :: bubble-sort! ( seq cmp -- sorted-seq )
>>     seq [ dup 0 = [ drop drop ] [
>>             swap over 1 - seq nth cmp [ :> j!
>>                     [ j 0 > [ j j 1 - [ seq nth ] bi@ < ] [ f ] if ] [
>>                             j j 1 - seq exchange j 1 - j!
>>                     ] while
>>             ] [ drop ] if
>>           ] if
>>     ] each-index seq ;
>>
>> The error is:
>>  The input quotations to "if" don't match their expected effects
>>  Input
>> Expected         Got
>>  [ drop drop ]                                                     (( ..a
>> -- ..b )) (( x x -- ))
>>  [ _ _ 2 load-locals swap over 1 - -1 get-local nth 0 get-local... (( ..a
>> -- ..b )) (( x x -- x x ))
>>
>> And second, as you can see i've used that construction: j 0 > [ j j 1 - [
>> seq nth ] bi@ < ] [ f ] if
>> as analog for binary lazy left-to-right `and'. It's because standart
>> Factor `and' operators are working just with booleans from stack. So im
>> interested whats an idiomatic way to do things like this? Maybe it could be
>> useful to have lazy left-to-right logical operators that work on quotations
>> in standart library?
>>
>> Thanks.
>> Ander
>>
>>
>> ------------------------------------------------------------------------------
>> This SF.net email is sponsored by
>>
>> Make an app they can't live without
>> Enter the BlackBerry Developer Challenge
>> http://p.sf.net/sfu/RIM-dev2dev
>> _______________________________________________
>> Factor-talk mailing list
>> Factor-talk@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by
>
> Make an app they can't live without
> Enter the BlackBerry Developer Challenge
> http://p.sf.net/sfu/RIM-dev2dev
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
>

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to