Prolog Type Error -
i'm working on prolog algorithm perform "swap" on list.
example:
input: [1,2,3,4] -> output: [3,4,1,2] input: [1,2,3,4,5] -> output: [4,5,3,1,2]
the first half , second half of list swap places, if there odd number center element retains it's position. have come algorithm, getting error:
?- swap([1,2,3,4],l). error: length/2: type error: `integer' expected, found `round(4/2)'
my code follows:
swap(l, s) :- length(l, length), reverse(l, l2), mixing(l, length, a), trim(l2, length/2 , b), append(a,b,s). trim(l,n,s) :- length(p,n), append(p,s,l). mixing(l, length, a) :- ( mod(length, 2) == 0 -> trim(l, round(length/2), a) ; trim(l, round(length/2), a) ).
the problem in 'mixing' when call trim (l, round(length/2), a) type not integer? understand length/2 not integer (most float) , thought round equivalent integer(expr) rounds , transforms data type integer. tried replacing round truncate(expr) , integer(expr), receiving same errors. can explain i'm doing wrong?
round
not function, it's predicate. haven't looked @ rest of code, line should be
round(length/2, r), trim(l, r, a)
edit: btw, you're overthinking it.
swap([], []). swap([x], [x]). swap([x, y | a], [y, x | b]) :- swap(a, b).
Comments
Post a Comment