List comprehensions: Difference between revisions
Content added Content deleted
m (→{{header|Haskell}}: (Added a type signature)) |
(→{{header|AppleScript}}: Updated primitives) |
||
Line 175: | Line 175: | ||
{{trans|JavaScript}} |
{{trans|JavaScript}} |
||
<lang AppleScript>-- List comprehension by direct and unsugared use of list monad |
<lang AppleScript>-- List comprehension by direct and unsugared use of list monad |
||
Line 181: | Line 180: | ||
on pythagoreanTriples(maxInteger) |
on pythagoreanTriples(maxInteger) |
||
script X |
script X |
||
on |
on |λ|(X) |
||
script Y |
script Y |
||
on |
on |λ|(Y) |
||
script Z |
script Z |
||
on |
on |λ|(Z) |
||
if X * X + Y * Y = Z * Z then |
if X * X + Y * Y = Z * Z then |
||
unit([X, Y, Z]) |
unit([X, Y, Z]) |
||
Line 191: | Line 190: | ||
[] |
[] |
||
end if |
end if |
||
end |
end |λ| |
||
end script |
end script |
||
bind(Z, |
bind(Z, enumFromTo(1 + Y, maxInteger)) |
||
end |
end |λ| |
||
end script |
end script |
||
bind(Y, |
bind(Y, enumFromTo(1 + X, maxInteger)) |
||
end |
end |λ| |
||
end script |
end script |
||
bind(X, |
bind(X, enumFromTo(1, maxInteger)) |
||
end pythagoreanTriples |
end pythagoreanTriples |
||
-- TEST ----------------------------------------------------------------------- |
|||
on run |
on run |
||
-- Pythagorean triples drawn from integers in the range [1..n] |
-- Pythagorean triples drawn from integers in the range [1..n] |
||
Line 213: | Line 212: | ||
pythagoreanTriples(25) |
pythagoreanTriples(25) |
||
--> {{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17}, |
--> {{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17}, |
||
-- {9, 12, 15}, {12, 16, 20}, {15, 20, 25}} |
|||
⚫ | |||
end run |
end run |
||
Line 228: | Line 227: | ||
-- concat :: a -> a -> [a] |
-- concat :: a -> a -> [a] |
||
script concat |
script concat |
||
on |
on |λ|(a, b) |
||
a & b |
a & b |
||
end |
end |λ| |
||
end script |
end script |
||
Line 243: | Line 242: | ||
-- |
-- GENERIC FUNCTIONS ---------------------------------------------------------- |
||
⚫ | |||
-- GENERIC LIBRARY FUNCTIONS |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end enumFromTo |
|||
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
-- foldl :: (a -> b -> a) -> a -> [b] -> a |
||
Line 253: | Line 264: | ||
set lng to length of xs |
set lng to length of xs |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set v to |
set v to |λ|(v, item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return v |
return v |
||
Line 265: | Line 276: | ||
set lst to {} |
set lst to {} |
||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set end of lst to |
set end of lst to |λ|(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
Line 278: | Line 289: | ||
else |
else |
||
script |
script |
||
property |
property |λ| : f |
||
end script |
end script |
||
end if |
end if |
||
end mReturn |
end mReturn</lang> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end range</lang> |
|||
{{Out}} |
{{Out}} |
||
<lang AppleScript>{{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17}, {9, 12, 15}, {12, 16, 20}, {15, 20, 25}}</lang> |
<lang AppleScript>{{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17}, {9, 12, 15}, {12, 16, 20}, {15, 20, 25}}</lang> |