Permutations of a given string

N

Nick Keighley

Write a program to print all the permutations of a given string.

(define (permutations s)
(if (null? s)
(list nil)
(flatmap (lambda (x)
(map (lambda (p) (cons x p))
(permutaions (remove x s))))
s)))

[SICP p124]
 
K

Keith Thompson

Eric Sosman said:
#define GIVEN_STRING ""
int main(void) {
return 0;
}

The empty string has one permutation.

Oh, I see, you didn't print a new-line after printing the permutation.

Which actually raises a nearly interesting issue. C99 7.19.2p2 says:

A text stream is an ordered sequence of characters composed into
_lines_, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

It's not clear from this wording whether an empty stream, consisting
of zero lines, is valid. I have no doubt that it was intended to be
the case; this is merely an issue about the wording in the standard.
 
V

viza

Which actually raises a nearly interesting issue. C99 7.19.2p2 says:

A text stream is an ordered sequence of characters composed into
_lines_, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

It's not clear from this wording whether an empty stream, consisting of
zero lines, is valid. I have no doubt that it was intended to be the
case; this is merely an issue about the wording in the standard.

Your inference that an empty stream contains zero lines is only valid on
implementations that define a line as requiring a newline character.
Under implementations which define the final newline as optional or
forbidden, the empty (zero-character) stream may contain 1 line.

In any case if you are going to be picky over an entirely academic point,
the standard you quote above says "lines" not "line or lines", so one
could argue that a one-line stream was invalid. Even discussing it would
be a bit sad though...

viza
 
K

Keith Thompson

viza said:
Your inference that an empty stream contains zero lines is only valid on
implementations that define a line as requiring a newline character.
Under implementations which define the final newline as optional or
forbidden, the empty (zero-character) stream may contain 1 line.

In any case if you are going to be picky over an entirely academic point,
the standard you quote above says "lines" not "line or lines", so one
could argue that a one-line stream was invalid.

Yes, you're right; I was going to mention that.
Even discussing it would
be a bit sad though...

Why? What's wrong with discussing an opportunity to improve the
wording of the standard (apart from the fact that it would be more
appropriate i comp.std.c)?
 
L

lawrence.jones

Keith Thompson said:
It's not clear from this wording whether an empty stream, consisting
of zero lines, is valid. I have no doubt that it was intended to be
the case; this is merely an issue about the wording in the standard.

There's a clue in 7.19.3p4:

Whether a file of zero length (on which no characters have been
written by an output stream) actually exists is implementation-
defined.

-- Larry Jones

It's not denial. I'm just very selective about the reality I accept.
-- Calvin
 
R

Ron Ford

saki said:


Okay.

