Unused functions.

Pierpaolo BERNARDI <>
Wed Feb 5 19:48:46 CET 2003


From: "Matthias Lang" <>

>  > I have a bunch of functions that are called only via apply.
>  > The compiler warns that these functions are unused and
>  > removes them.
>
> Apply can only call exported functions.
>
> The compiler is doing the right thing. It is removing functions which
> you cannot possibly call. See also the manual entry about 'apply':

Ah, yes, it explicitly says that only exported functions can by apply'ed.
I had missed this bit.  Thanks (and thanks to Francesco who points out
the same thing).

And, on a second though, it makes perfect sense from the point of view
of a compiler trying to optimize code.

From: "Bengt Kleberg" <>

> there is a suggestion from  to not use apply at all.
> see his new erlang standard library packages. for those that do not
> have them i will quoute from the CONVENTIONS file:
>
> - Don't use apply(Module, Function, Arguments)
>   unless the Arguments list actually *can* vary
>   arbitrarily in length. (This is rarely the case.)
>   Instead, use:
>
> Module:Function(Arg1, ... ArgN)
>
>   Where Module and/or Function is a variable.
>   (This is *much* more efficient than 'apply'.)

Speed is not at all important in my case, but all the functions
are in fact constants (0 args), so I can apply (oops) this suggestion.

In my case I could also use a case which explicitly calls the right
function given the atom, but I think the more compact form is better
maintainable.

I'm attaching the code at bottom.  As you can see it's just a toy,
though, not on this ml, but in other forums it can come handy.

>   Never use erlang:apply/2. (If you did not know it
>   existed, then just forget about it again.)

Too late.  I'm already contaminated by decades of Lisp.

P.


%%%----------------------------------------------------------------------
%%% File    : non-sequitur.erl
%%% Author  :  <>
%%% Purpose :
%%% Created :  2 Feb 2003 by  <>
%%%----------------------------------------------------------------------

-module(non_sequitur).
-author('').

-compile(export_all).
-export([flame/0,
  flame/1]).

%%% "Flame" program.  This has a chequered past.
%%%
%%% The original was on a Motorola 286 running Vanilla V.1,
%%% about 2 years ago.  It was couched in terms of a yacc (I think]
%%% script.  I pulled the data out of it and rewrote it as a piece
%%% of PL/1 on Multics.  Now I've moved it into an emacs-lisp
%%% form.  If the original author cares to contact me, I'd
%%% be very happy to credit you!
%%%
%%% Ian G. Batten, 
%%%
%%% Ported to Common Lisp and Zmacs by Jamie Zawinski
<>.
%%% Ported to Erlang by Pierpaolo Bernardi <>
2003-02-02.

sentence() ->
    [["how can you say that ", statement, "?"],
     ["I can't believe how ", adjective, " you are."],
     ["only a ", der_term, " like you would say that ", statement, "."],
     [statement, ", huh?"],
     ["so, ", statement, "?"],
     [statement, ", right?"],
     ["I mean, ", sentence],
     ["don't you realise that ", statement, "?"],
     ["I firmly believe that ", statement, "."],
     ["let me tell you something, you ", der_term, ", ", statement, "."],
     ["furthermore, you ", der_term, ", ", statement, "."],
     ["I couldn't care less about your ", thing, "."],
     ["How can you be so ", adjective, "?"],
     ["you make me sick."],
     ["it's well known that ", statement, "."],
     [statement, "."],
     ["it takes a ", group_adj, " ", der_term, " like you to say that ",
      statement, "."],
     ["I don't want to hear about your ", thing, "."],
     ["you're always totally wrong."],
     ["I've never heard anything as ridiculous as the idea that ",
statement, "."],
     ["you must be a real ", der_term, " to think that ", statement, "."],
     ["you ", adjective, " ", group_adj, " ", der_term, "!"],
     ["you're probably ", group_adj, " yourself."],
     ["you sound like a real ", der_term, "."],
     ["why, ", statement, "!"],
     ["I have many ", group_adj, " friends."],
     ["save the ", thing, "s!"],
     ["no nukes!"],
     ["ban ", thing, "s!"],
     ["I'll bet you think that ", thing, "s are ", adjective, "."],
     ["you know, ", statement, "."],
     ["your ", quality, " reminds me of a ", thing, "."],
     ["you have the ", quality, " of a ", der_term, "."],
     [der_term, "!"],
     [adjective, " ", group_adj, " ", der_term, "!"],
     ["you're a typical ", group_adj, " person, totally ", adjective, "."],
     ["man, ", sentence]
    ].

