Reputation: 323
fun Dbt (nil,_) = nil
| Dbt (x::xs,y::ys) = (x::y)::(Dbt(xs,ys))
| Dbt (x::xs,nil) = [x]::(Dbt(xs,nil));
Is there a way of defining this function non-recursively by using higher order and or in built functions in sml??I have tried all I can but it seems I am not going anywhere.Any ideas will be appreciated thanks..
Upvotes: 3
Views: 224
Reputation: 31459
As you do not consume list but produce them, you won't be able to use the usual list-traversing operators (map, filter, fold...).
There is however one common and well-understood combinator for list production, which is
val unfold : ('a -> ('a * 'b) option) -> 'a -> 'b list
Unfortunately, this operator is not available in the basic SML library, so you may have to define it yourself.
fun Dbt (xs, ys) =
let fun Step (nil, _) = NONE
| Step (x::xs, y::ys) = SOME (x::y,(xs,ys))
| Step (x::xs, nil) = SOME ([x], (xs,nil))
in unfold Step (xs, ys)
Upvotes: 4