How to resolve the algorithm Calculating the value of e step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Calculating the value of e step by step in the AppleScript programming language

Table of Contents

Problem Statement

Calculate the value of   e.

(e   is also known as   Euler's number   and   Napier's constant.)

See details: Calculating the value of e

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Calculating the value of e step by step in the AppleScript programming language

Source code in the applescript programming language

--------------- CALCULATING THE VALUE OF E ----------------
on run
    
    sum(map(inverse, ¬
        scanl(product, 1, enumFromTo(1, 16))))
    
    --> 2.718281828459
    
end run

-- inverse :: Float -> Float
on inverse(x)
    1 / x
end inverse

-- product :: Float -> Float -> Float
on product(a, b)
    a * b
end product


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

-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
    if m  n then
        set lst to {}
        repeat with i from m to n
            set end of lst to i
        end repeat
        lst
    else
        {}
    end if
end enumFromTo

-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
    tell mReturn(f)
        set v to startValue
        set lng to length of xs
        repeat with i from 1 to lng
            set v to |λ|(v, item i of xs, i, xs)
        end repeat
        return v
    end tell
end foldl

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

-- 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

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

-- sum :: [Num] -> Num
on sum(xs)
    script add
        on |λ|(a, b)
            a + b
        end |λ|
    end script
    
    foldl(add, 0, xs)
end sum


------------- APPROXIMATION OF THE VALUE OF E ------------

-- eApprox :: Int -> Float
on eApprox(n)
    -- The approximation of E obtained after N iterations.
    script go
        on |λ|(efl, x)
            set {e, fl} to efl
            set flx to fl * x
            
            {e + (1 / flx), flx}
        end |λ|
    end script
    
    item 1 of foldl(go, {1, 1}, enumFromTo(1, n))
end eApprox


--------------------------- TEST -------------------------
on run
    -- The approximation of E obtained after 20 iterations.
    eApprox(20)
end run


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

-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
    if m  n then
        set xs to {}
        repeat with i from m to n
            set end of xs to i
        end repeat
        xs
    else
        {}
    end if
end enumFromTo


-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
    tell mReturn(f)
        set v to startValue
        set lng to length of xs
        repeat with i from 1 to lng
            set v to |λ|(v, item i of xs, i, xs)
        end repeat
        return v
    end tell
end foldl


-- 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


  

You may also check:How to resolve the algorithm Holidays related to Easter step by step in the C programming language
You may also check:How to resolve the algorithm Self-describing numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the Ada programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Lingo programming language
You may also check:How to resolve the algorithm Digital root step by step in the CLU programming language