CS代考 =========================

=========================

_________________________________________________________________________________________________
Question 1 Prolog

Copyright By PowCoder代写 加微信 powcoder

Please note:
· You must comment your code.

a) Occurrence

Write a predicate numOccur in Prolog which is true if the number E occurs R times in the list. For example:
?- numOccur(4, [1, 7, 5, 0, 4, 1, 4, 6], R).
?- numOccur(4, [1, 7, 5, 0, 8, 1, 8, 6], R).
?- numOccur(4, [ ], R).

b) Flipping
Write a predicate flip. Flip is to exchange the order of pairs itself and in the list.
?- flip( [ (a, b), ( c, d)], L )
L = [ (d,c), (b,a) ].

?- flip( [ (a, b), ( c, d), (e, f), (g, h), (1, 2)], L ).
L = [(2, 1), (h, g), (f, e), (d, c), (b, a)].

?- flip( [], L ).

?- flip( [(1,2)], L ).
L = [(2, 1)].

Question 2 Scheme

Please note:
· You are not allowed to use set! in answering this question.
· You must comment your code

a) Occurrence
Write a function numOccur in Scheme which has arguments a number E and a list L and returns how many times the number E occurs in the list L. For example:

(numOccur 4 ‘(1 7 5 0 4 1 4 6))
(numOccur 4 ‘(1 7 5 0 8 1 8 6))
(numOccur 4 ‘())

b) Frequency
Write a Scheme function that accepts a list of numbers L and returns the frequency (number of times) of each number occurs in the list. The frequency is to be returned in a list of lists where each list contains the number as car and its frequency as the second element in the list as in the following examples:

(frequency `())
(frequency `(1 5 2 7 1 6 1 6 4))
· ((1 3) (2 1) (4 1) (5 1) (6 2) (7 1))

(frequency `(1 5 9 7 -1))
· ((-1 1) (1 1) (5 1) (7 1) (9 1))

Question 3 Go

a) Methods

Complete the following program (also available as source file) by supplying the function printMeal and completing the main routine to produce the output (exactly as shown):

Main: Schnitzel at 15.50
Desert: Pumpkin Pie at 5.60
Total: 21.10

Note: Do not fill the gaps below but submit your solution as golang source file.

package main

import “fmt”

type Desert struct {
Name string
Price float32

type MainCourse struct {
Name string
Price float32

type Meal struct {
MainCourse
Total float32

func main() {

m := Meal{ _________________________________________________}

// Calculate the total price of the main course plus desert
m.Total = _________________________________________________________

m.printMeal()

b) Go Routines and Channels

Complete the go routine in the lambda below. The go routine is to send the numbers in the array on the channel and then close the channel.

package main

import “fmt”

func main() {
numbers := []int{216, 218, 221, 260}
ch := make(chan int)

// Your solution
// You will need to insert code in the source file and not

________________________________________________________

______________________________________________________

if num, ok := <-ch; !ok { fmt.Println("Channel closed") fmt.Println(num) 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com