2

I have a function

let simpleSum n =
    let s = n * (n+1)/2
    printf "%A " s
let result = simpleSum 10

I now want to make it recursive; tail-recursion without added variables is preferred. There is something wrong with my statement: if n <= 0 then 0

 let rec recSum n = 
     if n <= 0 then
         0
     else
         recSum n*(n+1)/2
 recSum 4

I run into the error:

FS0020: The result of this expression is implicitly ignored. 
Consider using 'ignore' to discard this value explicitly, e.g. 'expr :> ignore',
or 'let' to bind the result to a name, e.g. 'let result = expr'.

How do I fix this? I want to avoid variables.

Prune
  • 76,765
  • 14
  • 60
  • 81
DonutSteve
  • 317
  • 4
  • 15
  • 1
    What's not working? what errors/issues are you running into? – mosca125 Sep 26 '16 at 13:41
  • What's the point of that while loop? In which cases would the result be any different than just `let sum n = n * (n+1) / 2`? – sepp2k Sep 26 '16 at 13:43
  • The while loop is not necessary I know, but was a requirement in my assignment. I run into the error: FS0020: The result of this expression is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr :> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'. I want to avoid variables here. – DonutSteve Sep 26 '16 at 13:47
  • 1
    Are you sure you didn't misunderstand your assignment? You can either calculate the sum using a loop/recursion *or* you can use the formula. Doing both at the same time makes no sense. – sepp2k Sep 26 '16 at 13:51
  • First assignment was to use a loop. Second assignment was to use recursion. Right now I am just trying to make the recursion work. The sum should add up to the same. – DonutSteve Sep 26 '16 at 14:04
  • you should also indent your code 4 spaces, makes it easier to read. – s952163 Sep 26 '16 at 14:07
  • In Visual Studio and VSCode pressing tab in F# gives you four spaces. – asibahi Sep 26 '16 at 14:09
  • But was the assignment to use the formula you're using? Or was it to use loops and recursion in a normal/sane way? – sepp2k Sep 26 '16 at 14:12
  • The assignment is just to find the sum of 1 + 2 + ... +n using recursion. Dont worry too much about my code above, since it is most likely wrong in so many ways. I am trying to figure this thing out. – DonutSteve Sep 26 '16 at 14:20
  • Then you're not currently solving the assignment. Again: your loop solution does not actually ever loop (except in the case where it loops infinitely). If the assignment is just to write a loop that sums the numbers from 1 to n, you should just do that and get rid of the formula. Once you did that, you can can rewrite that loop as a recursion. But as long as your loop-version is not actually looping, you can only ever end up with a recursive version that never actually recurs, which would not be in the spirit of the assignment. – sepp2k Sep 26 '16 at 14:24
  • Alright so I removed the while loop. But still running in to all kinds of trouble when trying to make it recursive. – DonutSteve Sep 26 '16 at 14:32
  • Take a look at http://stackoverflow.com/questions/39698430/f-how-to-call-a-function-with-argument-byref-int. It has a reclusive implementation. – s952163 Sep 26 '16 at 14:33
  • I took a look at it, and I cant find the reason why I am getting the FS0020 error message: The result of this expression is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr :> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'. There is something wrong with my statement: if n <= 0 then 0 – DonutSteve Sep 26 '16 at 16:29

1 Answers1

2

I am not going to do your assignment for you. However, I will show you the usual way to transform a loop into a recursive function.

This is the "imperative" way to do a factorial.

let factorial n = 
    let mutable acc = 1
    let mutable iter = 1 
    while iter <= n do 
        acc <- acc * iter
        iter <- iter + 1 
    acc 

And here it is in a recursive implementation :

let recFactorial n = 
    let rec loop acc iter = 
        if iter > n then acc else 
        loop (acc*iter) (iter+1) 
    loop 1 1 

You will see I defined the actual recursive function inside my bigger function. I believe it makes the code cleaner this way.

There are many ways, subtly different, to change a while or for loop into a recursive function, but this I think shows you how the one-to-one relationship works.

Your code gets stuck in an infinite loop when I call sum 1 btw. Crashed my editor.

Edit: Incidentally, the "naïve" way to do a Factorial recursive function is this

let rec recFactorial n = 
    if n = 1 then 1 else 
    n * recFactoiral (n-1)

However, this is not recommended and will crash your program quickly due to stack overflow. Any article on Tail Recursion (what I did the first time) will explain why this is in a much better way than I can.

asibahi
  • 857
  • 8
  • 14