, ,

[SOLVED] Csci 461 project one to six solutions

$25

File Name: Csci_461_project_one_to_six_solutions.zip
File Size: 348.54 KB

Categories: , , Tags: , ,
5/5 - (1 vote)

1. Install Standard ML of New Jersey to your computer
2. Write/Run SML functions on Standard ML of New Jersey Interactive System
3. Write the SML functions in a text editor, then run the code on Standard ML of
New Jersey Interactive System

1. Write a function cube of type int ! int that returns the cube of its parameter
2. Write a function sqsum of type int ! int that takes an non-negative integer n
and returns the sum of the squares of all the integers 0 through n. Your
function need not behave well on negative input.

3. Write a function max of type int list ! int that returns the largest element of
a list of integers. Your function need not behave well if the list is empty.
(refer to exercise 13 on page 85 of the textbook)

Step One: Install Standard ML of New Jersey to your computer.
To install on Window, please watch the video
https://www.youtube.com/watch?v=hDkG2aH2iqU
To install on Mac, you may watch the video at https://youtu.be/qdUnCK85CoI

Step Two: Write all functions in a text editor and save the file as
YourNameProjOne.sml.

Step Three: Run your code and test the correctness with data
Open terminal window. Use command ls see the folders/files in current folder. Use
command cd .. to go into upper level folder. Use command cd folder_name to go to
the subfolder. Navigate to the folder where you saved YourNameProjOne.sml file.
Now type command: sml

Then type command: use “YourNameProjectOne.sml”
The file name must be in “ “. If everything is correct, your functions are ready to use.
Sample Run on My Computer: my file was saved as CSCI461ProjOne.sml in folder
Documents/CSCI461VA/Project. The file name is: CSCI461ProjOne.sml. It contains
the following code:
(* this is problem one *)
fun cube x = x*x*x;
(* this is problem two *)
fun cube2 x:real = x*x*x;
Here (* … *) are comments
Here is the screen shot for what I have done:
The due date will be announced on Blackboard.

Write a quick sort function of type int list -> int list. Here is a review for quick sort
algorithm. First pick an element and call it pivot. (The head of the list is an easy
choice for the pivot.) Partition the rest of the list into two sublists, one with all the
elements less than the pivot and another one with all the elements not less than the
pivot. Recursively sort the sublists. Combine two sublists and pivot into final sorted
list.

The solution must follow the style of function merge sort on page 114. The help
functions must be made inside let.

Sample Run:
Please see the screen shot below to see the sample run
File to turn in:
Save your function in file names YourNameProjTwo.sml and submit it via
blackboard link.
Due Date:
Will be announced on blackboard.

1. Apply the concepts of folding
2. Apply the concepts of map
3. Implement mymap which works as built-in map function in sml
4. Apply foldr, foldl, and mymap to solve some problem in on line of code

The student shall turn in five functions. You should use mymap1 and mymap2 in
problem 3, 4, and 5 if it is needed. However, the best way to do so is you use built-in
map to finish problem 3, 4, and 5 first. After they work, replace map with mymap1
or mymap2. If they still work, you can say that you implement them all correctly. If it
doesn’t work, then you now that your mymap1 or mymap2 maybe incorrect.

1. Define a function mymap1 with the same type and behavior as built-in map
function but without using map. This should be one-line of code. Use foldr or
foldl) (refer to problem Exercise 24 on page 147)

2. Define a function mymap2 with the same type and behavior as map. No map,
foldr, or foldl can be used. (refer to problem Exercise 26 on page 147)

3. Write a function ordlist of type char list -> int list that take a list of characters
and returns the list of integer codes of those characters. For example, if you
evaluate ordlist [#”A”, #”b”, #”C”] you should get [65, 98, 67] (refer to
Exercise 2 on page 144)

4. Write a function mylength fo type ‘a list -> int that returns the length of a list.
You cannot use the built-in length function. (refer to Exercise 11 on page
145)

5. Write a function max of type int list -> int that returns the largest element of
a list of integers. Your function need not behave well if the list is empty.

Sample Run:
Please see the screen shot on next page
Due Date:
The file need to be submitted through blackboard link on time. The file name should
be YourNameProjThree.sml. Due date will be announced on blackboard.

• Write data constructor with parameters
• Write type constructor with parameters
• Write recursively defined type constructors

The student shall turn in definition of mylist and three functions to handle mylist. In
YourNameProjFour.sml file, the student needs to write the following type and
functions

1. Define mylist type that is the same as the one on page 172 of the textbook.
2. Define a function prod that returns the product of all integers in a mylist of
integers. (refer to problem Exercise 6 on page 178)

3. Write a function append of type ‘a mylist -> ‘a mylist -> ‘a mylist that takes
two mylist values, a and b, and return a mylist contains all the values in a
followed by all the values in b. (refer to Exercise 8 on page 178)

4. Write a function reverse of type ‘a list -> ‘a list that takes a mylist x and
return a mylist of all elements of x, in a reverse order. (refer to Exercise 7on
page 178)

Please see the screen shot on next page. Please notice how infixr 5 is used. (Refer to
examples on page 174). Also please notice that the result list may not fully
displayed. For instance, for list with element 1, 2, 3, 4, 5, it may only display as 1
CONS 2 CONS 3 CONS # CONS #
Due Date:
The file needs to be submitted through blackboard link on time. The file name
should be YourNameProjFour.sml. Due date will be announced on blackboard.

