Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

something wrong in recursive functions inside a function


Hi,

I found some problems when using recursive functions inside a function,
for example:

=============================

use std

var data: int[10]

const findme = {x
    const bs = {l, r
        std.put("l = {}, r = {}\n", l, r)
        if l > r
            -> -1
        ;;
        var mid: int = (l + r) / 2
        std.put("data[mid] = {}\n", data[mid])
        if data[mid] > x
            -> bs(l, mid-1)
        elif data[mid] < x
            -> bs(mid+1, r)
        else
            -> mid
        ;;
    }
    -> bs(0, 9)
}

const main = {
    data[0] = 1
    for var i = 1; i < 10; i++
        data[i] = data[i-1] * 3
    ;;
    findme(7)
}
===============================

This program just prints:

  l = 0, r = 9
  data[mid] = 81

Then a segfault happens.

I think that's caused by using recursive functions inside a function. So
I wrote another program to test it:

==========================

use std

const test = {n
    const t0 = {i
        if i < n
            std.put("i = {}\n", i)
            t0(i+1)
        ;;
    }
    t0(0)
}

const main = {
    test(5)
}
============================

This time, the above program only prints "i = 0" then exits instead of
printing 5 lines.

So is there something wrong with using recursive functions inside a
function?

Iru


Follow-Ups:
Re: something wrong in recursive functions inside a functionOri Bernstein <ori@xxxxxxxxxxxxxx>
Re: something wrong in recursive functions inside a functionOri Bernstein <ori@xxxxxxxxxxxxxx>