Vincenzo aka Nick Name | 3 Mar 2004 03:04
Picon
Favicon

Trouble with [in,string]char* with hdirect

Hi all,

I am experimenting with hdirect for the first time, and I can't figure 
out how to call an haskell function of type String -> IO Int from C. I 
already can compile and use a function of type Char -> IO Int, so I 
assume to be using the right command line arguments.

I have, in Math.idl

----
module Math {
  int munlink([in,string]char *s);
};
----

in Math.hs

----
module Math where

munlink :: String -> IO Int
munlink s = putStr ("Hey!" ++ s ++ "\n") >> return (-1)
----

This is the generated .hs file:

module MathProxy where

import Prelude (fromEnum, toEnum)
import qualified Prelude
(Continue reading)

Georg Martius | 3 Mar 2004 14:44
Picon

State Monad

Hi folks,

I have a question to the State Monad or Monads in general - I'am not sure. 
Lets assume I have functions to transform strings. I can use the State 
Monad as follows:

strTrans :: String -> String
strTrans s = s ++ s

f :: State String ()
f = do put "hallo"	
	 modify strTrans
	 modify strTrans'
	 ...

Up to this point everything is clear.
Now I have also functions to map from (a, String) -> (a,String).
I could write:

modifyT :: ((a, String) -> (a, String)) -> a ->  State String a
modifyT trans a = do str <- get
	 	     let (a', str') = trans (a, str)
		     put str'
	 	     return a'

f :: State String ()
f = do put "hallo"
	 modify strTrans
	 i <- modifyT strIntTrans 4    -- strIntTrans :: (Int, String) -> (Int, 
String)
(Continue reading)

Wolfgang Jeltsch | 3 Mar 2004 15:03

Re: State Monad

Am Mittwoch, 3. März 2004 14:44 schrieb Georg Martius:
> [...]

> Now I have also functions to map from (a, String) -> (a,String).  I could
> write:
>
> modifyT :: ((a, String) -> (a, String)) -> a ->  State String a
> modifyT trans a = do str <- get
>                      let (a', str') = trans (a, str)
>                      put str'
>                      return a'
>
> f :: State String ()
> f = do put "hallo"
>          modify strTrans
>          i <- modifyT strIntTrans 4
>              -- strIntTrans :: (Int, String) -> (Int, String)
>          i' <- modifyT strIntTrans i    
>          ...

> But this is obviously awkward.  How can I stick two Monads in each other? I
> could't figure out how to use StateT.

StateT is one solution.  See
    http://www.haskell.org/pipermail/haskell/2004-January/013330.html
and the follow-ups (available via the "next message" links).  With StateT, 
modifyT may be written as follows (untested code):
    modifyT ::  ((a, String) -> (a, String)) -> StateT a (State String) ()
    modifyT trans
        = do 
(Continue reading)

Iavor S. Diatchki | 3 Mar 2004 19:34
Picon

Re: State Monad

hi,

Georg Martius wrote:

> Now I have also functions to map from (a, String) -> (a,String).
> I could write:
>
> modifyT :: ((a, String) -> (a, String)) -> a ->  State String a
> modifyT trans a = do str <- get
>               let (a', str') = trans (a, str)
>              put str'
>               return a'
>
> f :: State String ()
> f = do put "hallo"
>      modify strTrans
>      i <- modifyT strIntTrans 4    -- strIntTrans :: (Int, String) -> 
> (Int, String)
>      i' <- modifyT strIntTrans i   
>      ...

>
> But this is obviously awkward. How can I stick two Monads in each 
> other? I could't figure out how to use StateT. Any help would be 
> appreciated.
>
> Thanks!
>  Georg

could you be a little more specific on what you are trying to do?
(Continue reading)

Wolfgang Jeltsch | 3 Mar 2004 20:16

Re: State Monad

Am Mittwoch, 3. März 2004 18:15 schrieb Georg Martius:
> Thanks for your answer. I got it now.
> It works with lift instead of liftM.

Yes, of course.

> Georg

Wolfgang
Tom Pledger | 3 Mar 2004 20:51
Picon

Re: State Monad

Georg Martius wrote:
[...]

