RosettaCode

(Topics Related to FBSL)

Re: RosettaCode

Unread postby bugmagnet » Sat May 11, 2013 2:59 pm

Good spotting there, lad. Much appreciated!
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 » Sat May 11, 2013 9:23 pm

And a rollback to letter A:

1. Array concatenation
2. Arithmetic evaluation (temporarily disabled as not meeting the task requirements)
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 » Thu May 16, 2013 8:45 am

Mike,

What are the precedence rules for FBSL expressions?

In the following, which is on the way to becoming the OCTAL numbers solution (and I must confess, I haven't looked to see if octal numbers are already supported), if I don't parenthesize m BAND 7, the function fails. This suggests that /&/ has a higher precedence that /BAND/. Is this the case?

Kind regards,
Bruce.
Code: Select all
#APPTYPE CONSOLE

FOR DIM i = 0 TO 100
    PRINT octal(i), " ";
NEXT
PAUSE

FUNCTION octal(n AS INTEGER)
    DIM m AS INTEGER = n
    DIM r AS STRING = ""
    DO
        r = (m BAND 7) & r
        m = m >> 3
    LOOP UNTIL m = 0
    RETURN r
END FUNCTION
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 » Thu May 16, 2013 9:41 am

Mike

And here's a more generic one for any base between 2 and 36. I'm sure it can be sped up a bit but I'm not in a position to do much more than I have at the moment.
Code: Select all
FUNCTION asbase(n AS INTEGER, base AS INTEGER)
    IF n < 2 OR n > 36 THEN
        EXIT FUNCTION
    END IF
    DIM m AS INTEGER = n
    DIM d AS INTEGER
    CONST digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    DIM r AS STRING = ""
    DO
        d = m MOD base
        r = digits{d + 1} & r
        m = m \ base
    LOOP UNTIL m = 0
    RETURN r
END FUNCTION

Kind regards,
Bruce.

P.S. The reason I stuck to my string2array function in the Vig* cipher was that I couldn't get your version to work. Before I post it I have to work out how to decode, and I haven't had much time for that kind of thing of late.
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 » Thu May 16, 2013 6:28 pm

Both binary and octal numbers (&Bxxxxxx and &Oxxxxx, respectively) can be read by FBSL freely just like hex numbers (&Hxxxxx or 0xXXXXX). There is a HexToDec() function in FBSL but there are no such functions as OctToDec or BinToDec or vice versa just because noone ever needed them in practice. I can surely add them to FBSL but I just don't see any reason to do it only for the sake of Rosetta Code. We do not bundle FBSL to own DLL's like other languages do. Instead, we encourage using somebody else's DLL's where such functions may fit naturally not clogging the main language syntax. Every such addition would increase the binaries' size and I certainly don't like to see them grow at the expense of ballast noone really needs.

As for the precedence, there is no such notion applicable to mixed cases of arithmetic expressions concatenated to string variables. Everything depends on the programmer's intention which action should be the first to take, and which one, the second. This is exactly when it's the programmer's own task to resolve possible ambiguity by using the parentheses, e.g.:
Code: Select all
r = 7
m MOD (7 & r) <== concatenate to 77 and compute the final MOD; or
(m MOD 7) & r <== compute MOD then concatenate it to r


Please remember that FBSL variables are Variants which adjust their current type to the environment they are used in.
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 Mike Lobanovsky » Sat May 18, 2013 4:11 am

More RosettaCode submissions:

1. Binary search
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 » Sat May 18, 2013 8:46 am

Mike Lobanovsky wrote:As for the precedence, there is no such notion applicable to mixed cases of arithmetic expressions concatenated to string variables. Everything depends on the programmer's intention which action should be the first to take, and which one, the second.

Ok, it's just that you do have precedence in arithmetic expressions, viz
Code: Select all
#apptype console

print 2 + 3 * 5
print (2 + 3) * 5
print 2 + (3 * 5)
pause

and I had thought that precedence rules would have been defined for other kinds of expression.

I know better now.

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 » Sat May 18, 2013 5:16 pm

There is arithmetic and logical operator precedence in FBSL but no precedence rules are defined for maths vs. concatenation. By common convention, in such cases expressions are evaluated from the left to the right so that 7 MOD 7 & 7 = $(7 MOD 7) & "7" = "0" & "7" would yield a string literal "07" while 7 MOD (7 & 7) = 7 MOD %("7" & "7") = 7 MOD 77 would yield a numeric literal 7. All the casts shown here explicitly are performed by FBSL internally without the programmer's intervention.
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 May 29, 2013 5:24 pm

G'day,

I've just posted a solution to N_plus_one_half (and the second COBOL one is mine too). I'm troubled a little though: the task asks for the "best" and I'm sure there's a more idiomatic way of doing it in FBSL.

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 Codeman » Wed May 29, 2013 5:52 pm

Code: Select all
#APPTYPE CONSOLE
FOR DIM i = 1 TO 10
   PRINT i;
   IF i < 10 THEN PRINT ", ";   
NEXT
PAUSE


a little smaller code :wink:

and in case there is the same from 10 to 1:

Code: Select all
#APPTYPE CONSOLE
FOR DIM i = 10 DOWNTO 1
   PRINT i;
   IF i > 1 THEN PRINT ", ";   
NEXT
PAUSE
_-=<(¯`·._.·[ CodeMan ]·._.·´¯)>=-_
Your only limit : Your imagination

FBSL V3 TESTED On :
Image

Chris 1 (Corei5-4460S 2.9 GHZ w/ 16GB RAM, GeForce GTX 745 w/ 4GB VRAM, Windows 8 x64 Fr)
Chris 2 (2.0GHz AMD Atlon 64 X2 3800+ w/ 4GB RAM, GeForce 9600GT w/ 1GB VRAM, Windows 7 x64 Fr)
Chris 3 (Tablet pc : ASUS Transformer Book T100)
User avatar
Codeman
FBSL Administrator
FBSL Administrator
 
Posts: 497
Joined: Sat Nov 24, 2007 4:33 pm
Location: France

Re: RosettaCode

Unread postby bugmagnet » Mon Jun 17, 2013 2:37 am

G'day everyone

I've just posted another bit of FBSL up on RosettaCode for the Middle Three Digits task.

I was trying to come up with an optimized COBOL solution. It was so much easier to fabricate in FBSL and then translate back to COBOL. And the FBSL entry came out of the process.

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 » Mon Jun 17, 2013 8:47 pm

Hello Bruce,

I have no objections to your solution in principle though it looks too structured for what we usually see in our everyday practice. Using volatiles and "ForEach Dim" could have spared you a few intermediate variables. And of course, COUNT(array) - 1 = UBOUND(array).
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 Gerome » Mon Jun 17, 2013 8:56 pm

Mike Lobanovsky wrote:Hello Bruce,

I have no objections to your solution in principle though it looks too structured for what we usually see in our everyday practice. Using volatiles and "ForEach Dim" could have spared you a few intermediate variables. And of course, COUNT(array) - 1 = UBOUND(array).


BTW a little remark, you could have saved one variable by directly assigning your dictionnary like this :
Code: Select all
DIM dict[] = Split("123,12345,1234567,987654321,10001,-10001,-123,-100,100,-12345,1,2,-1,-10,2002,-2002,0", ",")
Yours,

(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
:: Full SETUP w. HELP 05th of December 2011 ::
http://www.fbsl.net/setup/FBSLv3.exe [full v3.4.10 installation pack]
http://www.fbsl.net/setup/FBSLv3bin.zip [minimal upgrade to v3.4.10]
Laissons les jolies femmes aux hommes sans imagination. / Let us leave pretty women to men without imagination.(M.Proust)
The success is a defeat for the one who does not want to dance any more! (H.F. Thiefaine)
User avatar
Gerome
FBSL Administrator
FBSL Administrator
 
Posts: 3149
Joined: Sat Mar 12, 2005 9:06 pm
Location: Paris -- France

Re: RosettaCode

Unread postby bugmagnet » Tue Jun 18, 2013 4:39 am

Mike/Gerome,

Thanks, gentlemen, for your help. This forum says I'm an FBSL Geek, but more likely I'm "L'enfant terrible".

Gratefully,
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 bugmagnet » Tue Jun 18, 2013 4:52 am

However,
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", ",")
DIM num AS INTEGER
'DIM i, j

FOREACH DIM i IN 0 TO UBOUND(dict)
    num = ABS(dict[i])
    IF num < 100 THEN
        display(dict[i], "is too small")
    ELSE
        FOREACH DIM j IN 9 DOWNTO 1
            IF num >= (10 ^ j) THEN
                IF j MOD 2 = 1 THEN
                    display(dict[i], "has even number of digits")
                ELSE
                    display(dict[i], middle3(num, j))
                END IF
                EXIT FOREACH
            END IF
        NEXT
    END IF
NEXT

PAUSE

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

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


on the line
Code: Select all
FOREACH DIM j IN 9 DOWNTO 1

I'm getting
Code: Select all
[00012] [middle3.fbs] Error: internal error

Press any key to continue...


Should I have put the main loop inside a sub/function?

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

PreviousNext

Return to FBSL v3 Discussion Board

Who is online

Users browsing this forum: No registered users and 1 guest

cron