List comprehensions: Difference between revisions
Content deleted Content added
m →{{header|Haskell}}: (Added a type signature) |
→{{header|AppleScript}}: Updated primitives |
||
Line 175:
{{trans|JavaScript}}
<lang AppleScript>-- List comprehension by direct and unsugared use of list monad
Line 181 ⟶ 180:
on pythagoreanTriples(maxInteger)
script X
on
script Y
on
script Z
on
if X * X + Y * Y = Z * Z then
unit([X, Y, Z])
Line 191 ⟶ 190:
[]
end if
end
end script
bind(Z,
end
end script
bind(Y,
end
end script
bind(X,
end pythagoreanTriples
-- TEST -----------------------------------------------------------------------
on run
-- Pythagorean triples drawn from integers in the range [1..n]
Line 213 ⟶ 212:
pythagoreanTriples(25)
--> {{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
Line 228 ⟶ 227:
-- concat :: a -> a -> [a]
script concat
on
a & b
end
end script
Line 243 ⟶ 242:
--
if n < m then▼
set d to -1▼
▲ else
set d to 1▼
end if▼
set lst to {}▼
repeat with i from m to n by d▼
set end of lst to i▼
end repeat▼
return lst▼
end enumFromTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 253 ⟶ 264:
set lng to length of xs
repeat with i from 1 to lng
set v to
end repeat
return v
Line 265 ⟶ 276:
set lst to {}
repeat with i from 1 to lng
set end of lst to
end repeat
return lst
Line 278 ⟶ 289:
else
script
property
end script
end if
end mReturn</lang>
▲-- range :: Int -> Int -> [Int]
▲on range(m, n)
▲ if n < m then
▲ set d to -1
▲ set d to 1
▲ end if
▲ set lst to {}
▲ repeat with i from m to n by d
▲ set end of lst to i
▲ end repeat
▲ return lst
{{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>
|