ginrst ginrts ginsrt ginstr gintrs gintsr girnst girnts girsnt girstn
girtns girtsn gisnrt gisntr gisrnt gisrtn gistnr gistrn gitnrs gitnsr
gitrns gitrsn gitsnr gitsrn gnirst gnirts gnisrt gnistr gnitrs gnitsr
gnrist gnrits gnrsit gnrsti gnrtis gnrtsi gnsirt gnsitr gnsrit gnsrti
gnstir gnstri gntirs gntisr gntris gntrsi gntsir gntsri grinst grints
grisnt gristn gritns gritsn grnist grnits grnsit grnsti grntis grntsi
grsint grsitn grsnit grsnti grstin grstni grtins grtisn grtnis grtnsi
grtsin grtsni gsinrt gsintr gsirnt gsirtn gsitnr gsitrn gsnirt gsnitr
gsnrit gsnrti gsntir gsntri gsrint gsritn gsrnit gsrnti gsrtin gsrtni
gstinr gstirn gstnir gstnri gstrin gstrni gtinrs gtinsr gtirns gtirsn
gtisnr gtisrn gtnirs gtnisr gtnris gtnrsi gtnsir gtnsri gtrins gtrisn
gtrnis gtrnsi gtrsin gtrsni gtsinr gtsirn gtsnir gtsnri gtsrin gtsrni
ignrst ignrts ignsrt ignstr igntrs igntsr igrnst igrnts igrsnt igrstn
igrtns igrtsn igsnrt igsntr igsrnt igsrtn igstnr igstrn igtnrs igtnsr
igtrns igtrsn igtsnr igtsrn ingrst ingrts ingsrt ingstr ingtrs ingtsr
inrgst inrgts inrsgt inrstg inrtgs inrtsg insgrt insgtr insrgt insrtg
instgr instrg intgrs intgsr intrgs intrsg intsgr intsrg irgnst irgnts
irgsnt irgstn irgtns irgtsn irngst irngts irnsgt irnstg irntgs irntsg
irsgnt irsgtn irsngt irsntg irstgn irstng irtgns irtgsn irtngs irtnsg
irtsgn irtsng isgnrt isgntr isgrnt isgrtn isgtnr isgtrn isngrt isngtr
isnrgt isnrtg isntgr isntrg isrgnt isrgtn isrngt isrntg isrtgn isrtng
istgnr istgrn istngr istnrg istrgn istrng itgnrs itgnsr itgrns itgrsn
itgsnr itgsrn itngrs itngsr itnrgs itnrsg itnsgr itnsrg itrgns itrgsn
itrngs itrnsg itrsgn itrsng itsgnr itsgrn itsngr itsnrg itsrgn itsrng
ngirst ngirts ngisrt ngistr ngitrs ngitsr ngrist ngrits ngrsit ngrsti
ngrtis ngrtsi ngsirt ngsitr ngsrit ngsrti ngstir ngstri ngtirs ngtisr
ngtris ngtrsi ngtsir ngtsri nigrst nigrts nigsrt nigstr nigtrs nigtsr
nirgst nirgts nirsgt nirstg nirtgs nirtsg nisgrt nisgtr nisrgt nisrtg
nistgr nistrg nitgrs nitgsr nitrgs nitrsg nitsgr nitsrg nrgist nrgits
nrgsit nrgsti nrgtis nrgtsi nrigst nrigts nrisgt nristg nritgs nritsg
nrsgit nrsgti nrsigt nrsitg nrstgi nrstig nrtgis nrtgsi nrtigs nrtisg
nrtsgi nrtsig nsgirt nsgitr nsgrit nsgrti nsgtir nsgtri nsigrt nsigtr
nsirgt nsirtg nsitgr nsitrg nsrgit nsrgti nsrigt nsritg nsrtgi nsrtig
nstgir nstgri nstigr nstirg nstrgi nstrig ntgirs ntgisr ntgris ntgrsi
ntgsir ntgsri ntigrs ntigsr ntirgs ntirsg ntisgr ntisrg ntrgis ntrgsi
ntrigs ntrisg ntrsgi ntrsig ntsgir ntsgri ntsigr ntsirg ntsrgi ntsrig
rginst rgints rgisnt rgistn rgitns rgitsn rgnist rgnits rgnsit rgnsti
rgntis rgntsi rgsint rgsitn rgsnit rgsnti rgstin rgstni rgtins rgtisn
rgtnis rgtnsi rgtsin rgtsni rignst rignts rigsnt rigstn rigtns rigtsn
ringst ringts rinsgt rinstg rintgs rintsg risgnt risgtn risngt risntg
ristgn ristng ritgns ritgsn ritngs ritnsg ritsgn ritsng rngist rngits
rngsit rngsti rngtis rngtsi rnigst rnigts rnisgt rnistg rnitgs rnitsg
rnsgit rnsgti rnsigt rnsitg rnstgi rnstig rntgis rntgsi rntigs rntisg
rntsgi rntsig rsgint rsgitn rsgnit rsgnti rsgtin rsgtni rsignt rsigtn
rsingt rsintg rsitgn rsitng rsngit rsngti rsnigt rsnitg rsntgi rsntig
rstgin rstgni rstign rsting rstngi rstnig rtgins rtgisn rtgnis rtgnsi
rtgsin rtgsni rtigns rtigsn rtings rtinsg rtisgn rtisng rtngis rtngsi
rtnigs rtnisg rtnsgi rtnsig rtsgin rtsgni rtsign rtsing rtsngi rtsnig
sginrt sgintr sgirnt sgirtn sgitnr sgitrn sgnirt sgnitr sgnrit sgnrti
sgntir sgntri sgrint sgritn sgrnit sgrnti sgrtin sgrtni sgtinr sgtirn
sgtnir sgtnri sgtrin sgtrni signrt signtr sigrnt sigrtn sigtnr sigtrn
singrt singtr sinrgt sinrtg sintgr sintrg sirgnt sirgtn sirngt sirntg
sirtgn sirtng sitgnr sitgrn sitngr sitnrg sitrgn sitrng sngirt sngitr
sngrit sngrti sngtir sngtri snigrt snigtr snirgt snirtg snitgr snitrg
snrgit snrgti snrigt snritg snrtgi snrtig sntgir sntgri sntigr sntirg
sntrgi sntrig srgint srgitn srgnit srgnti srgtin srgtni srignt srigtn
sringt srintg sritgn sritng srngit srngti srnigt srnitg srntgi srntig
srtgin srtgni srtign srting srtngi srtnig stginr stgirn stgnir stgnri
stgrin stgrni stignr stigrn stingr stinrg stirgn stirng stngir stngri
stnigr stnirg stnrgi stnrig strgin strgni strign string strngi strnig
tginrs tginsr tgirns tgirsn tgisnr tgisrn tgnirs tgnisr tgnris tgnrsi
tgnsir tgnsri tgrins tgrisn tgrnis tgrnsi tgrsin tgrsni tgsinr tgsirn
tgsnir tgsnri tgsrin tgsrni tignrs tignsr tigrns tigrsn tigsnr tigsrn
tingrs tingsr tinrgs tinrsg tinsgr tinsrg tirgns tirgsn tirngs tirnsg
tirsgn tirsng tisgnr tisgrn tisngr tisnrg tisrgn tisrng tngirs tngisr
tngris tngrsi tngsir tngsri tnigrs tnigsr tnirgs tnirsg tnisgr tnisrg
tnrgis tnrgsi tnrigs tnrisg tnrsgi tnrsig tnsgir tnsgri tnsigr tnsirg
tnsrgi tnsrig trgins trgisn trgnis trgnsi trgsin trgsni trigns trigsn
trings trinsg trisgn trisng trngis trngsi trnigs trnisg trnsgi trnsig
trsgin trsgni trsign trsing trsngi trsnig tsginr tsgirn tsgnir tsgnri
tsgrin tsgrni tsignr tsigrn tsingr tsinrg tsirgn tsirng tsngir tsngri
tsnigr tsnirg tsnrgi tsnrig tsrgin tsrgni tsrign tsring tsrngi tsrnig

