SCHEME - Give a short answer for each of the following questions. 1. If we evalu
ID: 3838843 • Letter: S
Question
SCHEME - Give a short answer for each of the following questions.
1. If we evaluate the following expressions, as Scheme would, what would be the result?
(define x 3)
(define y 10)
a. (list x y)
b. (list 'x 'y)
2. Define a function to compute the cube of a number
3. Define a function that takes a list of numbers and returns a list of their square roots.
Thus (sqrts 1.0 2.0 4.0) should return (1.0 1.41421 2.0).
(Hint: the answer to the question about how to calculate a square root is a built-in function called sqrt and yes, you can use map ).
4. Finish writing the program, called 'got_milk', that is bellow. It checks a list for the word "milk". It should return true (#t) or false (#f). We can
assume the list is of atoms (that it has no sub-lists). You might want to use the build-in function eq? to test for equality e.g. (eq? 'a (car '(a
b))) => #t
(define (got_milk alist)
(cond ((null? alist) #f)
))
> (got_milk '("bread", "cheese", "tomates", "milk")) =>#t
5. Write a function, called 'sum' , that sums all the elements of a list.
Extra Credit, write this function so that it sums up all the elements of a list including all sub-lists.
6. Consider the following Scheme function and then determine:
(define foo (list 1 2 5 6 9))
(define bar (list 0 2 5 7 9))
(define (mystery list1 list2)
(cond
( (null? list1) '() )
( (member (car list1) list2 )
(cons (car list1) (mystery (cdr list1) list2)))
(else (mystery (cdr list1) list2))
))
What would be the result (what would be the output) if we ran the command below?
>(mystery foo bar)
=>
PROLOG - Give a short answer for each of the following questions.
1. What would be the output from ?- append([a,b,c],[one,two,three], Result).
2. Given:
mother(mary, sue).
mother(mary, bill).
mother(sue, nancy).
mother(sue, jeff).
mother(jane, ron).
father(john, sue).
father(john, bill).
father(bob, nancy).
father(bob, jeff).
father(bill, ron).
parent(A,B) :- father(A,B).
parent(A,B) :- mother(A,B).
grandparent(C,D) :- parent(C,E), parent(E,D).
sibling(X,Y) :- parent(P,X), parent(P,Y), X=Y.
a. Define a new relation 'cousin' that defines the relationship between any two people whose parents are siblings.
b. Write a query for this expanded program that will identify all people who are cousins.
For example, the cousins of Ron would be ?- (cousin ron, Who).
3. If we had the following:
has_flu(rebecca).
has_flu(john).
has_flu(X):- kisses(X,Y),has_flu(Y).
kisses(janet,john).
What would the following return?
?- has_flu(janet).
4. If we had a one-way road links 6 towns. (i.e. town1----->-----town2---->----town3---->----town4--->----town5---->---town6 )
Finish writing the program that can work out if you can travel on that road and can_get from A to B.
connected(town1,town2).
connected(town2,town3).
connected(town3,town4).
connected(town4,town5).
connected(town5,town6).
can_get(X,Y) :- connected(X,Y).
can_get(X,Y) :- connected(X,Z), can_get( _____ , _____ ).
5. Suppose that we are working with the following (yes, the names are from the movie Pulp Fiction):
loves(vincent, mia).
loves(marcellus, mia).
loves(mia, marcellus).
loves(pumpkin, huney_bunny).
loves(huney_bunny, pumpkin).
jealous(A,B):- loves(A,C), loves(B,C), A = B.
a. Now we pose we have the query
?- jealous(X,Y).
What would we get back if we asked for all solutions? Show what would be the output.
b. Would it make a difference if we removed the last A=B ? If yes how?
Explanation / Answer
#include<stdio.h>
#include<conio.h>
void main()
{
int a,s,c;
clrscr();
printf(" Enter A Number: ");
scanf("%d",&a);
s=a*a; //Square = number * number
c=s*a; //Cube = Square * number
printf(" Square of %d is = %d",a,s);
printf(" Cube of %d is = %d",a,c);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.