> I could write:
>
> modifyT :: ((a, String) -> (a, String)) -> a ->  State String a
> modifyT trans a = do str <- get
>               let (a', str') = trans (a, str)
>              put str'
>               return a'
>
> f :: State String ()
> f = do put "hallo"
>      modify strTrans
>      i <- modifyT strIntTrans 4    -- strIntTrans :: (Int, String) -> 
> (Int, String)
>      i' <- modifyT strIntTrans i   
>      ...
>
> But this is obviously awkward.

[...]

Hi.

People have already replied about the state monad aspect, but there's 
another small improvement I'd like to suggest.

Look at what modifyT does with 'trans' and 'a'. They are always used 
together. So, how about combining them *outside* the definition of modifyT?
(Continue reading)

Chris Moline | 4 Mar 2004 01:13
Picon
Favicon

a rant from a stoned suicidal person

i want to start off by apologizing. im lonely. i dont know anyone.
anyone at all who is liek me. im different. everyone else is content to
just watch tv all the time and then on weekends go and get drunk. im
not.

i am not threatening anything here when i say waht i am about to say.

i know that waht i am about to say is not appropriate for haskell-cafe.
i know this. but this needs to be said. im going to kill myself

not right now. no you dont need to do anything. this is a plea. im
begging you. ok see here im on my knees.

my not-stoned self is going to hate me for this. but he needs the help
and i know hes going to forgive me. because im him:) isnt that funny?

i have no friends. none at all. im surrounded by motherfucking idiots

*IDIOTS*

i dont hate them. ok, im lying i hate them. i hate you. i hate this
mothierfucking list.

all i want is a goddamned friend who is *like me*
i like thinking about shit.
i like philosophy and math and all that stuff.

dont suggest going to college. im sorry but ive found this out the hard
way. im a loser. im a motherfucking loser.

(Continue reading)

Tom Pledger | 4 Mar 2004 01:23
Picon

Re: a rant from a stoned suicidal person

There are a lot of hoaxes on the internet, but just in case you're for 
real, here's an idea...

Step 1: Take 20 minutes to get some fresh air.

Step 2: When you get back to your computer, take 5 minutes to read 
Desiderata (http://shell.world-net.co.nz/~unikorn/desiderata.htm), 
especially the "everywhere life is full of heroism" bit. Sometimes we 
can't hear the heroes over the noise of the motherfucking idiots.

Step 3: Say either "Thanks, Tom" or "You're just another motherfucking 
idiot, Tom".
Georg Martius | 4 Mar 2004 11:08
Picon

Re: State Monad

Hi,

thanks for your suggestion. The thing is, that I don't want to change the 
type of my transformation functions.

To answer Iavor's question: I have basically two types of transformation 
functions. One StringTransformation (String -> String) and one 
transformation with a string and something (e.g. random generator) 
((a,String) -> (a,String)). The vast majority of transformation I use are 
 from the first type. I was looking for a nice way to write this in a the 
compact style. I haven't thought about exceptions yet.

My current approach is as follows:

withString :: String -> State String () -> String
withString state monad = execState monad state

withStringAndT :: String -> t -> StateT t (State String) () -> String
withStringAndT state t monad = execState (execStateT monad t) state

modifyT ::  ((t, String) -> (t, String))
	-> StateT t (State String) ()
modifyT trans
         = do s <- lift get
	     t <- get
	     let (t', s') = trans (t, s)
	     lift (put s')
	     put t'

now I can use either
(Continue reading)

David Sankel | 4 Mar 2004 21:19
Picon
Favicon

Fun with Haskell, runST, MArray, and a few queens.

Hello Enthusiasts,

  My fiancee was assigned the n-queens problem in her Data Structures class. 
It was a study in backtracking.  For those unfamiliar with the problem: one
is given a grid of n x n.  Return a grid with n queens on it where no queen
can be attacked by another.

  Anyway, I decided to try an implementation in Haskell (as I often do with
her assignments).  Instead of the imperative approach (adding a queen and
then getting rid of it), I opted for a functional one (the grid is passed to
recursive calls, etc.).

  The interesting thing about this assignment is the runtimes:

(n=10)
ghc      58.749s
ghc -O   12.580s
javac     1.088s

  The Haskell version takes significantly longer (and it gets worse for
larger inputs).  So it seems that imperative algorithms are much better for
certain problems.

  Since Haskell is supposed to have the ability to run imperative algorithms,
I was wondering if any of you could explain how runST and MArray could be
used to solve this problem (or is there a better way?).  I am also interested
in the run times you get with these two implementations of the n-queens
problem.

David
(Continue reading)


Gmane