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 lambda(X)
on |λ|(X)
script Y
script Y
on lambda(Y)
on |λ|(Y)
script Z
script Z
on lambda(Z)
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 lambda
end |λ|
end script
end script
bind(Z, range(1 + Y, maxInteger))
bind(Z, enumFromTo(1 + Y, maxInteger))
end lambda
end |λ|
end script
end script
bind(Y, range(1 + X, maxInteger))
bind(Y, enumFromTo(1 + X, maxInteger))
end lambda
end |λ|
end script
end script
bind(X, range(1, maxInteger))
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}, {9, 12, 15}, {12, 16, 20}, {15, 20, 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
end run


Line 228: Line 227:
-- concat :: a -> a -> [a]
-- concat :: a -> a -> [a]
script concat
script concat
on lambda(a, b)
on |λ|(a, b)
a & b
a & b
end lambda
end |λ|
end script
end script
Line 243: Line 242:




---------------------------------------------------------------------------
-- GENERIC FUNCTIONS ----------------------------------------------------------


-- enumFromTo :: Int -> Int -> [Int]
-- GENERIC LIBRARY FUNCTIONS
on enumFromTo(m, n)
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
-- 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 lambda(v, item i of xs, i, xs)
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 lambda(item i of xs, i, xs)
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 lambda : f
property |λ| : f
end script
end script
end if
end if
end mReturn
end mReturn</lang>

-- range :: Int -> Int -> [Int]
on range(m, n)
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 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>