Jump to content

Power set: Difference between revisions

m
→‎{{header|AppleScript}}: Updated primitives
(Added Kotlin)
m (→‎{{header|AppleScript}}: Updated primitives)
Line 168:
(functional composition examples)
{{Trans|Haskell}}
<lang AppleScript>-- powersetPOWER :: [a]SET -> [[a]]----------------------------------------------------------------
 
-- powerset :: [a] -> [[a]]
on powerset(xs)
script subSet
on lambda|λ|(acc, x)
script consXcons
on lambda|λ|(y)
{x} & y
end lambda|λ|
end script
acc & map(consXcons, acc)
end lambda|λ|
end script
Line 185 ⟶ 187:
end powerset
 
--------------------------------------------------------------------------------------
 
---------------- TEST ----------------------------------------------------------------------
-- TEST
on run
script test
on lambda|λ|(x)
set {setName, setMembers} to x
{setName, powerset(setMembers)}
end lambda|λ|
end script
Line 204 ⟶ 205:
--> {"Empty set", {{}}},
--> {"Set containing only empty set", {{}, {{}}}}}
end run
 
-- GENERIC FUNCTIONS ---------------------------------------------------------------
 
-- GENERIC FUNCTIONS ---------------------------------------------------------------
 
-- foldr :: (a -> b -> a) -> a -> [b] -> a
Line 216 ⟶ 215:
set lng to length of xs
repeat with i from lng to 1 by -1
set v to lambda|λ|(v, item i of xs, i, xs)
end repeat
return v
Line 228 ⟶ 227:
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda|λ|(item i of xs, i, xs)
end repeat
return lst
Line 241 ⟶ 240:
else
script
property lambda|λ| : f
end script
end if
end mReturn</lang>
 
{{Out}}
<lang AppleScript>{{"Set [1,2,3]", {{}, {3}, {2}, {2, 3}, {1}, {1, 3}, {1, 2}, {1, 2, 3}}},
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.