R code for AIMA