FizzBuzz

75 views
Skip to first unread message

dpt

unread,
Dec 14, 2016, 5:44:47 PM12/14/16
to ParaSail Programming Language
Hello

I am trying to code something similar like that:
https://gist.github.com/hitode909/140516
https://gist.github.com/kings13y/2818299

But after reading the manual, it looks like there is no pattern matching yet.
Am I right?

Tucker Taft

unread,
Dec 15, 2016, 7:12:08 AM12/15/16
to ParaSail Programming Language
You could do something quite like this after defining a helper module "CPair" (for comparable pair):

interface CPair<A_Type is Comparable<>; B_Type is Comparable<>> is
   const A : A_Type
   const B : B_Type
   op "=?"(Left, Right : CPair) -> Ordering is
     (Left.A != Right.A? Left.A =? Right.A: Left.B =? Right.B)
end interface CPair

import CPair
func FizzBuzz(I : Integer) -> Univ_String is
   type Bool_Pair is CPair<Boolean, Boolean>

   case Bool_Pair::(I mod 3 == 0, I mod 5 == 0) of
      [(#true, #true)] => return "Fizz Buzz"
      [(#true, #false)] => return "Fizz"
      [(#false, #true)] => return "Buzz"
      [(#false, #false)] => return To_String(I)
   end case
end func FizzBuzz

import FizzBuzz
func Test_FizzBuzz() is
   for I in 1 .. 30 forward loop
      Println(I | " => " | FizzBuzz(I))
   end loop
end func Test_FizzBuzz

//  Executing Test_FizzBuzz results in:
1 => 1
2 => 2
3 => Fizz
4 => 4
5 => Buzz
6 => Fizz
...
27 => Fizz
28 => 28
29 => 29
30 => Fizz Buzz

Basically you can do a "case" on anything that has the "=?" operator defined.  A relatively general pattern matcher module could be defined this way, but it would certainly be nice to build a more flexible mechanism into the language.

-Tuck

--
You received this message because you are subscribed to the Google Groups "ParaSail Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to parasail-programming-language+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tucker Taft

unread,
Dec 15, 2016, 12:01:16 PM12/15/16
to ParaSail Programming Language
Below is a version where "null" is used as a "wildcard" inspired by your second Scala example.
-Tuck
-----------

interface CPair<A_Type is Comparable<>; B_Type is Comparable<>> is
   const A : A_Type
   const B : B_Type
   op "=?"(Left, Right : CPair) -> Ordering is
     (Right.A not null and then Left.A not null and then Left.A != Right.A?
       Left.A =? Right.A:
      Right.B not null and then Left.B not null?
       Left.B =? Right.B: #equal)

end interface CPair

import CPair
func FizzBuzz2(X : Integer) -> Univ_String is
   type Int_Pair is CPair<optional Integer, optional Integer>

   case Int_Pair::(X mod 3, X mod 5) of
      [(0, 0)] => return "Fizz Buzz"
      [(0, null)] => return "Fizz"
      [(null, 0)] => return "Buzz"
      [..] => return "" | X
   end case
end func FizzBuzz2

import FizzBuzz2
func Test_FizzBuzz2() is
   Println("-- version using \"null\" as a wildcard --")

   for I in 1 .. 30 forward loop
      Println(I | " => " | FizzBuzz2(I))
   end loop
end func Test_FizzBuzz2

-- version using "null" as a wildcard --

1 => 1
2 => 2
3 => Fizz
4 => 4
5 => Buzz
6 => Fizz
7 => 7
8 => 8
9 => Fizz
10 => Buzz

...
27 => Fizz
28 => 28
29 => 29
30 => Fizz Buzz

dpt

unread,
Dec 15, 2016, 2:40:30 PM12/15/16
to ParaSail Programming Language, tuc...@yaletaft.com
Thank you very much!

Any plans to introduce a "general" wildcard and the "more flexible mechanism" into the language?

Tucker Taft

unread,
Dec 15, 2016, 4:53:59 PM12/15/16
to ParaSail Programming Language
It seems like something fairly simple could be added pretty easily.  I will have to think more about it!

-Tuck

On Thu, Dec 15, 2016 at 2:40 PM, dpt <dpt...@arcor.de> wrote:
Thank you very much!

Any plans to introduce a "general" wildcard and the "more flexible mechanism" into the language?

--

Martin Vahi

unread,
Dec 17, 2016, 10:53:21 PM12/17/16
to ParaSail Programming Language

Again, it's easier for me to talk here than anyone to actually do the real work, but I suspect that it might be helpful, if I mention the following 2 things:

A)
The amount of bits that a human is capable of outputting is very limited, regardless of whether the output is obtained in a form of characters through fingers that touch a keyboard or through some virtual reality gloves or through speech or other physical movements. That limits the amount of "code" that anyone is ever able to output, regardless of programming language. (That line of though might be taken even further by noting that the amount of characters that a person reads/hears either visually or through transcription of everything he/she ever hears, from early childhood till last breath, schools, universities, conversations with lovers included, is very limited.)

B)
The length of a program source in terms of number of characters (read: keystrokes) is the shorter, the shorter the most frequent constructs of that source are. A few uses of hopelessly verbose constructs do not influence the length of the source code that much, provided that they are infrequent "enough". The frequency of constructs depends on the problem domain. For example, a banking web application hardly ever includes constructs like "sin(x)", "tan(x)". A mechanics CAD tool might have far less boolean logic constructs in its embedded language than does a source code that is meant to be read by a theorem prover.


I'm not sure, how the frequencies might be measured, but probably C and C++ applications might represent the class of applications that will be written in ParaSail in the future. Probably operating systems, image processing applications, high-performance-computing calculation engines, interpreter implementations, game AI, real-time control systems, applications of equipment, where power consumption is vital.


dpt

unread,
Apr 22, 2017, 9:43:46 AM4/22/17
to ParaSail Programming Language, tuc...@yaletaft.com
How did you decide?

Tucker Taft

unread,
Apr 22, 2017, 9:52:35 AM4/22/17
to ParaSail Programming Language
I would say the most convenient approach would be to complete the support for tuples in ParaSail, with a "=?" operator that automatically provides some amount of pattern matching. Right now there is some syntax designed for tuples, but no support in the interpreter/compiler.  I am increasing a bit the time I have available for ParaSail enhancements, so robust "tuple" support will be one of the top priorities.

-Tuck

--

Edward De Jong

unread,
Apr 24, 2017, 2:57:30 AM4/24/17
to ParaSail Programming Language, tuc...@yaletaft.com
I challenge the assertion from dpt that the fizzbuzz program in scala is meritorious. I find that the matrix of [true, false], requires the reader that they remember upwards context, and memorize which one came first. I don't think the scala program is easier to read compared to ye olde modula2

FOR i := 1 TO 30 DO 
  IF i MOD 3 = 0 THEN
    print "fizz"
    END;

  IF i MOD 5 = 0 THEN
     print "buzz"
     END;
END; 

to me this is an extremely clear program, and i can see by close physical promixity that the test for MOD 3 corresponds to fizz. In the scala example, the meta-variables of "true" which is the implied result of the boolean test has to be remembered. I think it is easy in this case to show that the cognitive load placed on the reader is noticeably higher in the scala program compared to the old fashioned way (which could have been done in FORTRAN for goodness' sake just as clearly.

i think there are more important areas where parasail can be improved. perhaps i am picking on a straw man, a weak example. Pattern matching is an interesting area. Longer, more complex patterns as in parsing, regular expressions, etc. might be much more fruitful. To my own thinking finite state machines are an excellent area to attack, as inevitably one creates a FSM in your code somewhere. One can visualize the entire user interface of a product as a FSM.

Reply all
Reply to author
Forward
0 new messages