Longest common prefix: Difference between revisions
m
→AppleScript :: Functional: Tidied
m (→{{header|AppleScript}}: Restored missing comment indicator, inserted separator, added index subheadings.) |
m (→AppleScript :: Functional: Tidied) |
||
Line 272:
----
===Functional===
and for more productivity, and higher re-use of existing library functions, we can write a functional definition (rather than a procedure).
<lang applescript>-------------------
-- longestCommonPrefix :: [String] -> String
on longestCommonPrefix(xs)
if
map(my fst, ¬
takeWhile(my
else
end if
end longestCommonPrefix
--------------------------
on run
script test
Line 306 ⟶ 309:
--------------------- GENERIC FUNCTIONS
-- all :: (a -> Bool) -> [a] -> Bool
Line 319 ⟶ 322:
end tell
end all
true▼
else▼
script p
on |λ|(x)
h = x
end |λ|
end if
end allSame
-- chars :: String -> [Char]
Line 324 ⟶ 344:
characters of s
end chars
-- comparing :: (a -> b) -> (a -> a -> Ordering)
Line 343 ⟶ 364:
end script
end comparing
-- concatMap :: (a -> [b]) -> [a] -> [b]
Line 355 ⟶ 377:
return acc
end concatMap
-- eq (==) :: Eq a => a -> a -> Bool
Line 364 ⟶ 387:
end script
end eq
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 376 ⟶ 400:
end tell
end foldl
-- fst :: (a, b) -> a
Line 386 ⟶ 411:
end fst
▲-- identical :: [a] -> Bool
▲on identical(xs)
▲ if 0 < length of xs then
▲ set h to item 1 of xs
▲ all(eq(h), rest of xs)
▲ else
▲ true
▲ end if
-- intercalate :: String -> [String] -> String
Line 406 ⟶ 420:
str
end intercalate
-- justifyLeft :: Int -> Char -> String -> String
Line 419 ⟶ 434:
end script
end justifyLeft
-- length :: [a] -> Int
Line 429 ⟶ 445:
end if
end |length|
-- map :: (a -> b) -> [a] -> [b]
Line 443 ⟶ 460:
end tell
end map
-- maximumBy :: (a -> a -> Ordering) -> [a] -> a
Line 459 ⟶ 477:
foldl(max, missing value, xs)
end maximumBy
-- min :: Ord a => a -> a -> a
Line 468 ⟶ 487:
end if
end min
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
Line 480 ⟶ 500:
end if
end mReturn
-- Egyptian multiplication - progressively doubling a list, appending
Line 497 ⟶ 518:
return out & dbl
end replicate
-- showList :: [a] -> String
Line 515 ⟶ 537:
end if
end showList
-- take :: Int -> [a] -> [a]
Line 547 ⟶ 570:
end if
end take
-- takeWhile :: (a -> Bool) -> [a] -> [a]
Line 563 ⟶ 587:
end if
end takeWhile
-- transpose :: [[a]] -> [[a]]
Line 582 ⟶ 607:
map(cols, item 1 of rows)
end transpose
-- unlines :: [String] -> String
|