That's beautiful, Richard, and ordered. It's ten across, and six factorial
is 5*2 * 6*4*3 is 10 times 72, and that looks right on.

How did you get this outputed before the "I won't do your homework" posts
appeared? Is this something you cover in your new book?
 
R

Ron Ford

Ron Ford said:



#include <stdlib.h>

int main(void)
{
system("anagram string | sort | fmt > string.txt");
return 0;
}

The word that follows anagram is sucked in as argv, in this case, 'string'.

The output is piped to sort and then to something that creates a rectangle
that is usenet-appropriate, followed by being herded into a text.

I think fmt would be strongly-challenged by argv of "jehosophat." BTW, I
misposted in c.l.c as opposed to c.l.f. otherwise.
 
D

Default User

Whether a file of zero length (on which no characters have been
written by an output stream) actually exists is implementation-
defined.
It's not denial. I'm just very selective about the reality I accept.


Ok, I'm just not buying the supposed randomness of these Calvin quotes.




Brian
 
L

lawrence.jones

Default User said:
Ok, I'm just not buying the supposed randomness of these Calvin quotes.

I'm sure there's a PhD thesis somewhere in why randomly selected Calvin
and Hobbes quotes seem far more relevant than randomly selected quotes
have any business being. :)

-- Larry Jones

Don't you hate it when your boogers freeze? -- Calvin
 
R

Richard

I'm sure there's a PhD thesis somewhere in why randomly selected Calvin
and Hobbes quotes seem far more relevant than randomly selected quotes
have any business being. :)

-- Larry Jones

Don't you hate it when your boogers freeze? -- Calvin

Regardless, it would be nice if you would stick the signature delimiter
"-- " followed by a new line before your name and the quote.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top