Please note that the students need to answer the following two questions:
1. How many solutions does it print?
2. How many of them are distinct?

Then the student need to modify the program so that only the distinct solutions will be print
out.
Instruction on how to write and run the SWI-Prolog program:
Step One: Write your program using any text editor. Save the program as
YourNameProjFive.swipl

Step Two: Open terminal window. Use cd command to navigate to the folder where you saved
your project four program.

Step Three: Type swipl. The SWI-Prolog program will run

Step Four: Type consult(‘YourNameProjFour.swipl’). (must have period at the end)

Step Five: Tyep length(X, 7), solution([w, w, w, w], X). (end with period) Use the semicolon after
each solution to make it print them all.

Add the following features to the adventure game from chapter 20 of the textbook.

1. There is a gate between the fork in the path and the mountaintop. The gate is a
separate location; that is, the player must move from at (you, fork) to at (you, gate) and
then to at (you, mountaintop).

2. To move forward through the gate, the player must first unlock it with a key.

3. The key is somewhere in the maze. The player must find it and explicitly pick it up

4. If the player tries to pass through the gate while still holding the key, he or she is killed
by lightning. (To get the treasure, the player must first open the gate, then put down the
key and then pass through)

Start from the code in this chapter, which is copied below (it is also on this book’s web site. A
link is provided in this module’s folder). Part of your implementation should be a general way
for the player to pick thing up, carry them, and put them down. Design your solution so that it
would be easy to add additional objects for the player to pick up and put down.

/*
This is a little adventure game. There are three
entities: you, a treasure, and an ogre. There are
six places: a valley, a path, a cliff, a fork, a maze,
and a mountaintop. Your goal is to get the treasure
without being killed first.

*/
/*
First, text descriptions of all the places in
the game.
*/
description(valley,
‘You are in a pleasant valley, with a trail ahead.’).
description(path,
‘You are on a path, with ravines on both sides.’).
description(cliff,
‘You are teetering on the edge of a cliff.’).
description(fork,
‘You are at a fork in the path.’).
description(maze(_),
‘You are in a maze of twisty trails, all alike.’).
description(mountaintop,
‘You are on the mountaintop.’).
/*
report prints the description of your current
location.
*/
report :-
at(you,X),
description(X,Y),
write(Y), nl.
/*

These connect predicates establish the map.
The meaning of connect(X,Dir,Y) is that if you
are at X and you move in direction Dir, you
get to Y. Recognized directions are
forward, right, and left.

*/
connect(valley,forward,path).
connect(path,right,cliff).
connect(path,left,cliff).
connect(path,forward,fork).
connect(fork,left,maze(0)).
connect(fork,right,mountaintop).
connect(maze(0),left,maze(1)).
connect(maze(0),right,maze(3)).
connect(maze(1),left,maze(0)).
connect(maze(1),right,maze(2)).
connect(maze(2),left,fork).
connect(maze(2),right,maze(0)).
connect(maze(3),left,maze(0)).
connect(maze(3),right,maze(3)).
/*
move(Dir) moves you in direction Dir, then
prints the description of your new location.
*/
move(Dir) :-
at(you,Loc),
connect(Loc,Dir,Next),
retract(at(you,Loc)),
assert(at(you,Next)),
report,
!.
/*

But if the argument was not a legal direction,
print an error message and don’t move.
*/
move(_) :-
write(‘That is not a legal move.
’),
report.
/*
Shorthand for moves.
*/
forward :- move(forward).
left :- move(left).
right :- move(right).
/*

If you and the ogre are at the same place, it
kills you.
*/
ogre :-
at(ogre,Loc),
at(you,Loc),
write(‘An ogre sucks your brain out through
’),
write(‘your eye sockets, and you die.
’),
retract(at(you,Loc)),
assert(at(you,done)),
!.
/*

But if you and the ogre are not in the same place,
nothing happens.
*/
ogre.
/*
If you and the treasure are at the same place, you
win.
*/
treasure :-
at(treasure,Loc),
at(you,Loc),
write(‘There is a treasure here.
’),
write(‘Congratulations, you win!
’),
retract(at(you,Loc)),
assert(at(you,done)),
!.
/*

But if you and the treasure are not in the same
place, nothing happens.
*/
treasure.
/*
If you are at the cliff, you fall off and die.
*/
cliff :-
at(you,cliff),
write(‘You fall off and die.
’),
retract(at(you,cliff)),
assert(at(you,done)),
!.
/*
But if you are not at the cliff nothing happens.
*/
cliff.
/*
Main loop. Stop if player won or lost.
*/
main :-
at(you,done),
write(‘Thanks for playing.
’),
!.
/*
Main loop. Not done, so get a move from the user
and make it. Then run all our special behaviors.
Then repeat.
*/
main :-
write(‘
Next move — ‘),
read(Move),
call(Move),
ogre,
treasure,
cliff,
main.
/*
This is the starting point for the game. We
assert the initial conditions, print an initial
report, then start the main loop.
*/
go :-
retractall(at(_,_)), % clean up from previous runs
assert(at(you,valley)),
assert(at(ogre,maze(3))),
assert(at(treasure,mountaintop)),
write(‘This is an adventure game.
’),
write(‘Legal moves are left, right, or forward.
’),
write(‘End each move with a period.

’),
report,
main.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] Csci 461 project one to six solutions[SOLVED] Csci 461 project one to six solutions
$25