Power set: Difference between revisions
Content added Content deleted
m (AppleScript Adjusting names etc to improve readability of relationship to JS and Haskell examples) |
|||
Line 167: | Line 167: | ||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
(functional composition examples) |
(functional composition examples) |
||
{{Trans|Haskell}} |
|||
<lang AppleScript>-- powerset :: [a] -> [[a]] |
<lang AppleScript>-- powerset :: [a] -> [[a]] |
||
on powerset(xs) |
on powerset(xs) |
||
script subSet |
script subSet |
||
on lambda( |
on lambda(acc, x) |
||
script |
script consX |
||
on lambda(y) |
on lambda(y) |
||
{x} & y |
{x} & y |
||
Line 177: | Line 178: | ||
end script |
end script |
||
acc & map(consX, acc) |
|||
end lambda |
end lambda |
||
end script |
end script |
||
Line 243: | Line 244: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn |
end mReturn |
||
-- concat :: [[a]] -> [a] | [String] -> String |
|||
on concat(xs) |
|||
script append |
|||
on lambda(a, b) |
|||
a & b |
|||
end lambda |
|||
end script |
|||
if length of xs > 0 and class of (item 1 of xs) is string then |
|||
set unit to "" |
|||
else |
|||
set unit to {} |
|||
end if |
|||
foldl(append, unit, xs) |
|||
end concat</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 1,436: | Line 1,453: | ||
powerset (head:tail) = acc ++ map (head:) acc where acc = powerset tail</lang> |
powerset (head:tail) = acc ++ map (head:) acc where acc = powerset tail</lang> |
||
or |
or |
||
<lang Haskell> |
<lang Haskell>powerSet :: [a] -> [[a]] |
||
powerset = foldr (\x acc -> acc ++ map (x:) acc) [[]]</lang> |
|||
Examples: |
Examples: |
||