Update README.md
Browse files
README.md
CHANGED
@@ -24,52 +24,52 @@ Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the
|
|
24 |
## Example Output
|
25 |
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
>
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
>> ```javascript
|
34 |
-
>> const factorial = n => n === 0 ? 1 : n * factorial(n - 1);
|
35 |
-
>>
|
36 |
-
>> console.log(factorial(5)); // Output: 120
|
37 |
-
>>
|
38 |
-
>> ```
|
39 |
-
>>
|
40 |
-
>> Now, let's try to write an equivalent code snippet in ANSI-C with similar functionality:
|
41 |
-
>>
|
42 |
-
>> ```c
|
43 |
-
>> #include <stdio.h>
|
44 |
-
>> #define MAX_RECURSION_DEPTH 1000
|
45 |
-
>> int factorial(int n) {
|
46 |
-
>> if (n == 0)
|
47 |
-
>> return 1;
|
48 |
-
>> else {
|
49 |
-
>> int result;
|
50 |
-
>> result = factorial(--n);
|
51 |
-
>> if (++depth > MAX_RECURSION_DEPTH)
|
52 |
-
>> while(1){} // Recursion stack overflow
|
53 |
-
>> return n * result;
|
54 |
-
>> }
|
55 |
-
>> }
|
56 |
-
>> int main() {
|
57 |
-
>> int i, result;
|
58 |
-
>> int n = 5;
|
59 |
-
>> int depth = 0;
|
60 |
-
>>
|
61 |
-
>> result = factorial(n);
|
62 |
-
>> printf("Factorial of %d is: %d\n", n, result);
|
63 |
-
>> return 0;
|
64 |
-
>> }
|
65 |
-
>> ```
|
66 |
-
>>
|
67 |
-
>> As you can see, the ANSI-C code becomes more verbose and has several disadvantages compared to JavaScript:
|
68 |
-
>>
|
69 |
-
>> 1. Need to define a maximum recursion depth using a constant or an array index, which can lead to hardcoded limits and potential stack overflow issues.
|
70 |
-
>> 2. The use of preprocessor directives (#define) for defining functions and macros can make the code less readable and harder to understand.
|
71 |
-
>> 3. The lack of closures in ANSI-C requires using static variables or passing arguments as arguments to nested functions, making the code more verbose and less concise.
|
72 |
-
>> ```
|
73 |
|
74 |
## Merge Details
|
75 |
### Merge Method
|
|
|
24 |
## Example Output
|
25 |
|
26 |
|
27 |
+
give me some code that is very simple in javascript but would be very difficult in ANSI-C
|
28 |
+
|
29 |
+
> In JavaScript, functions are first-class values, which means you can assign them to variables, pass them as arguments to other functions, and even nest them inside other functions. This leads to concise and expressive code.
|
30 |
+
>
|
31 |
+
> Here's an example of a simple recursive function that calculates the factorial of a given number in JavaScript:
|
32 |
+
>
|
33 |
+
> ```javascript
|
34 |
+
> const factorial = n => n === 0 ? 1 : n * factorial(n - 1);
|
35 |
+
>
|
36 |
+
> console.log(factorial(5)); // Output: 120
|
37 |
+
>
|
38 |
+
> ```
|
39 |
+
>
|
40 |
+
> Now, let's try to write an equivalent code snippet in ANSI-C with similar functionality:
|
41 |
+
>
|
42 |
+
> ```c
|
43 |
+
> #include <stdio.h>
|
44 |
+
> #define MAX_RECURSION_DEPTH 1000
|
45 |
+
> int factorial(int n) {
|
46 |
+
> if (n == 0)
|
47 |
+
> return 1;
|
48 |
+
> else {
|
49 |
+
> int result;
|
50 |
+
> result = factorial(--n);
|
51 |
+
> if (++depth > MAX_RECURSION_DEPTH)
|
52 |
+
> while(1){} // Recursion stack overflow
|
53 |
+
> return n * result;
|
54 |
+
> }
|
55 |
+
> }
|
56 |
+
> int main() {
|
57 |
+
> int i, result;
|
58 |
+
> int n = 5;
|
59 |
+
> int depth = 0;
|
60 |
+
>
|
61 |
+
> result = factorial(n);
|
62 |
+
> printf("Factorial of %d is: %d\n", n, result);
|
63 |
+
> return 0;
|
64 |
+
> }
|
65 |
+
> ```
|
66 |
+
>
|
67 |
+
> As you can see, the ANSI-C code becomes more verbose and has several disadvantages compared to JavaScript:
|
68 |
>
|
69 |
+
> 1. Need to define a maximum recursion depth using a constant or an array index, which can lead to hardcoded limits and potential stack overflow issues.
|
70 |
+
> 2. The use of preprocessor directives (#define) for defining functions and macros can make the code less readable and harder to understand.
|
71 |
+
> 3. The lack of closures in ANSI-C requires using static variables or passing arguments as arguments to nested functions, making the code more verbose and less concise.
|
72 |
+
> ```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
## Merge Details
|
75 |
### Merge Method
|