[erlang-questions] Proposal: add lists:intersperse/2 and lists:intercalate/2

Richard A. O'Keefe ok@REDACTED
Tue Mar 8 01:24:16 CET 2016


On 8/03/16 5:59 am, Siraaj Khandkar wrote:

> Both of the above results are functions of 2 sequences, the difference 
> is that 1st works with a pre-computed list, while the 2nd works with a 
> an infinite stream of 0s.

(A) No.  0 is not an infinite stream of 0s.
(B) Erlang doesn't have infinite lists.
(C) Haskell *does* have infinite lists, but you still have to
    write cycle [0] to get one.
(D) I was discussing *specific* code definitions:

intersperse(Sep, [X|Xs]) -> [X | intersperse_(Xs)];
intersperse(_, []) -> [].

intersperse_(Sep, [X|Xs]) -> [Sep,X | intersperse_(Xs)];
intersperse_(_, []) -> [].

interleave([X|Xs], [Y|Ys]) -> [X,Y | interleave(Xs, Ys)];
interleave([], []) -> [].

which have quite distinct interfaces.  interleave/2 takes two
list(T) sequences; intersperse/2 takes a T and a list(T).

Someone raised the issue of finding functions.
It's actually quite hard in most programming languages to guess
the names of most built-in functions from their meaning.
For example, who would have guess that "read a line from
standard input" was called gets() in C?

Haskell has a web interface where you can give the *type* of
a function and get a list of well-known functions with that
type.  Squeak has (or had, I haven't updated in a while) an
IDE interface where you can give an input-output *example*
of a function and get a list of well-known functions that give
that output for that input.  It might be a good idea to do
that for at least the Erlang lists module, so that you could
do function_finder:find(",", ["a","b"], "a,b")
==> lists:intercalate/2
and function_finder:find([d,r,e,a,d], [a,d,d,e,r])
==> lists:sort/1




More information about the erlang-questions mailing list