How do I extract information from inner parameters in Haskell? -


in of programming languages support mutable variables, 1 can implement java example:

interface accepter<t> {     void accept(t t); }  <t> t getfromdoubleaccepter(accepter<accepter<t>> acc){     final list<t> l = new arraylist<t>();     acc.accept(new accepter<t>(){          @override         public void accept(t t) {             l.add(t);         }      });     return l.get(0); //not being called? exception! } 

just not understand java, above code receives can can provided function takes 1 parameter, , supposed grape parameter final result.

this not callcc: there no control flow alternation. inner function's parameter concerned.

i think equivalent type signature in haskell should be

getfromdoubleaccepter :: (forall b. (a -> b) -> b) -> 

so, if can gives function (a -> b) -> b type of choice, must have a in hand. job give them "callback", , keep whatever sends in mind, once returned you, return that value caller.

but have no idea how implement this. there several possible solutions can think of. although don't know how each of them work, can rate , order them prospected difficulties:

  • cont or contt monad. consider easiest.

  • rws monad or similar.

  • any other monads. pure monads maybe consider harder.

  • use standard pure functional features lazy evaluation, pattern-matching, fixed point contaminator, etc. consider hardest (or impossible).

i see answers using of above techniques (and prefer harder ways).

note: there should not modification of type signature, , solution should same thing java code does.

update

once seen commented out getfromdoubleaccepter f = f id realize have made wrong. use forall make game easier looks twist makes easy. actually, above type signature forces caller pass whatever gave them, if choose a b implementation gives same expected result, just... not expected.

actually came mind type signature like:

getfromdoubleaccepter :: ((a -> ()) -> ()) -> 

and time harder.

another comment writer asks reasoning. let's @ similar function

getfunctionfromaccepter :: (((a -> b) -> b) -> b) -> -> b 

this 1 have naive solution:

getfunctionfromaccepter f = \a -> f $ \x -> x 

but in following test code fails on third:

exemain =     print $ getfunctionfromaccepter (\f -> f (\x -> 10)) "example 1" -- 10     print $ getfunctionfromaccepter (\f -> 20) "example 2" -- 20     print $ getfunctionfromaccepter (\f -> 10 + f (\x -> 30)) "example 3" --40, should 30 

in failing case, pass function returns 30, , expect function back. final result in turn 40, fails. there way implement doing just thing wanted?

if can done in haskell there lot of interesting sequences. example, tuples (or other "algebraic" types) can defined functions well, since can type (a,b) = (a->b->())->() , implement fst , snd in term of this. , this, way used in couple of other languages not have native "tuple" support features "closure".

the type of accept void accept(t) equivalent haskell type t -> io () (since every function in java io). getfromdoubleaccepted can directly translated as

import data.ioref  type accepter t = t -> io ()  getfromdoubleaccepter :: accepter (accepter a) -> io getfromdoubleaccepter acc =     l <- newioref $ error "not called"     acc $ writeioref l     readioref l 

if want idiomatic, non-io solution in haskell, need more specific actual end goal besides trying imitate java-pattern.

edit: regarding update

getfromdoubleaccepter :: ((a -> ()) -> ()) -> 

i'm sorry, signature in no way equal java version. saying a, given function takes function takes a doesn't return or kind of side effects, want somehow conjure value of type a. implementation satisfies given signature essentially:

getfromdoubleaccepter :: ((a -> ()) -> ()) -> getfromdoubleaccepter f = getfromdoubleaccepter f 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -