Page 1 of 1

Newb question about variable not completely bound

Posted: 15 Nov 2013 7:26
by wyvie
Hello. I've recently started learning prolog and am testing around some stuff.
Yesterday i got some troubles with reading a list from console. Well, i need to get a list of integers greater then zero. So i'm reading them recursively from console in the first clause and want to fall into the second clause if i read 0 which means user has finished entering the list. But in the second clause i get an error that sais that variable '_' is not completely bound. Searching around the forum didn't help at all (maybe i'm dumb, i'm really sorry if it's so). Please, could you help me just a little bit? :)

Here's a little part of the code that generates that error:

Code: Select all

read_list([H|L]) :-     H = stdio::read(),     H > 0,     read_list(L). read_list(_).

Posted: 15 Nov 2013 7:58
by Tonton Luc
Hi,

Maybe try this (you forget "!," in your code) :

Code: Select all

read_list([H|L]) :-     H = stdio::read(),     H > 0,!, % here !!!     read_list(L). read_list(_).

Posted: 15 Nov 2013 8:03
by wyvie

Posted: 15 Nov 2013 9:16
by Tonton Luc
Hi,

Code: Select all

read_list([]):-!. read_list([H|L]) :-     H = stdio::read(),     H > 0,!,     read_list(L). read_list([_|L]) :-!,     read_list(L).

Posted: 15 Nov 2013 9:59
by wyvie
Well, more errors now :)
read_list([]) is the only clause that actually works, as the empty list is the first and most obvious one to return true. And it doesn't even make an attempt to read anything. Also it gives two warnings about unreachable code.

I tried to move this clause, and it has just brought me to a correct answer.

Code: Select all

read_list([H|L]) :-     H = stdio::read(),     H > 0,!,     read_list(L). read_list([]):-!.
This code works fine. And i can't understand, why...
But i think i'll understand it a little bit later. Thank you very much! Problem is solved :)