quality() ->
    ["ignorance", "stupidity", "worthlessness", "prejudice",
     "lack of intelligence", "lousiness", "bad grammar",
     "lousy spelling", "lack of common decency", "ugliness",
     "nastiness", "subtlety", "dishonesty",
     [adjective, " ", quality]].

adjective() ->
    ["ignorant", "crass", "pathetic", "sick",
     "bloated", "malignant", "perverted", "sadistic",
     "stupid", "unpleasant", "lousy", "abusive", "bad",
     "braindamaged", "selfish", "improper", "nasty",
     "disgusting", "foul", "intolerable", "primitive",
     "depressing", "dumb", "phoney",
     [adjective, " and ", adjective],
     ["as ", adjective, " as a ", thing]
    ].

der_term() ->
    [[adjective, " ", der_term], "sexist", "fascist",
     "weakling", "coward", "beast", "peasant", "racist",
     "cretin", "fool", "jerk", "ignoramus", "idiot",
     "wanker", "rat", "slimebag", "DAF driver",
     "Neanderthal", "sadist", "drunk", "capitalist",
     "wimp", "dogmatist", "wally", "maniac",
     "whimpering scumbag", "pea brain", "arsehole",
     "moron", "goof", "incompetant", "lunkhead", "Nazi",
     "SysThug", [der_term, " ", der_term]].

thing() ->
    [[adjective, " ", thing], "computer",
     "Honeywell dps8", "whale", "operation",
     "sexist joke", "ten-incher", "dog", "MicroVAX II",
     "source license", "real-time clock",
     "mental problem", "sexual fantasy",
     "venereal disease", "Jewish grandmother",
     "cardboard cut-out", "punk haircut", "surfboard",
     "system call", "wood-burning stove",
     "graphics editor", "right wing death squad",
     "disease", "vegetable", "religion",
     "cruise missile", "bug fix", "lawyer", "copyright",
     "PAD"].

group_adj() ->
    ["gay", "old", "lesbian", "young", "black",
     "Polish", adjective, "white",
     "mentally retarded", "Nicaraguan", "homosexual",
     "dead", "underpriviledged", "religious",
     [thing, "-loving"], "feminist", "foreign",
     "intellectual", "crazy", "working", "unborn",
     "Chinese", "short", adjective, "poor", "rich",
     "funny-looking", "Puerto Rican", "Mexican",
     "Italian", "communist", "fascist", "Iranian",
     "Moonie"].

