Re: Struggling with flatten function.

"Oliver Bandel [email protected] [ocaml_beginners]" <[email protected]> Thu, 29 Sep 2016 23:38:59 +0200
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <20160929233859.Horde.tINo98kufYz3Pv64kHjZoKS@webmail.in-berlin.de>
Hello,


Zitat von "Douglas Lewit [email protected] [ocaml_beginners]"  
<[email protected]> (Thu, 29 Sep 2016 15:10:56 -0500)

> So I figured out the following:
>
> let rec flatten = function
>                          |[ ] -> [ ]
>                          |head :: tail -> head @ flatten tail ;;
>
> This mimics the builtin List.flatten function from the List module.
[...]

So, you reinvented the wheel.



[...]
> [ [[1]; [2]; [3]] [[4; 5; 6]]; [[7]]; [[8]]; [[9]] ] because this is really
> an int list list list!  Yes, the flatten function works if we apply it
> twice, but how would some other function know to flatten the list twice?
[...]

If this would be allowed to be compiled by the OCaml-compiler,
re-flattening until the length did not change any more, would be the way
to find the end of the process.


>
> In terms of imperative logic I'm trying to continually flatten the list
> until there's no change.... but that will generate an Error and I don't
> know how to handle that.

What kind of error are you talking about?

I think, the OCaml-compiler would not allow you to compile
a code that allows this, because a typing-error would be detected.

So I guess, there is no way to make this possible,
because of the type system.

In other words: you would need to do an implementation in a different way.
Possibly with implementing your own data structures, e.g. with sum types.
But then  it is a different kind of problem - not builtin lists of  
builtin lists
anymore.

Ciao,
    Oliver