Yuce's Blog

Cognitive dissonance is yoga for the brain.*

99 Bottles of Beer in Marvin3


How to write 99 bottles of beer in Marvin3?

:bottles  dup dup 1 > [" bottles"] [" bottle"] ifte cat ;

    bottles dup " of beer on the wall," cat println
    " of beer. " cat println
    "Take one down, pass it around." println ;

:theend  "No more beer left." println ;

:beers  [dup 0 >] [sing decr] while theend eat ;

# start singing
99 beers

Also you may check a previous post for the original Marvin. Other languages? 99 Bottles of Beer on the Wall lists many. See 99 Bottles of Beer on Wikipedia.

marvin3, marvin, 99 bottles

Marvin3 is launched


Marvin3 is a new cross-platform stack-based language developed in Python. Marvin3 features object-oriented-pro­gram­ming, anonymous routines, eager lists, variables, lexical scope, com­bi­na­tors and support for modules.

This is from my Marvin3 project home. Marvin has evolved into Marvin3 and is becoming ready to be tried by the audience. In order to make it better known, and because it is free software, I requested a Source­Forge account yesterday, and to my surprise my request was approved in a couple of hours (while I was led to believe that it would take two days!!!). Well, I am happy for that, and I released Marvin3 0.0.6 which has some continue.

marvin3, marvin

Accumulator Generator


In his accumulator generator page, Paul Graham compares several languages to solve a particular problem that is quoted below (apparently, he has chosen this example in favor of his new Lisp-like language Arc):

The problem: Write a function foo that takes a number n and returns a function that takes a number i, and returns n incremented by i.

Note: (a) that's number, not integer, (b) that's incremented by, not plus.

My trial with Marvin was a failure, and could not satisfy all the 5 requirements given here. But now with Marvin2 (a rewrite of the language) this can be accomplished with the following line:


marvin, accumulator generator, Paul Graham

Infix Comparison in Marvin


Here is a sample Marvin code that converts an infix expression into postfix. I have also posted this one to the In­fix­Com­par­i­son page of Con­cate­na­tive Languages Wiki. Note from the future: Sadly, these links are broken.

# infix.mar | Converts an infix expression into postfix

# This implementation uses the global variable space as a hash-table
# for operator precedence.

"listop forth dequeop" use

@|> globs & pushfrom ;

@string dfront NONE =
    { iftrue dpushf }
    { else
        dpopf 2dup |>
        { iftrue swap dpushf }

marvin, infix

Infix to Postfix Converter Class in Marvin


Here is an object oriented version of the infix to postfix converter written in Marvin:

# infixoop.mar | Converts an infix expression into postfix

# This implementation uses the object data as a hash-table
#    for operator precedence.

"listop forth dequeop" use

    @new  # define the hash-table for operator precedence
        FALSE ^++ ^+- ^-+ ^-- ^*+ ^*- ^** ^*/ ^/+ ^/- ^/* ://
        TRUE ^+* ^+/ ^-* :-/ me ;

    @|> & pushfrom ;

    @string dfront NONE 

marvin, infix, postfix

99 Bottles of Beer on the Wall


The following is one of the ways to do 99 bottles of beer in Marvin. If you don't know what I mean, check 99 Bottles of Beer on the Wall.

99 ^beers
%beers 0 >
    " bottle(s) of beer" & dup " on the wall," & println
    "." & println
    "Take one down, pass it around," println
    %beers -- ^beers

"No more beers left." println

marvin, 99 bottles

Currying in Marvin


Yesterday, I figured out a way (which was very obvious) to curry a routine in Marvin. First of all, what's currying?

Currying is named after the logician Haskell Curry. (...) The underlying insight of "currying" is that it is possible to treat (almost) every function as a partial function of just one argument. All that is necessary for currying to work is to allow the return value of functions to themselves be functions, but with the returned functions narrowed or closer to completion. (...) each successive call to a curried return function fills in more of the data involved in a final com­pu­ta­tion (data attached continue.

marvin, currying

Another Currying Example in Marvin


Please see my previous post before if you haven't done so.

I've quoted David Mertz's article Charming Python in my previous post. In that article, (besides other things) a Haskell function is curryied as:

computation a b c d = (a + b^2+ c^3 + d^4)

Then it is filled as follows:

fillOne   = computation 1
fillTwo   = fillOne 2
fillThree = fillTwo 3
answer    = fillThree 5

and the answer is 657. The same thing can be done in Marvin:

[ swap 2 ** + swap 3 ** + swap 4 ** + ] :computation
[ 1 \computation ] :fillOne
[ 2 \fillOne ] 

marvin, currying