2) List Product Define a predicate list_prod/2 that takes a list of numbers as a
ID: 3597648 • Letter: 2
Question
2) List Product
Define a predicate list_prod/2 that takes a list of numbers as a first parameter and
determines the product of all of the list elements in the second parameter. Your
predicate should have the signature list_prod(+List, +Number). The product of an
empty list should be zero.
Examples:
?- list_prod([4,3], Product).
Product = 12.
?- list_prod([7,8,0,13], Product).
Product = 0.
?- list_prod([6,2,5,10], Product).
Product = 600.
?- list_prod([], Product).
Product = 0.
Define and test the Prolog predicates described below. Each of your predicates must have the
same name and signature as the examples below. Your predicates must behave properly on all
instances of valid input types. Your submission should consist of a single source code text file
that includes all facts, predicate definitions, and propositions.
Explanation / Answer
typedef struct /* define a context structure */ { ... } context; foreign_t my_function(term_t a0, term_t a1, foreign_t handle) { struct context * ctxt; switch( PL_foreign_control(handle) ) { case PL_FIRST_CALL: ctxt = malloc(sizeof(struct context)); ... PL_retry_address(ctxt); case PL_REDO: ctxt = PL_foreign_context_address(handle); ... PL_retry_address(ctxt); case PL_CUTTED: free(ctxt); PL_succeed; } } if ( PL_is_atom(t) ) { char *s; PL_get_atom_chars(t, &s); ...; } or char *s; if ( PL_get_atom_chars(t, &s) ) { ...; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.