Question: coded in f# For this hand-in you also need to consider scenarios where your solutions should return an error, i.e., an exception. The requirement is,

coded in f#

For this hand-in you also need to consider scenarios where your solutions should return an error, i.e., an exception. The requirement is, that no matter what input you pass to your function that fulfils the function type, then the function should return the intended answer or an exception. It is up to you to define the exceptions and whether they should carry extra information, like error messages.

Exercise 2.1 Write a function downTo : int -> int list

so that downTo n returns the n-element list [n; n-1; . . .; 1]. You must use if-then-else expressions to define the function.

Secondly define the function downTo2 having same semantics as downTo. This time you must use pattern matching.

Exercise 2.2 Write a function removeOddIdx : int list -> int list

sothatremoveOddIdx xsremovestheodd-indexedelementsfromthelistxs:

removeOddIdx [x0; x1; x2; x3; x4; ...] = [x0; x2; x4; ...] removeOddIdx [] = [] removeOddIdx [x0] = [x0] 

Exercise 2.3 Write a function combinePair : int list -> (int*int) list

so that combinePair xs returns the list with elements from xs combined into pairs. If xs contains an odd number of elements, then the last element is thrown away:

combinePair [x1; x2; x3; x4] = [(x1,x2);(x3,x4)] combinePair [x1; x2; x3] = [(x1,x2)] combinePair [] = [] combinePair [x1] = [] 

Hint: Try use pattern matching. Exercise 2. Write a function

explode : string -> char list sothatexplode sreturnsthelistofcharactersins:

explode "star" = [s;t;a;r] 

Hint: if s is a string then s.ToCharArray() returns an array of characters. You can then use List.ofArray to turn it into a list of characters.

Now write a function

explode2 : string -> char list similar to explode except that you now have to use the string function s.Chars (or .[]), where s is a string. You can also make use of s.Remove(0,1). The definition of explode2 will be recursive.

Exercise 2. Write a function implode : char list -> string

sothatimplode sreturnsthecharactersconcatenatedintoastring: implode [a;b;c] = "abc"

Hint: Use List.foldBack. Now write a function implodeRev : char list -> string

sothatimplodeRev sreturnsthecharactersconcatenatedinreverseorderintoastring: implodeRev [a;b;c] = "cba" Hint: Use List.fold.

1

Exercise 2. Write a function toUpper : string -> string

sothattoUpper sreturnsthestringswithallcharactersinuppercase: toUpper "Hej" = "HEJ"

Hint: Use System.Char.ToUpper to convert characters to upper case. You can do it in one line using implode, List.map and explode.

Write the same function toUpper1 using forward function composition ((f g) x = g(f x)) . Write the same function toUpper2 using the pipe-forward operator (|>) and backward function composition

(). Hint:isdefinedas(f g) x = (f o g) x = f(g(x)). Hint: |>isdefinedasx |> f = f x. The two operators are by default supported by F#. You can have F# interactive print the types:

> (<<);; val it : ((a -> b) -> (c -> a) -> c -> b) =  > (|>);; val it : (a -> (a -> b) -> b) =  > (>>);; val it : ((a -> b) -> (b -> c) -> a -> c) =  

Exercise 2. Write a function palindrome : string -> bool,

sothatpalindrome sreturnstrueifthestringsisapalindrome;otherwisefalse. A string is called a palindrome if it is identical to the reversed string, eg, Anna is a palindrome but Ann is

not. The function is not case sensitive.

Exercise 2. The Ackermann function is a recursive function where both value and number of mutually recursive calls grow rapidly.

Write the function

ack: int * int -> int that implements the Ackermann function using pattern matching on the cases of (m,n) as given below.

n + 1 if m = 0 A(m, n) = A(m 1, 1) if m > 0 and n = 0 A(m1,A(m,n1)) ifm>0andn>0

What is the result of ack(3,11). Notice: The Ackermann function is defined for non negative numbers only.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!