[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

name this function

Grumpus

6/2/2016 12:55:00 PM

Is there a common name for the function that takes an item and a list
and places the item between adjacent list elements, like this:

(foo '-- (list 1 2 3))
=> (1 -- 2 -- 3)

15 Answers

Dan Sommers

6/2/2016 12:57:00 PM

0

On Thu, 02 Jun 2016 12:55:08 +0000, Grumpus wrote:

> Is there a common name for the function that takes an item and a list
> and places the item between adjacent list elements, like this:
>
> (foo '-- (list 1 2 3))
> => (1 -- 2 -- 3)

intersperse?

https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-List.html#v:i...

HTH,
Dan

ram

6/2/2016 12:57:00 PM

0

Grumpus <grumpus@example.org> writes:
>Is there a common name for the function that takes an item and a list
>and places the item between adjacent list elements, like this:
>(foo '-- (list 1 2 3))
>=> (1 -- 2 -- 3)

JavaScript:

[ 1, 2, 3 ].join( "--" )
"1--2--3"

Kaz Kylheku

6/2/2016 1:47:00 PM

0

On 2016-06-02, Dan Sommers <dan@tombstonezero.net> wrote:
> On Thu, 02 Jun 2016 12:55:08 +0000, Grumpus wrote:
>
>> Is there a common name for the function that takes an item and a list
>> and places the item between adjacent list elements, like this:
>>
>> (foo '-- (list 1 2 3))
>> => (1 -- 2 -- 3)
>
> intersperse?
>
> https://hackage.haskell.org/package/base-4.9.0.0/docs/Data-List.html#v:i...

Called "interpose" in Clojure and Emacs Lisp "dash" package.

rpw3

6/2/2016 1:53:00 PM

0

Grumpus <grumpus@example.org> wrote:
+---------------
| Is there a common name for the function that takes an item and a list
| and places the item between adjacent list elements, like this:
|
| (foo '-- (list 1 2 3))
| => (1 -- 2 -- 3)
+---------------

Perl (and some other languages) call that "join":

http://perl.about.com/od/perltutorials/qt/pe...
...
$STRING = join(JOINER, @LIST);

I hacked up a variation in CL with the arguments swapped,
thus allowing the "joiner" arg to be optional, defaulting
to #\SPACE, and named it JOIN-STRINGS:

> (join-strings '("foo" "bar" "baz"))

"foo bar baz"
> (join-strings '("foo" "bar" "baz") ":")

"foo:bar:baz"
> (join-strings '("foo" "bar" "baz") " <==> ")
"foo <==> bar <==> baz"
>

The source of only a half-dozen lines,
so is left as an exercise for the reader... ;-}


-Rob

-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

Barry Margolin

6/2/2016 2:38:00 PM

0

In article <57503a3f$0$45004$742ec2ed@news.sonic.net>,
rpw3@rpw3.org (Rob Warnock) wrote:

> Grumpus <grumpus@example.org> wrote:
> +---------------
> | Is there a common name for the function that takes an item and a list
> | and places the item between adjacent list elements, like this:
> |
> | (foo '-- (list 1 2 3))
> | => (1 -- 2 -- 3)
> +---------------
>
> Perl (and some other languages) call that "join":

That's the name in several languages, but in all cases it also turns the
list/array into a string as well. So it's related, but this is a
significant difference. The way this name should generally be understood
is that it's joining the elements together into a string, with a
delimiter also interspersed.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Madhu

6/2/2016 4:47:00 PM

0


* Kaz Kylheku <20160602064215.443@kylheku.com> :
Wrote on Thu, 2 Jun 2016 13:46:45 +0000 (UTC):

| On 2016-06-02, Dan Sommers <dan@tombstonezero.net> wrote:
|> On Thu, 02 Jun 2016 12:55:08 +0000, Grumpus wrote:
|>
|>> Is there a common name for the function that takes an item and a list
|>> and places the item between adjacent list elements, like this:
|>>
|>> (foo '-- (list 1 2 3))
|>> => (1 -- 2 -- 3)

The string function for this in emacs was mapconcat.

(mapconcat 'identity '("1" "2" "3") "-") ;=> "1-2-3"

Over the years, a few mapconcat versions have been posted on cll, which
generalized mapconcat for cl's sequences including lists. Eg. Vassil
Nikolov's 2009 post snzocro7wsm.fsf@luna.vassil.nikolov.name , You'd
call it like this:

(map-concatenate 'list 'list '(1 2 3) '(--))

Here is my reference implementation

(defun map-concatenate (result-type function sequence separator &key (start 0)
end from-end)
(reduce (lambda (&rest rest)
(when rest
(destructuring-bind (a b) rest
(concatenate result-type a separator b))))
sequence :key function :start start :end end :from-end from-end))

|>
|> intersperse?
|
| Called "interpose" in Clojure and Emacs Lisp "dash" package.

I used to think this looked like a trainwreck but it looks more properly
like demon spawn of pcase (along with its cousing seq in the main git
repo) after pcase ascended out of the bottomless pit.

rpw3

6/2/2016 5:41:00 PM

0

Barry Margolin <barmar@alum.mit.edu> wrote:
+---------------
| rpw3@rpw3.org (Rob Warnock) wrote:
| > Perl (and some other languages) call that "join":
|
| That's the name in several languages, but in all cases it also
| turns the list/array into a string as well. So it's related,
| but this is a significant difference. The way this name should
| generally be understood is that it's joining the elements
| together into a string, with a delimiter also interspersed.
+---------------

That's why I called my CL version JOIN-STRINGS, reserving
the name JOIN [as yet unwritten] for the more general one
you refer to, analogous to the MAPCAR vs. MAP distinction.
Like MAP and CONCATENATE, perhaps a CL JOIN should specify
a result-type. Then it could be used for either the OP's
request exactly as stated:

(join 'list '(1 2 3) :separator '--) => (1 -- 2 -- 3)

*or* the more common Perl-like string version [which my
JOIN-STRINGS does]:

(join 'string '("foo" "bar" "baz") :separator ", ")
=> "foo, bar, baz"

The interesting question for me is the extent to which such
a JOIN should attempt to do automatic coercions to (subsets of)
the result-type. That is, should these "just work"?

(join 'string '(1 2 3) :separator '--) => "1--2--3"

(join 'string '(foo bar baz) :separator '|, |) => "foo, bar, baz"


-Rob

p.s. And then there are other "small details" such as the
default separator. If the result-type is STRING, then
#\space or " " seems reasonable. But what if result-type
is LIST? What is a reasonable default separator? NIL or ()?
[Meaning actually *insert* a NIL.] Null/nothing? [Meaning
insert nothing, just return the list arg.]

-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <http://rpw...
San Mateo, CA 94403

Pascal J. Bourguignon

6/2/2016 6:40:00 PM

0

Grumpus <grumpus@example.org> writes:

> Is there a common name for the function that takes an item and a list
> and places the item between adjacent list elements, like this:
>
> (foo '-- (list 1 2 3))
> => (1 -- 2 -- 3)

I would name it INTERSPERCE. However, I have an old function named
LIST-INSERT-SEPARATOR:


cl-user> (list-insert-separator (list 1 2 3) '--)
(1 -- 2 -- 3)


There's also mapconcat, but only for strings:

cl-user> (mapconcat 'identity (list "a" "b" "c") "--")
"a--b--c"


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

William James

6/2/2016 8:21:00 PM

0

Pascal J. Bourguignon wrote:

> There's also mapconcat, but only for strings:
>
> cl-user> (mapconcat 'identity (list "a" "b" "c") "--")
> "a--b--c"

OCaml:

String.concat "--" ["a";"b";"c"];;
===>
"a--b--c

--
Today Dr. Duke quoted an article from Breitbart on an agreement between the EU
and internet companies like Facebook, YouTube, and Twitter to effectively
censor "hate speech," which of course is anything Jews hate to hear.
http://www.renseradioarchives....

Ben Bacarisse

6/3/2016 10:12:00 AM

0

Grumpus <grumpus@example.org> writes:

> Is there a common name for the function that takes an item and a list
> and places the item between adjacent list elements, like this:
>
> (foo '-- (list 1 2 3))
> => (1 -- 2 -- 3)

Hunting for a one-liner, the best I could do is this:

(cdr (mapcan #'list (let ((s '(~))) (rplacd s s)) '(1 2 3)))

(Just a curiosity, not really an answer.)

--
Ben.