Newb question about variable not completely bound

Discussions related to Visual Prolog
wyvie
Posts: 3
Joined: 15 Nov 2013 7:17

Newb question about variable not completely bound

Unread post 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(_).
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Unread post 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(_).
wyvie
Posts: 3
Joined: 15 Nov 2013 7:17

Unread post by wyvie »

User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Unread post 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).
wyvie
Posts: 3
Joined: 15 Nov 2013 7:17

Unread post 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 :)
Post Reply