Eric Florenzano’s Programming Meme in Prolog

by Sebastian Benthall

I was pointed to Eric Florenzano’s Programming Meme today. I decided to take up the challenge with a language I’ve always wanted to learn: Prolog!

A few hours later, and I got something that works. I can’t say it’s at all elegant, but it’s late and at this point I’m just happy I figured out the language’s I/O. Any pointers on Prolog are welcome.

Without further ado, here’s the meme:

Rules:

1. Implement a program that takes in a user’s name and their age, and prints hello to them once for every year that they have been alive.
2. Post these rules, the source code for your solution, and the following list (with you included) on your blog.
3. Bonus points if you implement it in a language not yet seen on the following list!

The List:

1. [Python] http://www.eflorenzano.com/blog/post/trying-start-programming-meme
2. [Bash] http://aartemenko.com/texts/bash-meme/
3. [C] http://dakrauth.com/media/site/text/hello.c
4. [Java] http://adoleo.com/blog/2008/nov/25/programming-meme/
5. [Python 3] http://mikewatkins.ca/2008/11/25/hello-meme/
6. [Ruby] http://stroky.l.googlepages.com/gem
7. [Ruby] http://im.camronflanders.com/archive/meme/
8. [Lisp] http://justinlilly.com/blog/2008/nov/25/back-on-the-horse/
9. [Lua] http://aartemenko.com/texts/lua-hello-meme/
10. [Functional Python] http://aartemenko.com/texts/python-functional-hello-meme/
11. [Erlang] http://surfacedepth.blogspot.com/2008/11/erics-programming-meme-in-erlang.html
12. [Haskell] http://jasonwalsh.us/meme.html
13. [PHP] http://fitzgeraldsteele.wordpress.com/2008/11/25/memeing-in-php-2/
14. [Javascript] http://www.taylanpince.com/blog/posts/responding-to-a-programming-meme/
15. [Single-File Django] http://www.pocketuniverse.ca/archive/2008/november/27/florenzano-factor/
16. [Prolog] https://digifesto.wordpress.com/2009/11/17/eric-florenzanos-programming-meme-in-prolog/

The Prolog solution:

getout(Name,0,[]).

getout(Name,N,Output) :-
        N > 0,
        N1 is N-1,
        Output = [[N, ') Hello, ', Name, '\n'] | Output1],
        getout(Name,N1,Output1).

doit :-
        write('What is your name? Remember to put a period after it, and put it in quotes if there are spaces.'), nl,
        read(Name),
        write('How old are you?  You\'re going to want to enter a number then a period here.'), nl,
        read(Age),
        getout(Name,Age,W  ),
        reverse(W,X),
        append(X,Z),
        concat_atom(Z, Out),
        write(Out).

Here’s an example of running it with SWI-Prolog:

?- ['meme.pro'].
Warning: /home/sb/meme.pro:2:
	Singleton variables: [Name]
% meme.pro compiled 0.00 sec, 8 bytes
true.

?- doit.
What is your name? Remember to put a period after it, and put it in quotes if there are spaces.
|    'Sebastian Benthall'.
How old are you?  You're going to want to enter a number then a period here.
|    24.
1) Hello, Sebastian Benthall
2) Hello, Sebastian Benthall
3) Hello, Sebastian Benthall
4) Hello, Sebastian Benthall
5) Hello, Sebastian Benthall
6) Hello, Sebastian Benthall
7) Hello, Sebastian Benthall
8) Hello, Sebastian Benthall
9) Hello, Sebastian Benthall
10) Hello, Sebastian Benthall
11) Hello, Sebastian Benthall
12) Hello, Sebastian Benthall
13) Hello, Sebastian Benthall
14) Hello, Sebastian Benthall
15) Hello, Sebastian Benthall
16) Hello, Sebastian Benthall
17) Hello, Sebastian Benthall
18) Hello, Sebastian Benthall
19) Hello, Sebastian Benthall
20) Hello, Sebastian Benthall
21) Hello, Sebastian Benthall
22) Hello, Sebastian Benthall
23) Hello, Sebastian Benthall
24) Hello, Sebastian Benthall
Advertisements