function - removing alternating elements on a list in scala -
i trying write recursive function in scala takes in list of strings , returns list alternating elements original list:
for example:
list = {"a","b","c"} list b = {"a","c"}
the head should included.
def removealt(list:list[string], str:string):list[string]=lst match{ case nil=> list() case => head::tail if(head == true) removealternating(list,head) else head::removealternating(list,head)
i stack overflow error.
i understand code incorrect trying understand logic on how accomplish recursion , no built in classes.
def remove[a](xs:list[a]):list[a] = xs match { case nil => nil case x::nil => list(x) case x::y::t => x :: remove(t) }
if list empty, return empty list. if we're @ last element of list, return that. otherwise, there must 2 or more elements. add first element alternate elements of rest of list (and omit second element)
Comments
Post a Comment