Skip to content

Instantly share code, notes, and snippets.

@yashkumaratri
Created November 1, 2020 12:50
Show Gist options
  • Save yashkumaratri/95d6beea12b26697331b2359e980d8db to your computer and use it in GitHub Desktop.
Save yashkumaratri/95d6beea12b26697331b2359e980d8db to your computer and use it in GitHub Desktop.
Reading a csv file of city distance in prolog.
Assuming the CSV looks something like :
"Distance","E","G","C","B"
"A","3","5","7","3"
"B","4","5","6","0"
"C","4","1","0","6"
"D","5","8","3","6"
Code:
:- [library(csv)] .
:- [library(lists)] .
%! distance(CITY_A,CITY_B,DISTANCE)
%
% dynamic facts asserted based upon csv file .
:- dynamic distance/3 .
:- op(1,'xfy','csv_') .
%! csv(FILE0)
%
% read the CSV file `FILE0` and add facts to the database .
csv(FILE0)
:-
(start) csv_ (FILE0)
.
(start) csv_ (FILE0)
:-
csv:csv_read_file(FILE0,[HEADER|ROWss]) ,
row__to__list(HEADER,HEADERs) ,
(loop) csv_ (HEADERs,ROWss)
.
(loop) csv_ (_HEADERs,[])
:-
true
.
(loop) csv_ (HEADERs,[ROW|ROWss])
:-
row__to__list(ROW,ROWs) ,
lists:nth1(1,ROWs,CITY_A) ,
QUERY_A=(lists:nth1(NTH,ROWs,DISTANCE)) ,
QUERY_B=(NTH > 1) ,
QUERY_C=(lists:nth1(NTH,HEADERs,CITY_B)) ,
QUERY=(QUERY_A,QUERY_B,QUERY_C) ,
ASSERT=assertz(distance(CITY_A,CITY_B,DISTANCE)) ,
forall(QUERY,ASSERT) ,
(loop) csv_ (HEADERs,ROWss)
.
row__to__list(ROW,ROWs)
:-
ROW=..[_|ROWs]
.
/*
?- csv('data/distances.csv').
true ;
false.
?- listing(distance).
distance('A', 'E', 3).
distance('A', 'G', 5).
distance('A', 'C', 7).
distance('A', 'B', 3).
distance('B', 'E', 4).
distance('B', 'G', 5).
distance('B', 'C', 6).
distance('B', 'B', 0).
distance('C', 'E', 4).
distance('C', 'G', 1).
distance('C', 'C', 0).
distance('C', 'B', 6).
distance('D', 'E', 5).
distance('D', 'G', 8).
distance('D', 'C', 3).
distance('D', 'B', 6).
?-
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment