Sale!

CS2104  Problem Set 5 solution

$24.99 $17.49

Original Work ?

Download Details:

  • Name: PS5.zip
  • Type: zip
  • Size: 1.49 KB

Category: You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

5/5 - (2 votes)

Problem 1 [2 marks, submit to PS6P01]
Complete the following Haskell function so that it complies to the specification given below.
split = foldr –- only fill in at the indicated place,
(\ x y -> — do not change anything else in the skeleton
— …. < your code here > …..
[[]]
The function split must split a list of numbers into positive and negative segments. The
following is a possible interaction for split:
ghci> split [1,2,3,4,-1,-2,-3,4,3,4,2,-5,-4,-3,-3,-2,-1,10]
it:- [[1,2,3,4],[-1,-2,-3],[4,3,4,2],[-5,-4,-3,-3,-2,-1],[10]]
In completing the function, only add code at the specified position. Do not change anything
else in the skeleton of the function.
Submission: In IVLE, in the cs2104 workbin, you will find a folder called “Homework
submissions”. In that folder, there are currently 2 subfolders: PS6P01, and PS6P02. The
last two digits of the folder name indicate the solution that is to be submitted into that folder: the
solution to Question 1 into PS6P01, and so on (that is, you need to submit 2 separate solutions to
2 problems). A solution should consist of a single text file that can be compiled, or loaded into the
interpreter of interest and executed. You should provide as much supplementary information about
your solution as you can, in the form of program comments. Moreover, if you work in a team,
state the members of the team at the beginning of the file, in a comment. You do not need to
submit the same file twice, one submission per team is sufficient.
Marks: 6
marks
Problem 2 [4 marks, submit to PS6P02]
Consider the following C program:
#include <stdio.h>
// builds a string showing the sequence of moves that
// solves the towers of hanoi puzzle — moving all discs
// from peg ‘a’ to peg ‘b’ using peg ‘c’ as aux
// n is the number of discs, and assumed to be less than 10
void hanoi(char ** p, int n, int a, int b, int c) {
if ( n == 0 ) return ;
hanoi(p,n-1,a,c,b) ;
**p = ‘0’+(char)a ;
(*p) ++ ;
**p = ‘ ‘ ;
(*p) ++ ;
**p = ‘t’ ;
(*p) ++ ;
**p = ‘o’ ;
(*p) ++ ;
**p = ‘ ‘ ;
(*p) ++ ;
**p = ‘0’+(char)b ;
(*p) ++ ;
**p = ‘\n’ ;
(*p) ++ ;
hanoi(p,n-1,c,b,a) ;
}
int main() {
char a[1000] ; // string buffer
char *p = a ; // current position in string
hanoi(&p,4,1,2,3) ; // build the string of moves for 4 discs
*p = ‘\0’ ; // terminate the string
// printf(a) ; // string could be printed, but not in VAL code
}
The program builds a string which, when printed, lists the sequence of moves for the tower of
hanoi puzzle with 4 discs, moving all discs from the first peg to the second peg, using the
third peg as an auxilliary. Translate the above program into VAL.