Path: ...!weretis.net!feeder6.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail From: fir Newsgroups: comp.lang.c Subject: Re: Recursion, Yo Date: Sun, 07 Apr 2024 11:52:29 +0200 Organization: i2pn2 (i2pn.org) Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 7 Apr 2024 09:52:35 -0000 (UTC) Injection-Info: i2pn2.org; logging-data="378528"; mail-complaints-to="usenet@i2pn2.org"; posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0"; User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24 In-Reply-To: X-Spam-Checker-Version: SpamAssassin 4.0.0 Bytes: 3753 Lines: 108 Lawrence D'Oliveiro wrote: > Some homies they be sayin’ recursion is Teh Evulz. Well, check this > out. This program be recursing to the max, yo. > > Peace. > > ---- > /* > Generate permutations of a list of items. > Pass a list of arbitary words as command arguments, and this > program will print out all possible orderings of them. > */ > > #include > #include > #include > #include > > typedef void (*permute_action) > ( > const char * const * words > ); > > void permute > ( > unsigned int nrwords, > const char * const * words, > permute_action action > ) > { > if (nrwords > 0) > { > const char ** const permbuf = (const char **)malloc(nrwords * sizeof(char *)); > bool * const used = (bool *)malloc(nrwords); > for (unsigned int i = 0; i < nrwords; ++i) > { > used[i] = false; > } /*for*/ > > void permute1 > ( > unsigned int depth > ) > { > if (depth < nrwords) > { > for (unsigned int i = 0; i < nrwords; ++i) > { > if (not used[i]) > { > permbuf[depth] = words[i]; > used[i] = true; > permute1(depth + 1); > used[i] = false; > } /*if*/ > } /*for*/ > } > else > { > action(permbuf); > } /*if*/ > } /*permute1*/ > > permute1(0); > > free(permbuf); > free(used); > } /*if*/ > } /*permute*/ > > int main > ( > int argc, > char ** argv > ) > { > const unsigned int nrwords = argc - 1; > unsigned int count = 0; > > void collect > ( > const char * const * words > ) > { > count += 1; > fprintf(stdout, "[%d](", count); > for (unsigned int i = 0; i < nrwords; ++i) > { > if (i != 0) > { > fputs(", ", stdout); > } /*if*/ > fputs(words[i], stdout); > } /*for*/ > fputs(")\n", stdout); > } /*collect*/ > > permute > ( > /*nrwords =*/ nrwords, > /*words =*/ (const char * const * )(argv + 1), > /*permute_action =*/ (permute_action)collect > ); > > fprintf(stdout, "Nr found: %d\n", count); > } /*main*/ > okay, there are some class of things that suit good for recursion - and its probably good to spot whose they are