11/28

Context-free grammar

S – ab

S – A

A – a

A – b

 

CAPS: non-terminal (can’t stop till all gone)

lowercase: terminal symbols

 

S => ab, a, b

  1. – ab
  2. – A
    – a
  3. – A

    – b

 

parse.pl

> parse([s(3)], S).

> parse([a(3)], S).

> parse([b(3)], S).

> parse([c(3)], S).

 

parse([], []).

parse([H|T],[H|T1]) :-

integer(H),

parse( T, T1 ).

parse([H|T], Result) :-

rep( H, H1 ),

append( H1, T, Newlist), //Concatenation

parse( Newlist, Result).

 

 

List-doubler

listDbl( [], [] ).

listDbl( [H|T], [H1|T1] ):-

H1 is 2*H,

listDbl(T, T1).

 

> listDbl([], S).

> listDbl([1], S).

> listDbl([1, -3, 4, 5], S).

 

 

H99

 

Advertisements