How to get int from float in picat?

187 views Asked by At

I try to to read a line as string from console (stdin) in picat and get its half:

main =>
  L = read_line(),
  B = L.length/2,
  S = L.slice(1,B),
  println(S).

crashes with error(integer_expected(2.0),slice)

when int used instead of B - no crash. So how to turn B into integer?

3

There are 3 answers

0
ha9u63a7 On

Try either using integer(..) function to convert L.length/2 to integer or use to_integer() function....should do it for you.

0
CapelliC On

type inference plays an essential role in functional evaluation. (/ /2) it's a floating point arithmetic operator, but slice/2 expects an integer. So you should instead use (// /2).

Picat> L=read_line(),println(L.slice(1,L.length//2)).
123456789
1234
L = ['1','2','3','4','5','6','7','8','9']
yes
0
Mark On

you could use built-in function such as floor, round or ceiling from math module (more functions here). So you could modify your code like this:

main =>
    L = read_line(),
    B = round(L.length/2),
    S = L.slice(1,B),
    println(S).