Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question (See Below): 2. In this assignment you can use all Prolog functions. (a) L1 and L2 are arbitrary numeric lists/trees. Make Prolog program addlst(L1,

Question (See Below):

2. In this assignment you can use all Prolog functions. (a) L1 and L2 are arbitrary numeric lists/trees. Make Prolog program addlst(L1, L2, Sum) where Sum is the sum of all components in L1 and L2. For example, the statement addlst([[1],2],[3|4]],[[1]],S] should return S = 11.

(b) List X contains n numbers x1, x2,...,xn. List Y contains k numbers y1, y2,...,yk. Write a Prolog program that that in the case n=k returns the value x1*y1+x2*y2+...xn*yn; in all other cases the program should return the error message Length error, as in the following examples:

?- sp([1,2,3] , [3, 2, 1], S]. S = 10. ?- sp([1,2,3,4], [3,2,1], S). S = Length error.

TO BE DONE IN PROLOG (SEE BELOW EXAMPLE):

image text in transcribed

image text in transcribed

HI CHEGG EXPERT PLEASE DO AS SOON AS POSSIBLE, REALLY URGENT.

The following sample data been added to the database. 1 2 3 4 5 6 AN male(tom, 25). %add data to the database male(tim, 26) male(jerry,24). female(tony, 18) female(lily, 19) female(lisa, 20). The rules have been written for the asked questions male(ton,25) add data to the database naletim, 26) male(erry,24) female(tony, 18) female(lity, 19) female(lisa, 20) person(Name, Age) - male(Name, Age); female(Name, Age) find name and age of person ncount(Count) - indall(x,malel X).L). Length(L.Count) find the count of male using Tindall function fcount(Count) - findall(x, female X).L). Length(.Count) find the count of male using findall function listal(L) - Tindall (X. (malel X) ; female(x)),L) find list of all ages.using find all count_elems([HIT). N. Acc. Count) :- H>N. count this person if he is older than N Accl is ACC + 1. increment the accumulator count elems(T. N. Acci, Count) check the rest of the list count elems([HIT). N. Acc. Count): Ha N. don't count this person if he is younger than N count clems (T. N. Acc, Count) check rest of list without incrementing acc) count elems(1). Count. Count) At the end, instantiate total with accumulator older (Age. Count) - ListAll().count_elens(L.Age, 0. Count) first find ist of all ages and then count function to find the saximum of list: maxlist(1, R. R) Send maxlist([xlxs). Ca. R)- X > Ca, maxlist(xs. X. R) > Co and X will be the max until here maxlist(XIXS]. Co, R) - X Ca, maxlist(Xs. Ca. R) X

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions