RosettaCode

(Topics Related to FBSL)

Re: RosettaCode

Unread postby Mike Lobanovsky » Tue Jun 18, 2013 10:27 am

No Bruce,

Yours is not a correct syntax for the ForEach/Next loop. This form of iteration allows us to access each element in an array without referring to any particular index within the array or the array bounds per se. In this way, we are able to avoid creating implicitly any unwanted elements if the array happens to be a dynamic array. dict is a dynamic array (in fact, every FBSL text split function returns one) and we'd better use unindexed iteration to be on the safe side.

OTOH the inner loop does need an index to calculate the power, which yields the following resultant code:
Code: Select all
FOREACH DIM num IN dict
   IF ABS(num) < 100 THEN
      display(num, "is too small")
   ELSE
      FOR DIM j = 9 DOWNTO 1
         IF ABS >= 10 ^ j THEN
            IF j MOD 2 = 1 THEN
               display(num, "has even number of digits")
            ELSE
               display(num, middle3(ABS, j))
            END IF
            EXIT FOR
         END IF
      NEXT
   END IF
NEXT


The use of ForEach/Next and volatile ABS permits us to:

1. avoid referencing an array element through its index which is a time-costly operation in any language, whereas here the element is automatically buffered in the "num" variable;
2. reduce the count of temporary number variables to just one "num" by buffering the temporary absolute value in the ABS() function's own volatile.

Also, the middle3() function can be safely reduced to:
Code: Select all
FUNCTION middle3(n, pwr)
    DIM m = (n \ (10 ^ (pwr \ 2 - 1))) MOD 1000
    IF m = 0 THEN RETURN "000"
    RETURN m
END FUNCTION


All this will make our code a few temporary variables and lines shorter and also faster.

And finally, to me "geek" sounds more like "ill with" which doesn't necessarily mean "master in". When combined, "geek" and "master" yield a "guru". :D
Mike
"Я старый солдат, мадам, и не знаю слов любви."
"I am an old soldier, ma'am, and I don't know the words of love."
"Je suis un vieux soldat, madame, et je ne connais pas les mots d'amour."
"Ich bin ein alter Soldat, gnädige Frau, und ich weiß nicht die Worte der Liebe."

__________________________________________________________________________________________________________________________________________________
(3.2GHz i5 Core Quad, 8GB RAM / 2 x nVidia GTX 550Ti SLI-bridged, 2GB VRAM)
(x86 Win XP Pro Russian Sp3/x86 Win Vista Ultimate Sp2/x64 Win 7 Ultimate Sp1/Wine in x64 elementaryOS Luna)
User avatar
Mike Lobanovsky
FBSL Administrator
FBSL Administrator
 
Posts: 1824
Joined: Tue Apr 19, 2005 8:22 am
Location: Republic of Belarus

Re: RosettaCode

Unread postby bugmagnet » Tue Jun 18, 2013 3:39 pm

Mike

Thanks for your code. Frustratingly, it still doesn't work for me, giving me
Code: Select all
[00008] [middle3.fbs] Error: internal error

Press any key to continue...

This is the code I'm using, cut and pasted from your previous posting
Code: Select all
#APPTYPE CONSOLE

DIM dict[] = Split("123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0", ",")
FOREACH DIM num IN dict
    IF ABS(num) < 100 THEN
        display(num, "is too small")
    ELSE
        FOR DIM j = 9 DOWNTO 1
            IF ABS >= 10 ^ j THEN
                IF j MOD 2 = 1 THEN
                    display(num, "has even number of digits")
                ELSE
                    display(num, middle3(ABS, j))
                END IF
                EXIT FOR
            END IF
        NEXT
    END IF
NEXT
PAUSE

FUNCTION display(num, msg)
    PRINT LPAD(num, 11, " "), " --> ", msg
END FUNCTION

FUNCTION middle3(n, pwr)
    DIM m = (n \ (10 ^ (pwr \ 2 - 1))) MOD 1000
    IF m = 0 THEN RETURN "000"
    RETURN m
END FUNCTION

Just in case it's an old version of the binary, here are the directory details and an md5sum
Code: Select all
03/06/2013  11:09 PM           600,576 Fbsl.exe

Code: Select all
C:\Program Files\FBSLv3>md5sum fbsl.exe
998ab4f2bb7d900a187cc2f8a7e051fd *fbsl.exe


Kind regards,
Bruce.
User avatar
bugmagnet
FBSL geek
FBSL geek
 
Posts: 175
Joined: Sat Apr 06, 2013 5:58 am
Location: Fremantle WA, Australia

Re: RosettaCode

Unread postby Mike Lobanovsky » Wed Jun 19, 2013 12:35 am

Surprisingly, it gives the same error for me too. The offending statement is DIM-ing j in the inner loop header. If you DIM j outside the both loops or as a separate statement within the outer loop, the program runs fine.

There must be a regression somewhere in the For/ForEach/Next or Dim statement sources. I need time to investigate the cause. I'll hunt and kill the bug eventually and then we'll update the Rosetta Code entry.

