DuckQueen
DuckQueen

Reputation: 810

How to get int from float in picat?

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?

Upvotes: 0

Views: 194

Answers (3)

Mark
Mark

Reputation: 419

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).

Upvotes: 2

CapelliC
CapelliC

Reputation: 60034

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

Upvotes: 0

ha9u63a7
ha9u63a7

Reputation: 6872

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

Upvotes: 0

Related Questions