The Issue is to input two lists and repeat the elements in first list according the corresponding number in other list.
e.g. repeat([2,3,4],[1,2,2]) would give [2,3,3,4,4]
fun repeat1(a,b)=if b>0 then a::repeat1(a,b-1) else [];
fun repeat([],[])=[]
|repeat([l1],[m1])=repeat1(l1,m1)
|repeat(l1::ln,m1::mn)=if length(l1::ln)=length(m1::mn) then if ln<>[] then repeat1(l1,m1)@repeat(ln,mn) else [] else [];
The error says
stdIn:36.64-36.66 Warning: calling polyEqual
stdIn:34.5-36.112 Warning: match nonexhaustive
(nil,nil) => ...
(l1 :: nil,m1 :: nil) => ...
(l1 :: ln,m1 :: mn) => ...
What are these for (I know its for base cases and inductive cases are not sufficient)? But how do I remove this warning even though this does not effect the output of the program?. Thanks.
You obviously forgot two base cases when only one in two lists is empty. Since these cases are malformed inputs, you can use exception to handle them:
After adding two new base cases, you don’t need to compare lengths of two lists in the last pattern anymore. The use of
lengthshould be avoided since it will traverse the whole list once more. And the pattern([l1], [m1])could be removed due to redundancy.So the new
repeatfunction looks like: