How to resolve the algorithm Padovan sequence step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Padovan sequence step by step in the AppleScript programming language

Table of Contents

Problem Statement

The Padovan sequence is similar to the Fibonacci sequence in several ways. Some are given in the table below, and the referenced video shows some of the geometric similarities. Show output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Padovan sequence step by step in the AppleScript programming language

Source code in the applescript programming language

--------------------- PADOVAN NUMBERS --------------------

-- padovans :: [Int]
on padovans()
    script f
        on |λ|(abc)
            set {a, b, c} to abc
            
            {a, {b, c, a + b}}
        end |λ|
    end script
    
    unfoldr(f, {1, 1, 1})
end padovans


-- padovanFloor :: [Int]
on padovanFloor()
    script f
        property p : 1.324717957245
        property s : 1.045356793253
        on |λ|(n)
            {floor(0.5 + ((p ^ (n - 1)) / s)), 1 + n}
        end |λ|
    end script
    
    unfoldr(f, 0)
end padovanFloor


-- padovanLSystem :: [String]
on padovanLSystem()
    script rule
        on |λ|(c)
            if "A" = c then
                "B"
            else if "B" = c then
                "C"
            else
                "AB"
            end if
        end |λ|
    end script
    
    script f
        on |λ|(s)
            {s, concatMap(rule, characters of s) as string}
        end |λ|
    end script
    
    unfoldr(f, "A")
end padovanLSystem


--------------------------- TEST -------------------------
on run
    unlines({"First 20 padovans:", ¬
        showList(take(20, padovans())), ¬
        "", ¬
        "The recurrence and floor-based functions", ¬
        "match over the first 64 terms:\n", ¬
        prefixesMatch(padovans(), padovanFloor(), 64), ¬
        "", ¬
        "First 10 L-System strings:", ¬
        showList(take(10, padovanLSystem())), ¬
        "", ¬
        "The lengths of the first 32 L-System", ¬
        "strings match the Padovan sequence:\n", ¬
        prefixesMatch(padovans(), fmap(|length|, padovanLSystem()), 32)})
end run


-- prefixesMatch :: [a] -> [a] -> Bool
on prefixesMatch(xs, ys, n)
    take(n, xs) = take(n, ys)
end prefixesMatch


------------------------- GENERIC ------------------------

-- concatMap :: (a -> [b]) -> [a] -> [b]
on concatMap(f, xs)
    set lng to length of xs
    set acc to {}
    tell mReturn(f)
        repeat with i from 1 to lng
            set acc to acc & (|λ|(item i of xs, i, xs))
        end repeat
    end tell
    return acc
end concatMap


-- floor :: Num -> Int
on floor(x)
    if class of x is record then
        set nr to properFracRatio(x)
    else
        set nr to properFraction(x)
    end if
    set n to item 1 of nr
    if 0 > item 2 of nr then
        n - 1
    else
        n
    end if
end floor


-- fmap <$> :: (a -> b) -> Gen [a] -> Gen [b]
on fmap(f, gen)
    script
        property g : mReturn(f)
        on |λ|()
            set v to gen's |λ|()
            if v is missing value then
                v
            else
                g's |λ|(v)
            end if
        end |λ|
    end script
end fmap


-- intercalate :: String -> [String] -> String
on intercalate(delim, xs)
    set {dlm, my text item delimiters} to ¬
        {my text item delimiters, delim}
    set s to xs as text
    set my text item delimiters to dlm
    s
end intercalate


-- length :: [a] -> Int
on |length|(xs)
    set c to class of xs
    if list is c or string is c then
        length of xs
    else
        (2 ^ 29 - 1) -- (maxInt - simple proxy for non-finite)
    end if
end |length|


-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
    -- The list obtained by applying f
    -- to each element of xs.
    tell mReturn(f)
        set lng to length of xs
        set lst to {}
        repeat with i from 1 to lng
            set end of lst to |λ|(item i of xs, i, xs)
        end repeat
        return lst
    end tell
end map


-- min :: Ord a => a -> a -> a
on min(x, y)
    if y < x then
        y
    else
        x
    end if
end min


-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
    -- 2nd class handler function lifted into 1st class script wrapper. 
    if script is class of f then
        f
    else
        script
            property |λ| : f
        end script
    end if
end mReturn


-- properFraction :: Real -> (Int, Real)
on properFraction(n)
    set i to (n div 1)
    {i, n - i}
end properFraction


-- showList :: [a] -> String
on showList(xs)
    "[" & intercalate(",", map(my str, xs)) & "]"
end showList


-- str :: a -> String
on str(x)
    x as string
end str


-- take :: Int -> [a] -> [a]
-- take :: Int -> String -> String
on take(n, xs)
    set c to class of xs
    if list is c then
        if 0 < n then
            items 1 thru min(n, length of xs) of xs
        else
            {}
        end if
    else if string is c then
        if 0 < n then
            text 1 thru min(n, length of xs) of xs
        else
            ""
        end if
    else if script is c then
        set ys to {}
        repeat with i from 1 to n
            set v to |λ|() of xs
            if missing value is v then
                return ys
            else
                set end of ys to v
            end if
        end repeat
        return ys
    else
        missing value
    end if
end take


-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
on unfoldr(f, v)
    -- A lazy (generator) list unfolded from a seed value
    -- by repeated application of f to a value until no
    -- residue remains. Dual to fold/reduce.
    -- f returns either nothing (missing value),
    -- or just (value, residue).
    script
        property valueResidue : {v, v}
        property g : mReturn(f)
        on |λ|()
            set valueResidue to g's |λ|(item 2 of (valueResidue))
            if missing value  valueResidue then
                item 1 of (valueResidue)
            else
                missing value
            end if
        end |λ|
    end script
end unfoldr


-- unlines :: [String] -> String
on unlines(xs)
    -- A single string formed by the intercalation
    -- of a list of strings with the newline character.
    set {dlm, my text item delimiters} to ¬
        {my text item delimiters, linefeed}
    set s to xs as text
    set my text item delimiters to dlm
    s
end unlines


  

You may also check:How to resolve the algorithm Identity matrix step by step in the FunL programming language
You may also check:How to resolve the algorithm URL parser step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Quackery programming language
You may also check:How to resolve the algorithm P-value correction step by step in the Perl programming language
You may also check:How to resolve the algorithm Tarjan step by step in the jq programming language