Longest common prefix: Difference between revisions

m
m (→‎{{header|AppleScript}}: Restored missing comment indicator, inserted separator, added index subheadings.)
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).
and for a functional definition (rather than a procedure) we can compose reusable generics, applying a ''takeWhile'' function (with a suitable predicate) to a transposed matrix of the characters in the given words.
 
<lang applescript>------------------- longestCommonPrefixLONGEST ::COMMON [String]PREFIX -> String-----------------
 
 
-- longestCommonPrefix :: [String] -> String
on longestCommonPrefix(xs)
if {}1 < length of xs then
map(my fst, ¬
takeWhile(my identicalallSame, my transpose(map(my chars, xs)))) as text
else
""xs as text
end if
end longestCommonPrefix
 
 
---------------------------TEST-- TESTS --------------------------
on run
script test
Line 306 ⟶ 309:
 
 
--------------------- GENERIC FUNCTIONS- --------------------
 
-- all :: (a -> Bool) -> [a] -> Bool
Line 319 ⟶ 322:
end tell
end all
 
 
-- identicalallSame :: [a] -> Bool
on identicalallSame(xs)
if 02 <> length of xs then
true
else
script p
set property h to: item 1 of xs
on |λ|(x)
h = x
end |λ|
end ifscript
all(eq(h)p, rest of xs)
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)
-- True if the list is empty, or
-- if all its items are identical.
if 0 < length of xs then
set h to item 1 of xs
all(eq(h), rest of xs)
else
true
end if
end identical
 
-- 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
9,655

edits