All Questions
Tagged with functional-programming recursion
671 questions
13
votes
1
answer
487
views
Optimization of tail recursion in R
Since version 4.4.0, R supports tail recursion through the Tailcall function. I automatically assumed that it means an improvement for codes that use tail recursion.
However, consider the following ...
0
votes
3
answers
88
views
difficulty to understand the code, and maybe the concept itself (recursion problem)
this code below is from Grokking Algorithms book that is an exercise for the functional programming/recursion and an application for the D&C concept. the function finds the maximum number in a ...
0
votes
2
answers
147
views
Is there a difference in Haskell, regarding tail-recursion, between using guards that return boolean values and using (||) operators? [closed]
Let's use the following function as an example:
findWord :: [[Char]] -> [(Int, Int)] -> (Int, Int) -> String -> Bool
findWord _ _ _ [] = True -- Word was found
...
1
vote
1
answer
102
views
Recursive Functional Programming Question not making sense
The question is:
FunctionZ [] = 0
FunctionZ[x:xs] = x + 2 * FunctionZ(xs)
Complete the table below by writing the value of the argument passed to each call of
FunctionZ
and the value ...
1
vote
0
answers
126
views
Create bi-directional tree in F#
I am trying to define and create a tree structure where the parent knows the children and vice versa.
I created the following F# code which does not show any warnings (in fsx) and also seems to ...
1
vote
2
answers
210
views
How can I write a haskell function that applies a function on a list until one of its parameters turns 0?
I have to write the following haskell function:
It receives an Integer (let's call it h) and a list of Integers as
parameters. It should iterate through the list, and increment the
value of each ...
0
votes
1
answer
146
views
Pascal Triangle in OCaml: Get next line as a list
I am trying to get an OCaml function which gives the next line of a Pascal's triangle as a list for example:
(next_line: int -> list -> int list)
let get_next_line [1;1] returns [1;2;1] and so ...
0
votes
2
answers
89
views
Declaring foldl ( from left) in terms of foldr (from right)
I have foldl:
let rec foldl f lst acc = match lst with
| [] -> acc
| hd::tl -> foldl f lst (f hd acc)
and I have foldr:
let rec foldr f lst acc = match lst with
| [] -> acc
| hd::tl ...
-1
votes
1
answer
98
views
Getting the two minimum values from a list in OCaml
I need to write a function twoMin: int list → int * int using foldl that returns the two smallest values (not elements) of a list. You may assume that the list contains at least two elements with ...
-1
votes
1
answer
36
views
Using foldl to get the mean of a list
I am trying to get the mean of a list by using the following helping function:
let rec foldl f l b = match l with
| [] -> b
| x :: l -> foldl f l (f x b);;
let avg l = foldl (fun x acc ->...
1
vote
1
answer
51
views
Trying to get the number of even and odd integers in a list
I am trying to write a recursive function that takes in a list as argument and returns the number of even and odd numbers in tuple (number_of_even, number_of_odd)
let rec countEvenOdd lst e o =
...
1
vote
3
answers
111
views
OCaml list flattening using a recursive function
I am trying to get a recursive function to flatten lists where I am not allowed a helper function. I can only use @. And I'm trying to use "match with"
I tried this way
let rec flatten lst1 =...
0
votes
1
answer
100
views
OCaml higher order functions
Somebody please explain the algorithm in this problem for OCaml.
I have the solution but I do not understand it.
Define iterup: (int * 𝛼 → 𝛼) → int → int → 𝛼 → 𝛼. Iterup takes a function which ...
1
vote
0
answers
43
views
Are these two ways the same for overriding a function?
https://nixos.org/guides/nix-pills/override-design-pattern#id1431 provides a way of overriding a function f:
rec {
makeOverridable = f: origArgs:
let
origRes = f origArgs;
in
...
2
votes
1
answer
87
views
Typescript circular function reference
This function is actually quite simple, it either returns a function or a string depending on a inner function parameter.
function strBuilder(str: string) {
return function next(str2?: string) {
...