Thanks for pointing this bug out to me.
Mike
"Я старый солдат, мадам, и не знаю слов любви."
"I am an old soldier, ma'am, and I don't know the words of love."
"Je suis un vieux soldat, madame, et je ne connais pas les mots d'amour."
"Ich bin ein alter Soldat, gnädige Frau, und ich weiß nicht die Worte der Liebe."

__________________________________________________________________________________________________________________________________________________
(3.2GHz i5 Core Quad, 8GB RAM / 2 x nVidia GTX 550Ti SLI-bridged, 2GB VRAM)
(x86 Win XP Pro Russian Sp3/x86 Win Vista Ultimate Sp2/x64 Win 7 Ultimate Sp1/Wine in x64 elementaryOS Luna)
User avatar
Mike Lobanovsky
FBSL Administrator
FBSL Administrator
 
Posts: 1824
Joined: Tue Apr 19, 2005 8:22 am
Location: Republic of Belarus

Re: RosettaCode

Unread postby bugmagnet » Tue Oct 06, 2015 9:44 am

Not a bug report. just dropping by for a "hello, what's happening?"

I recently posted a binary notation solution in RosettaCode in the mLite language and thought it needed an FBSL entry as well. Below is the conversion of the mLite. There's probably a better way of doing it in FBSL. Suggestions?

Code: Select all
function binary(byref n as integer, byref s as string = "") as string
   if n > 0 then
      return binary(n \ 2, (n mod 2) & s)
   else
      return s
   end if
end function


msgbox(0,binary(9000),"",0)


Regards,
тот, кто привлекает ошибки
User avatar
bugmagnet
FBSL geek
FBSL geek
 
Posts: 175
Joined: Sat Apr 06, 2013 5:58 am
Location: Fremantle WA, Australia

Re: RosettaCode

Unread postby bugmagnet » Tue Oct 06, 2015 10:39 am

Actually works just as well with byval rather than byref.
User avatar
bugmagnet
FBSL geek
FBSL geek
 
Posts: 175
Joined: Sat Apr 06, 2013 5:58 am
Location: Fremantle WA, Australia

Re: RosettaCode

Unread postby Mike Lobanovsky » Tue Oct 06, 2015 12:54 pm

Привет тому, кто привлекает ошибки! Сколько лет, сколько зим! :D

bugmagnet wrote:There's probably a better way of doing it in FBSL.

Algorithmically, not really. FBSL does have a built-in Hex() string function but no Bin() or Oct() -- even though it understands binary and octal numeric input -- simply because nobody has ever asked for them.

So, I can only suggest some syntax mods to let FBSL do all the necessary conversions for you automatically thus making the source code somewhat shorter:
Code: Select all
Function ToBinary(n, s = "")
  If n Then Return ToBinary(n \ 2, n Mod 2 & s)
  Return s
End Function

MsgBox(,ToBinary(9000),,) ' from decimal
MsgBox(,ToBinary(&B10001100101000),,) ' from binary
MsgBox(,ToBinary(&O21450),,) ' from octal
MsgBox(,ToBinary(&H2328),,) ' from hexadecimal

I've also changed the function name because BINARY is one of FBSL's built-in constants to specify the respective file access mode which would otherwise be inaccessible because of your function name "shadowing" it from the user.

"hello, what's happening?"

Nothing much really because like most other BASICs out there, we're experiencing a noticeable lack of interest lately, which effectively discourages my attempts at any further dev work. FBSL is currently in a suspended state, development-wise, even though I do have a yet-unpublished Release Candidate 3 ready that fixes a few RC2 bugs and adds zip and 7-zip functionality together with a few more image formats now available in FBSL intrinsically.
Mike
"Я старый солдат, мадам, и не знаю слов любви."
"I am an old soldier, ma'am, and I don't know the words of love."
"Je suis un vieux soldat, madame, et je ne connais pas les mots d'amour."
"Ich bin ein alter Soldat, gnädige Frau, und ich weiß nicht die Worte der Liebe."

__________________________________________________________________________________________________________________________________________________
(3.2GHz i5 Core Quad, 8GB RAM / 2 x nVidia GTX 550Ti SLI-bridged, 2GB VRAM)
(x86 Win XP Pro Russian Sp3/x86 Win Vista Ultimate Sp2/x64 Win 7 Ultimate Sp1/Wine in x64 elementaryOS Luna)
User avatar
Mike Lobanovsky
FBSL Administrator
FBSL Administrator
 
Posts: 1824
Joined: Tue Apr 19, 2005 8:22 am
Location: Republic of Belarus

Re: RosettaCode

Unread postby bugmagnet » Wed Oct 07, 2015 11:52 am

Regarding the current quietness, perhaps my latest blog posting will stir things up. That and the ping via Google Plus.

bugmagnet
User avatar
bugmagnet
FBSL geek
FBSL geek
 
Posts: 175
Joined: Sat Apr 06, 2013 5:58 am
Location: Fremantle WA, Australia

Previous

Return to FBSL v3 Discussion Board

Who is online

Users browsing this forum: No registered users and 1 guest

cron