0

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?

DuckQueen
  • 772
  • 10
  • 62
  • 134

3 Answers3

2

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).
Mark
  • 399
  • 4
  • 12
0

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

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0

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
CapelliC
  • 59,646
  • 5
  • 47
  • 90