Write an F function which takes an integer x

Assignment Help Programming Languages
Reference no: EM132223349

Programming Language Concepts Assignment -

This is a programming assignment in F#. Download the accompanying F# source file hw1.fsx and enter your solutions in it where indicated. When you are done, submit it as instructed on Piazza. Each of your answers must be free of static errors. You may receive no credit for problems whose code contains syntax or type errors.

In the following problems do not use any mutable variables (i.e., variables declared with let mutable) or any mutable predefined types such as references, mutable records, arrays, and so on. Define your functions recursively as needed and take advantage of the match operator.

1. Basic functional programming in F#

F# has a predefined list datatype. Strictly speaking, however, it is not necessary, For instance, integer lists could be simulated by the following user defined datatype

type ilist =

E

| L of int * ilist

where the constructor E stands for the empty list and the constructor L builds a new list by adding a number in front of another list. Then one can represent, say, a list with elements 1,4,6,7, in that order, with the ilist value L(1, L(4, L(6, L(7, E)))).

Using pattern matching and recursion, implement the following functions manipulating ilist values.

1. Write an F# function sum: ilist -> int which takes as input a ilist l and returns the sum of all the elements in l. For example, sum (L(1, L(3, L(3, E)))) is 7, while sum E is 0.

2. Write an F# function elem: int -> ilist -> int which takes a positive integer n and a list l, and returns the nth element of l if the list has at least n elements, and fails (using failwith) with message "Index out of bound" otherwise. For example, elem 2 (L(3, L(21, L(11, E)))) is 21.

3. Write an F# function isIn: int-> ilist -> bool which takes an integer x and an ilist l and returns true if and only if x occurs in l.

4. Write an F# function remove: int -> ilist -> ilist which, given an integer x and a list l, "removes" all the occurrences of x from l, that is, returns a list containing (in the same order) all and only the elements of l that differ from x.

For example, remove 2 (L(1, L(2, L(3, L(3, L(2, E)))))) is L(1, L(3, L(3, E))), remove 5 (L(1, L(2, L(3, E)))) is (L(1, L(2, L(3, E)))).

5. Write an F# function move: ilist -> ilist -> ilist which takes two lists and returns the result of inserting the values of the first list into the second, in reverse order. For example, move (L(1, L(2, L(3, E)))) (L(4, L(5, E))) is L(3, L(2, L(1, L(4, L(5, E))))).

6. Use function move to implement the function reverse: ilist -> ilist that returns the result of reversing its input list. For example, reverse (L(1, L(2, L(3, E)))) is L(3, L(2, L(1, E))).

2. Programming functional style with binary trees

Consider a variant of the binary tree algebraic datatype seen in class

type btree = Leaf of int | Node of btree * int * btree

and a new datatype defined as follows:

type finding = NotFound | Found of int

1. Write a function size: btree -> int which, given a tree t, returns the number of (non-leaf) nodes in it.

2. Write a function leftmost_nl: btree -> finding which takes as input a tree t, and returns NotFound if t is a leaf and otherwise returns (Found x) where x is the leftmost non-leaf value in a non-leaf node in t.

Observe that the leftmost non-leaf value of a tree of the form Node (t1, x, t2) is the same as the leftmost non-leaf value of t1 if t1 is not a leaf and is x otherwise.

3. Write a function mirror: btree -> btree which takes a tree t a returns the mirror image of t, that is, the tree obtained from t by swapping every left subtree with its corresponding right subtree.

4. Write a function leftReplace: int -> btree -> btree which, given an integer x and a btree t, returns a tree identical to t except that the leftmost value of t, if any, is replaced by x in the new tree.

The function should be such that leftReplace x t equals t when t does not have a leftmost node.

Note that differently from question 2, here the leaf values must also be considered.

5. Write a function toString nz: btree -> string which takes a tree t a returns a string consisting of all the non-zero numbers stored in t separated by white spaces. The order of the numbers in the list should correspond to a preorder traversal of the tree (root, left subtree, right subtree). The string should separate its numbers by exactly one space character and contain no initial or final spaces.

Attachment:- Assignment Files.rar

Reference no: EM132223349

Questions Cloud

?how it can apply to hofstede cultural six dimensions : Latin America it is used for lead generation and digital sales and In Middle east countries Facebook is more used for running various healthcare campaigns
Discuss the causes of poor-quality products or services : Write an essay in response to a question that will be provided - Discuss the causes of poor-quality products or services in an organization you are familiar
Create a bad news business letter : I need help to create a bad news business letter. The letter should be written to Mary Lamb; 4000 Lima Street, Mercury, Oregon 97309.
Executive summary describing wal-mart corporate strategy : Using the Wal-Mart running cases from chapter 1 through chapter seven, conduct an executive summary describing Wal-Mart's corporate strategy
Write an F function which takes an integer x : CS:3820 Programming Language Concepts Assignment - Write an F# function isIn: int-> ilist -> bool which takes an integer x and an ilist l
Safety stock should be added in addition to cycle inventory : To cope with demand uncertainty, how much safety stock should be added in addition to the cycle inventory you have found in Part A?
Explain what inventory planning and ordering system : Explain what inventory planning and ordering system you are going to use in this case and why.
Advise on the merits of the liquidators arguments : Advise on the merits of the liquidators arguments in British company law, indicating whether or not Mr Lay should be personally liable for Enrons debts
Capacity for change to rapidly deliver each outcome : 1. As it relates to change champions, there are literally hundreds of books, articles, studies and theories on change?

Reviews

len2223349

1/27/2019 9:31:34 PM

This is a programming assignment in F#. You can do it alone or in a team two people. In the latter case, you are responsible for contacting other students and form a team. Download the accompanying F# source file hw1.fsx and enter your solutions in it where indicated. When you are done, submit it as instructed on Piazza. Each of your answers must be free of static errors. You may receive no credit for problems whose code contains syntax or type errors. Pay close attention to the specification of each problem and the restrictions imposed on its solution. Solutions ignoring the restrictions may receive only partial credit or no credit at all.

len2223349

1/27/2019 9:31:28 PM

In the following problems do not use any mutable variables (i.e., variables declared with let mutable) or any mutable predefined types such as references, mutable records, arrays, and so on. Define your functions recursively as needed and take advantage of the match operator. You do not need, and should not use, any F# library functions unless instructed otherwise. However, you can use your own auxiliary functions if you prefer. Do not worry about the efficiency of your solution. Strive instead for cleanness and simplicity. Unnecessarily complicated code may not receive full credit. Do the assigned readings before starting attempting this assignment. If you do not, you might find the assignment exceedingly hard. Be sure to review the syllabus for details about this course's cheating policy.

Write a Review

Programming Languages Questions & Answers

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd