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 set-list of enumerated constants for week

Write a set/list of enumerated constants called week which contains days of the week. Have variable called today that is of type week. Allot value to today.

  Write down a recursive method to print all the permutations

write a recursive method to print all the permutations of a string. for instance for a string abc the printout is abc

  Write program using while loop to output squares of integers

Write a program that uses while loop to perform the following steps:Output the sum of all the even numbers between firstnum and secondnum inclusive. Output all the numbers and their squares between firstnum and secondnum inclusive.

  Design and implement class instance data for title of book

Design and implement a class called Book that contains instance data for the title, author, publisher, and copyright date. Define the Book constructor to accept and initialize this data.

  Formulate a linear programming model for given problem

A company produces two products that are processed on two assembly lines. Assembly line 1 has 100 available hours, and assembly 2 has 42 available hours. Formulate a linear programming model for this problem

  Write program to asks user to enter temperatures

Write down the C++ program which asks the user to enter 12 temperatures for each month in year and store them in one-dimensional array called "temps".

  Weekly demand for motorola cell phones at a retail store is

weekly demand for motorola cell phones at a retail store is normally distributed with a mean of 300 and a standard

  Discuss when scripting should be taught to individuals

Discuss when scripting should be taught to individuals who are learning software programming and discuss the advantages and disadvantages of doing so at specific times in their learning process.

  Create memo to grace to outline purposes of organization

Create a memo to Grace in which you outline purposes of organization and costs and benefits of becoming a member.

  Methods to declare and initialize one dimensional arraay

What is an array? Show different methods to declare and initialize one dimensional array. Draw how an integer array of 6 elements having base address 5001 is stored in memory.

  Discuss how the definition of privacy that is commonly used

Discuss how the definition of privacy that is commonly used (freedom from observation) may differ from the definition of privacy.

  What professional organizations might you join and websites

a write a 200- to 300-word short-answer response to the followingwhat professional organizations might you join and

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