statement() ->
    [["your ", thing, " is great"],
     [thing, "s are fun"],
     [person, " is a ", der_term],
     [group_adj, " people are ", adjective],
     ["every ", group_adj, " person is a ", der_term],
     ["most ", group_adj, " people have ", thing, "s"],
     ["all ", group_adj, " dudes should get ", thing, "s"],
     [person, " is ", group_adj],
     ["trees are ", adjective],
     ["if you've seen one ", thing, ", you've seen them all"],
     ["you're ", group_adj],
     ["you have a ", thing],
     ["my ", thing, " is pretty good"],
     ["the Martians are coming"],
     ["the ", paper, " is always right"],
     ["just because you read it in the ", paper, " that doesn't mean it's
true"],
     [person, " was ", group_adj],
     [person, "'s ghost is living in your ", thing],
     ["you look like a ", thing],
     ["the oceans are full of dirty fish"],
     ["people are dying every day"],
     ["a ", group_adj, " man ain't got nothing in the world these days"],
     ["women are inherently superior to men"],
     ["the system staff is fascist"],
     ["there is life after death"],
     ["the world is full of ", der_term, "s"],
     ["you remind me of ", person],
     ["technology is evil"],
     [person, " killed ", person],
     ["the Russians are tapping your phone"],
     ["the Earth is flat"],
     ["it's OK to run down ", group_adj, " people"],
     ["Multics is a really ", adjective, " operating system"],
     ["the CIA killed ", person],
     ["the sexual revolution is over"],
     ["Lassie was ", group_adj],
     ["the ", group_adj, "s have really got it all together"],
     ["I was ", person, " in a previous life"],
     ["breathing causes cancer"],
     ["it's fun to be really ", adjective],
     [quality, " is pretty fun"],
     ["you're a ", der_term],
     ["the ", group_adj, " culture is fascinating"],
     ["when ya gotta go ya gotta go"],
     [person, " is ", adjective],
     [person, "'s ", quality, " is ", adjective],
     ["it's a wonderful day"],
     ["everything is really a ", thing],
     ["there's a ", thing, " in ", person, "'s brain"],
     [person, " is a cool dude"],
     [person, " is just a figment of your imagination"],
     ["the more ", thing, "s you have, the better"],
     ["life is a ", thing],
     ["life is ", quality],
     [person, " is ", adjective],
     [group_adj, " people are all ", adjective, " ", der_term, "s"],
     [statement, ", and ", statement],
     [statement, ", but ", statement],
     ["I wish I had a ", thing],
     ["you should have a ", thing],
     ["you hope that ", statement],
     [person, " is secretly ", group_adj],
     ["you wish you were ", group_adj],
     ["you wish you were a ", thing],
     ["I wish I were a ", thing],
     ["you think that ", statement],
     [statement, ", because ", statement],
     [group_adj, " people don't get married to ", group_adj,
      " people because ", reason],
     [group_adj, " people are all ", adjective, " because ", reason],
     [group_adj, " people are ", adjective, ", and ", reason],
     ["you must be a ", adjective, " ", der_term, " to think that ",
      person, " said ", statement],
     [group_adj, " people are inherently superior to ", group_adj, "
people"],
     ["God is Dead"]].

paper() ->
    ["Daily Mail", "Daily Express",
     "Centre Bulletin", "Sun", "Daily Mirror",
     "Daily Telegraph", "Beano", "Multics Manual"].

person() ->
    ["Reagan", "Ken Thompson", "Dennis Ritchie",
     "JFK", "the Pope", "Gadaffi", "Napoleon",
     "Karl Marx", "Groucho", "Michael Jackson",
     "Caesar", "Nietzsche", "Heidegger",
     "Henry Kissinger", "Nixon", "Castro", "Thatcher",
     "Attilla the Hun", "Alaric the Visigoth", "Hitler"].

reason() ->
    ["they don't want their children to grow up to be too lazy to steal",
     ["they can't tell them apart from ", group_adj, " dudes"],
     ["they're too ", adjective],
     [person, " wouldn't have done it"],
     "they can't spray paint that small",
     ["they don't have ", thing, "s"],
     "they don't know how",
     ["they can't afford ", thing, "s"]].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

capitalize([C|Cs]) -> [capitalize(C)|Cs];
capitalize(C) when integer(C), C >= $a, C =< $z -> C - $a + $A;
capitalize(C) -> C.

random_elt(L) -> lists:nth(random:uniform(length(L)),L).

stringp([X|Xs]) when integer(X), X >= 0, X =< 255 -> stringp(Xs);
stringp([]) -> true;
stringp(_) -> false.

flame() -> capitalize(lists:flatten(expand(sentence))).

flame(0) -> [];
flame(N) when N > 0 -> [flame()|flame(N-1)].

expand(S) when atom(S) ->
    %%expand(random_elt(apply(non_sequitur,S,[])));
    expand(random_elt(non_sequitur:S()));
expand(X) ->
    S = stringp(X),
    case S of
 true -> X;
 false -> lists:map(fun (Q) -> expand(Q) end,X)
    end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

bigflame(N) -> bigflame(N,0,"").

bigflame(0,Lun,S) -> {Lun,S};
bigflame(N,Lun,S) ->
    F = flame(),
    L = length(F),
    if L > Lun -> bigflame(N-1,L,F);
       true -> bigflame(N-1,Lun,S)
    end.





More information about the erlang-questions mailing list