Question
You will need to use Erlang language to solve following tasks: This question concerns strings, like foo and football. (a) Define a function init in
You will need to use Erlang language to solve following tasks:
This question concerns strings, like "foo" and "football". (a) Define a function init in Erlang that takes two strings and returns true if the first string is an initial segment of the other: in other words, if the first string can be extended to make the second. For example,
init("foo","football") should be true, and init("foo","ballfoot") should be false.
(b) Define a function drop that takes an integer N and a string St, and which returns the string St with the first N elements dropped, if the string has that many elements (and as many as possible otherwise). For example,
drop(2,"football") should be "otball", and drop(12,"football") should be "".
(c) Using the functions init and drop, or otherwise, define a function subst that takes three strings, Old, New and St. The function returns a string in which the first occurrence of Old is replaced by New; if it doesnt occur, then the string is returned unchanged. For example:
subst("foo","bar","football") should be "bartball", and subst("foo","bar","ballfoot") should be "ballbart", and subst("foo","bar","footfoot") should be "bartfoot".
(d) How would you modify your answer to (c) so that all occurrences of Old are replaced by New? How would you modify it so that only the last occurrence of Old was replaced?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started