All Questions
13 questions
0
votes
3
answers
152
views
Why Higher Order Function Is Only Called Once
I'm struggling to understand why memoize in this example appears to only be called once. Looking at the code, I would expect "memo prints once" to print with each layer of recursion. However,...
2
votes
4
answers
262
views
Higher-order function with recursion in Javascript
newbie here... I'm trying to grasp the concept of functional programming in Javascript, but I got stuck.
I'm trying to apply a function to another function with recursion (higher-order function). Let'...
1
vote
0
answers
86
views
How to make a higher-order function to process other functions and make them useable by a recursive `while` function?
So I have these functions:
/**
* @description Recurses while a condition is true. Not stack-safe.
* @param {Object} object_arguments - Contains the arguments for the function.
* @param {function} ...
-2
votes
2
answers
969
views
writing a recursive function to count the depth of a tree
I have to write a recursive function that, Given a Tree
datatype,will return the depth of the tree. An empty tree should return
0. A single root node Tree should return 1.
expected output:
let ...
-2
votes
1
answer
172
views
write a recursive function that adds elements in ascending order [closed]
I have to write the recursive function insertSort. Given a list of
type x and an element of type x insert the element into the list such
that the list is in ascending order. Duplicates are allowed. ...
1
vote
2
answers
58
views
Write the recursive function adjuster
Write the recursive function adjuster. Given a list of type
x, an int and an element of type x, either remove from the front of the
list until it is the same length as int, or append to the end of the ...
0
votes
3
answers
907
views
A function that returns maximum of list and count of occurrences
I'm writing a recursive function mxAndC. When I give it a list, it should return a tuple. The tuple will have the maximum of the given list as its first element and the second element will be the ...
0
votes
1
answer
650
views
How to create a function that encodes run-length using fold_right?
I created a function and helper function that find the number of repeating elements in a list, and what those elements.
let rec _encode l x =
match l with
| [] -> 0
| head::rest -> (if ...
0
votes
2
answers
433
views
Higher order functions in tree traversal
How would I be able to create common tree operations such as insert and search while passing in functions to reduce redundancy. For example, a recursive function calls itself on the left branch when ...
1
vote
1
answer
364
views
R - converting recursive code to functional programming
I want to do a conditional cumsum. I originally thought I could use the Reduce function but I wasnt able to. To explain clearly :
a <- rep(1,5)
b <- rnorm(n=5,mean=0.5)
c <- rep(2,5)
d <- ...
99
votes
1
answer
8k
views
What are paramorphisms?
Reading through this classic paper, I'm stuck on paramorphisms. Unfortunately the section is quite thin, and the Wikipedia page doesn't say anything.
My Haskell translation is:
para :: (a -> [a] ...
3
votes
3
answers
891
views
How can this function be written using foldr?
I have this simple function which returns a list of pairs with the adjacents elements of a list.
adjacents :: [a] -> [(a,a)]
adjacents (x:y:xs) = [(x,y)] ++ adjacents (y:xs)
adjacents (x:xs) = []
...
4
votes
2
answers
10k
views
reversing a list in OCaml using fold_left/right
UPDATE - Solution
Thanks to jacobm for his help, I came up with a solution.
// Folding Recursion
let reverse_list_3 theList =
List.fold_left (fun element recursive_call -> recursive_call::...