All Questions
22 questions
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
2
answers
537
views
How to return each line of a text file using a recursive function?
I am trying to return each line of html.txt:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
My current code is this:
def line_out(file):
if len(file) == 1:...
4
votes
2
answers
1k
views
Recursive calls between two functions
I need to recursive check two objects and keys need to be sorted, so I created two functions.
buildObj - get all uniq keys from object, sort them and call buildObjKey on each key
buildObjKey - if one ...
2
votes
2
answers
211
views
Does Python have a `nest` function like Mathematica?
Mathematica has a convenient Nest function for repeatedly evaluating f(f(f(f(...f(n))))). Not something you need every day, but occasionally useful. Here is a trivial implementation:
def nest(f, expr, ...
0
votes
1
answer
222
views
How to solve factorial even numbers on the n number, then multiply them
For example there are 3 then the program will
multiplying the first 3 factorials 2 x 4 x 6 = 48. For example if the user wants 3 then the program will multiply the first 3 factorials only and the sum ...
0
votes
2
answers
60
views
Trouble computing recursive value of this function
let rec f (l: int list) : int * int =
begin match l with
| [] -> (0,0)
| [x] (x,x)
| x::y::tl -> let (a,b) = f tl in
(x + a, y + b)
end
let r = f [2;3;4;...
-1
votes
1
answer
104
views
How many elements from one list are present in the other list
I can't seem to be able to create a function that takes two lists as arguments and returns how many elements there are common in both lists.
e.g. f [1, 2, 4, 2] [2, 3, 4, 4] returning 2 (repetitions ...
7
votes
5
answers
7k
views
Writing a factorial tail recursive function in Scala
I'm trying to write a tail recursive function in the below way, but the compiler is throwing an error:
Too many arguments for method apply: (v1: Int)Int in trait Function1
else factorial(x-1, ...
1
vote
2
answers
88
views
Why does "Sum()" succeed where "+" fails in recursive R function?
I am experimenting with the functional programming paradigm in R. I have defined a function that sums a sequence of integers from n to m. When I use sum() the function returns the expected result:
...
2
votes
3
answers
86
views
Why does this function return true?
const res = (n => {
const even = x => {
if (x === 0)
return true
else {
const odd = y => !even(y)
return odd(x - 1)
}
}
return even(n)
})(42)
console.log(...
1
vote
1
answer
508
views
How to double elements in an F# list and set them in a new list
I am very new to F# and functional programming in general, and would like to recursively create a function that takes a list, and doubles all elements.
This is what I used to search for a spacific ...
5
votes
1
answer
1k
views
Corecursion vs Recursion understanding in scala
if with recursion almost clear, for example
def product2(ints: List[Int]): Int = {
@tailrec
def productAccumulator(ints: List[Int], accum: Int): Int = {
ints match {
...
3
votes
1
answer
693
views
Error: The value Recusion function will be evaluated as part of its own definition in F#
let rec recFunc=
let read = stream.Read(buffer, 0, buffer.Length)
match read with
| a when a <= 0 -> ms.ToArray()
| _ -> recFunc // Called recursion Function
buffer
I ...
17
votes
2
answers
9k
views
How do I create an _inline_ recursive closure in Swift? [duplicate]
Recursion is trivial with global functions in Swift. For example:
func f()
{
f()
}
However, a closure cannot refer to itself. For example:
var f: (Void -> Void) =
{
f()
}
yields the ...
0
votes
1
answer
113
views
Groovy qSort and Filter implementations giving stack overflow error
This is the implementation of filter in Groovy I am attempting to use.
def filter(list, p) {
if (list.size() == 0) { return list }
if (p(list.head())) {
[] + list.head() + filter(...