kandi background
kandi background
Explore Kits
kandi background
Explore Kits
Explore all Learning open source software, libraries, packages, source code, cloud functions and APIs.

Popular New Releases in Learning

新书出版

v1.22.1

v5.1.0

v0.1.4

1.7.0

leetcode

新书出版

wtfjs

v1.22.1

intro.js

v5.1.0

algorithms

v0.1.4

LeetCode-Go

1.7.0

Popular Libraries in Learning

Trending New libraries in Learning

Top Authors in Learning

1

126 Libraries

4879

2

90 Libraries

1933

3

54 Libraries

23695

4

48 Libraries

67554

5

35 Libraries

463

6

35 Libraries

2030

7

33 Libraries

12103

8

33 Libraries

25961

9

30 Libraries

594

10

29 Libraries

4333

1

126 Libraries

4879

2

90 Libraries

1933

3

54 Libraries

23695

4

48 Libraries

67554

5

35 Libraries

463

6

35 Libraries

2030

7

33 Libraries

12103

8

33 Libraries

25961

9

30 Libraries

594

10

29 Libraries

4333

Trending Kits in Learning

Trending Discussions on Learning

    How do purely functional languages handle index-based algorithms?
    How can I fix this : Warning: the fonts "Times" and "Times" are not available for the Java logical font "Serif"
    Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'
    How do I fix CLIENT_MISSING_INTENTS error?
    Getting error 'digital envelope routines', reason: 'unsupported', code: 'ERR_OSSL_EVP_UNSUPPORTED'
    Vue 3 passing array warning: Extraneous non-props attributes were passed to component but could not be automatically inherited
    TypeError: match.loader.options.plugins is not a function
    Group and create three new columns by condition [Low, Hit, High]
    What should the result be when assigning a variable to a reference to itself, in-between modified and then returned by a function call?
    Flutter (2.5) - A splash screen was provided to Flutter, but this is deprecated

QUESTION

How do purely functional languages handle index-based algorithms?

Asked 2022-Apr-05 at 12:51

I have been trying to learn about functional programming, but I still struggle with thinking like a functional programmer. One such hangup is how one would implement index-heavy operations which rely strongly on loops/order-of-execution.

For example, consider the following Java code:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20

Here, in the prefixList function, the nums list is first cloned, but then there is the iterative operation performed on it, where the value on index i relies on index i-1 (i.e. order of execution is required). Then this value is returned.

What would this look like in a functional language (Haskell, Lisp, etc.)? I have been learning about monads and think they may be relevant here, but my understanding is still not great.

ANSWER

Answered 2022-Mar-07 at 21:17

This is not an index-heavy operation, in fact you can do this with a one-liner with scanl1 :: (a -> a -> a) -> [a] -> [a]:

copy icondownload icon

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21

indeed, for the list of Nums, we get:

copy icondownload icon

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21Prelude> prefixList [1 .. 9]
22[1,3,6,10,15,21,28,36,45]
23

scanl1 takes the first item of the original list as initial value for the accumulator, and yields that. Then each time it takes the accumulator and the next item of the given list, and sums these up as new accumulator, and yields the new accumulator value.

Often one does not need indexing, but enumerating over the list is sufficient. Imperative programming languages often work with for loops with indexes, but in many cases these can be replaced by foreach loops that thus do not take the index into account. In Haskell this also often helps to make algorithms more lazy.

If you really need random access lookups, you can work with data structures such as defined in the array and vector packages.

Source https://stackoverflow.com/questions/71387267

Community Discussions contain sources that include Stack Exchange Network

    How do purely functional languages handle index-based algorithms?
    How can I fix this : Warning: the fonts "Times" and "Times" are not available for the Java logical font "Serif"
    Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'
    How do I fix CLIENT_MISSING_INTENTS error?
    Getting error 'digital envelope routines', reason: 'unsupported', code: 'ERR_OSSL_EVP_UNSUPPORTED'
    Vue 3 passing array warning: Extraneous non-props attributes were passed to component but could not be automatically inherited
    TypeError: match.loader.options.plugins is not a function
    Group and create three new columns by condition [Low, Hit, High]
    What should the result be when assigning a variable to a reference to itself, in-between modified and then returned by a function call?
    Flutter (2.5) - A splash screen was provided to Flutter, but this is deprecated

QUESTION

How do purely functional languages handle index-based algorithms?

Asked 2022-Apr-05 at 12:51

I have been trying to learn about functional programming, but I still struggle with thinking like a functional programmer. One such hangup is how one would implement index-heavy operations which rely strongly on loops/order-of-execution.

For example, consider the following Java code:

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20

Here, in the prefixList function, the nums list is first cloned, but then there is the iterative operation performed on it, where the value on index i relies on index i-1 (i.e. order of execution is required). Then this value is returned.

What would this look like in a functional language (Haskell, Lisp, etc.)? I have been learning about monads and think they may be relevant here, but my understanding is still not great.

ANSWER

Answered 2022-Mar-07 at 21:17

This is not an index-heavy operation, in fact you can do this with a one-liner with scanl1 :: (a -> a -> a) -> [a] -> [a]:

copy icondownload icon

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21

indeed, for the list of Nums, we get:

copy icondownload icon

1public class Main {
2    public static void main(String[] args) {
3        List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9);
4        System.out.println("Nums:\t"+ nums);
5        System.out.println("Prefix:\t"+prefixList(nums));
6    }
7  
8    private static List<Integer> prefixList(List<Integer> nums){
9      List<Integer> prefix = new ArrayList<>(nums);
10      for(int i = 1; i < prefix.size(); ++i)
11        prefix.set(i, prefix.get(i) + prefix.get(i-1));
12      return prefix;
13    }
14}
15/*
16System.out: 
17Nums:   [1, 2, 3, 4, 5, 6, 7, 8, 9]
18Prefix: [1, 3, 6, 10, 15, 21, 28, 36, 45]
19*/
20prefixList = scanl1 (+)
21Prelude> prefixList [1 .. 9]
22[1,3,6,10,15,21,28,36,45]
23

scanl1 takes the first item of the original list as initial value for the accumulator, and yields that. Then each time it takes the accumulator and the next item of the given list, and sums these up as new accumulator, and yields the new accumulator value.

Often one does not need indexing, but enumerating over the list is sufficient. Imperative programming languages often work with for loops with indexes, but in many cases these can be replaced by foreach loops that thus do not take the index into account. In Haskell this also often helps to make algorithms more lazy.

If you really need random access lookups, you can work with data structures such as defined in the array and vector packages.

Source https://stackoverflow.com/questions/71387267

Community Discussions contain sources that include Stack Exchange Network

Tutorials and Learning Resources in Learning

Tutorials and Learning Resources are not available at this moment for Learning

Share this Page

share link

Get latest updates on Learning

  • © 2022 Open Weaver Inc.