Skip to main content

All 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 ...
Bence Tolvaj's user avatar
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:...
Flamenco33's user avatar
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 ...
Dimabytes's user avatar
  • 536
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, ...
William John Holden's user avatar
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 ...
Yeni Eldima's user avatar
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;...
Kawaki's user avatar
  • 69
-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 ...
user avatar
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, ...
Vasu's user avatar
  • 93
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: ...
jfinmaniv's user avatar
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(...
user avatar
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 ...
user1809295's user avatar
  • 1,451
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 { ...
initmax's user avatar
  • 395
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 ...
chirag's user avatar
  • 1,828
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 ...
Vatsal's user avatar
  • 18.2k
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(...
kschmit90's user avatar
  • 538

15 30 50 per page