473,549 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

powerset operation

Hi,

I would like to know how to implement a powerset operation in perl.
My friend found the code:

sub subsets {
map {
my $n = $_;
[@_[grep {$n & 1 << $_} 0..$#_]]
} (1..2**($#_ + 1));
}

online, and that makes sense - but I would like to understand how to
build a lists of lists etc through the `normal' way of building the
powerset.

Normal being akin to the haskell code:
powerset :: [a] -> [[a]]
powerset [] = [[]]
powerset (x:xs) = concat $ [[l, x:l] | l <- powerset xs]

my closest perl approximation is:

sub subsets {
my @list;
@list = @_;

return (()) if ($#list < 0);

my @subs;
@subs = subsets(@subs[1..$#subs]);

my @with;
@with = @subs;
foreach (@with) {
push(@{$_}, $list[0]);
push(@subs, [ $_ ]);
}

return @subs;
}
but this doesn't seem to work. (I'm relatively new to perl, but am a
somewhat well-versed unix and ruby user, if where I'm coming from
matters in explanations... )
Jul 19 '05 #1
0 1577

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1840
by: Tim Rowe | last post by:
Has anybody written a good powerset function for that nice new set datatype we now have? I've found a powerset operator for Python lists on the net, but that depends on lists being indexable which sets aren't. I suppose one answer is to iterate the set into a list, use the powerset I've got, then iterate the answer back int a set...
3
5884
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892 Bytes=7063896) How can I correlate which part of the SQL statement is running on full table scan. Please see below for the code and explain plan SQL Code
1
5998
by: Gunnar G | last post by:
I have a vector<int> x; and I would like to call the function foo( a_subset_of_x ) for ALL subsets of x (i.e. the powerset of x) Is this possible in an easy way?
4
1959
by: Hardy Wang | last post by:
Hi, I have a win form application, when a button is clicked, a lengthy operation will be triggered. During the time program is still running, this application seems not to be able to response to other actions. When I switch to other window, and switch back to my application, all I can see is a white window, it won't refresh itself until...
0
4119
by: John Jenkins | last post by:
Hi, any help on this would be greatly apprciated. I have been given a wsdl file from a customer generated by Oracle 10g Web Services tools. When I use wsdl.exe to attempt to produce proxy classes I get the following error: Error: Unable to import binding 'SubmitMessageSkeletonBinding' from namespace...
0
1292
by: relaxedrob | last post by:
Hi All, I have a portType such as this: <portType name="CMLeJobSoapGetEmpBrand"> <operation name="EJobGetEmpBrand"> <input message="tns:EJobEmpBrdReq" name="EJobEmpBrdReq"/> <output message="tns:EJobGetEmpBrdRes" name="EJobGetEmpBrdRes"/> <fault message="tns:Fault" name="Fault"/> </operation>
6
2846
by: Cerebrus99 | last post by:
Hi all, I'm making a Windows application that does some lengthy retrieval operations from a database and possibly from a internet resource. I want to show that the operation is going on, by using an Animated .gif in a picture box. Also note that this PictureBox also acts as the button to invoke this lengthy operation. In other words, the...
2
4794
by: Robinson | last post by:
I can start an Asynchronous operation against a data source with SQLCommand.BeginExecuteReader, allowing me to loop, checking for user cancellation before the operation has completed, but how then to cancel the SQLCommand if the user wishes to? The "Cancel" method states: ".......... The Cancel method cannot be used to cancel a pending...
0
3126
by: Default User | last post by:
I work on creating test cases for a SOAP-based set of servers, using soapUI. I received and updated set of WSDL and schema files, and when I made new tests and mock server operations, all of the ones that had been working now returned a SOAP fault. The faults look something like: <soap:Envelope...
0
7521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7959
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5369
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
764
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.