Re: [Haskell-cafe] Type question re: map

2009-03-06 Thread sam lee
map :: (a - b) - [a] - [b]

map takes a function and transforms a list of a's to b's.

map succ [1,2,3]
== [succ 1, succ 2, succ 3]
== [2, 3, 4]

In general,
map f :: [a] - [b]
where a is domain-type of f and b is image-type of f (f :: a - b).

map map [x, y, z]
== [map x, map y, map z]

so, x,y,z should functions of type (a - b).
and the result list, [map x, map y, map z], should have type [([a] - [b])]
because
map x :: [a] - [b]where x :: a - b



On 3/6/09, R J rj248...@hotmail.com wrote:


 Given the following (usual) definition of map:

 map:: (a - b) - [a] - [b]
 map f []   =  []
 map f (x : xs) =  f x : map f xs

 What's the type of map map?

 GHCi's :t command reveals:

 *Main :t map map
 map map :: [a - b] - [[a] - [b]]

 I'd be grateful if anyone could provide a systematic type calculation so
 that I can reason through more complicated examples myself.

 Thanks.

 _
 Windows Liveā„¢: Life without walls.
 http://windowslive.com/explore?ocid=TXT_TAGLM_WL_allup_1a_explore_032009
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] C-like Haskell

2009-01-28 Thread sam lee
Did you print it? I'm using same code with ghc --make -O2  and it
takes forever to finish.


On Wed, Jan 28, 2009 at 8:06 PM, Duncan Coutts
duncan.cou...@worc.ox.ac.uk wrote:
 On Wed, 2009-01-28 at 16:42 -0800, drblanco wrote:

 I do already have the number I wanted, but was wondering how this could be
 made faster, or even why it's so slow.  This is all on GHC 6.8.3 under OS X
 Intel, using ghc -O2.

 I'm not exactly sure what's different, but for me it works pretty well.
 I put back in the Int64 type signature.

 For comparison, the C code below runs in 1 second.

 You've got a faster machine than me :-)

 I compiled both the Haskell and C versions to standalone executables
 with ghc/gcc -O2 and ran them with time.

 C version:
 $ time ./circ
 3141592649589764829

 real0m2.430s
 user0m2.428s
 sys 0m0.000s

 Haskell version:
 time ./circ2
 3141592653589764829

 real0m2.753s
 user0m2.756s
 sys 0m0.000s


 Not too bad I'd say! :-)

 I was using ghc-6.10 for this test. It would appear that ghc-6.8 is a
 bit slower, I get:

 3141592653589764829

 real0m5.767s
 user0m5.768s
 sys 0m0.000s

 Now the other difference is that I'm using a 64bit machine so perhaps
 ghc just produces terrible code for Int64 on 32bit machines.

 Duncan

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Simplification of this function?

2009-01-16 Thread sam lee
saml @pl myFunc f (a,b) (c,d) = (f a c, f b d)
lambdabot myFunc = (`ap` snd) . (. fst) . flip flip snd . ((flip . (ap .))
.) . flip flip fst . ((flip . ((.) .)) .) . (flip = (((.) . flip . (((.) .
(,)) .)) .))

why not use zipWith?

[a,b] `g` [c,d]  where g = zipWith f

2009/1/16 Andrew Wagner wagner.and...@gmail.com

 I've been playing around with this, but haven't been able to come up with
 anything.
 myFunc f (a,b) (c,d) = (f a c, f b d)

 It feels as if there should be a nice simple version of this using some
 combination of {un,}curry, on, , ***, or something else.

 Any thoughts?

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Simplification of this function?

2009-01-16 Thread sam lee
Sorry it won't work because list should be homogeneous.
I think your myFunc is standard.
This guy names it thread:
http://alaska-kamtchatka.blogspot.com/2009/01/essence-of-concatenative-languages.html

On Fri, Jan 16, 2009 at 10:32 AM, sam lee skyn...@gmail.com wrote:

 saml @pl myFunc f (a,b) (c,d) = (f a c, f b d)
 lambdabot myFunc = (`ap` snd) . (. fst) . flip flip snd . ((flip . (ap
 .)) .) . flip flip fst . ((flip . ((.) .)) .) . (flip = (((.) . flip .
 (((.) . (,)) .)) .))

 why not use zipWith?

 [a,b] `g` [c,d]  where g = zipWith f

 2009/1/16 Andrew Wagner wagner.and...@gmail.com

 I've been playing around with this, but haven't been able to come up with
 anything.
 myFunc f (a,b) (c,d) = (f a c, f b d)

 It feels as if there should be a nice simple version of this using some
 combination of {un,}curry, on, , ***, or something else.

 Any thoughts?

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Time for a new logo?

2008-12-14 Thread sam lee
http://i35.tinypic.com/mjon83.png
used this: http://www.simwebsol.com/ImageTool/Default.aspx

2008/12/14 George Pollard por...@porg.es:
 ?


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Parsec and (then?) type-check

2008-12-12 Thread sam lee
I use my type checking monad, which is separate from Parsec's monad.
So, I can't think of a way to type check during parsing in Parsec's monad.

Anyways, this is what I did:

data Expr = ... | At SourcePos Expr

SourcePos is from Parsec.

Basically, my parse actions will return (At pos e).

And I pass At pos e to typeCheck action.

typeCheck (At pos e) = do
put pos
-- typeCheck is in State monad.
-- in case of error, I'll pos - get and report source position.
typeCheck e

typeCheck (Variable a) = do
   ...

check out this:
http://www.lipl.googlepages.com/index.html#source


On Fri, Dec 12, 2008 at 5:06 PM, Greg Fitzgerald gari...@gmail.com wrote:
 Parser gurus,

 When you write a parser with a library like Parsec, do you typically
 type-check while parsing, or afterward in a separate pass?  The latter
 is more modular, but it means labeling every element in the AST with
 the parser position so that you can give good error messages.  Do you
 find the added modularity worth the hassle or just pack type-checking
 into the parser pass?

 Thanks,
 Greg
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] SDL program under MinGW

2008-12-07 Thread sam lee
Yes, with some modifications.

I used SDL-1.2.13, not SDL-1.2.12.

Also, my Include-Dirs has include\SDL, not include:
C:\Users\client\code\SDL-1.2.13\include\SDL

If I don't put include\SDL, I get errors like
Graphics\UI\SDL\General.hsc:1:17:  SDL.h: No such file or directory

And I used the following command to configure:
C:\Users\client\Downloads\SDL-0.5.4runghc Setup.lhs configure
--prefix=C:\Users\client\haskell --datadir=$prefix\share --user

it builds and installs fine. I only get linker error (undefined
reference to SDL_main) when I compile a program that uses SDL binding.


On Sun, Dec 7, 2008 at 4:23 AM, Bit Connor [EMAIL PROTECTED] wrote:
 Did you follow the instructions described in the WIN32 file?

 On Sat, Dec 6, 2008 at 2:05 PM, sam lee [EMAIL PROTECTED] wrote:
 Hi.

 I am using Windows Vista.
 I installed the following:

 - ghc 6.8.3 (using official Windows binary installer)
 - MinGW (from http://nuwen.net/mingw.html)
 - SDL binding (from
 http://hackage.haskell.org/cgi-bin/hackage-scripts/package/SDL)

 I am trying to compile the following program:

module Main where

import qualified Graphics.UI.SDL as SDL

main = do
SDL.init [SDL.InitEverything]
SDL.quit

 I get the following error:

 C:\Users\client\code\sandboxghc --make Example.hs
 Linking Example.exe ...
 C:\MinGW\lib/libSDLmain.a(SDL_win32_main.o)(.text+0x409):SDL_win32_main.c:
 undefined reference to `SDL_main'

 According to SDL faq
 (http://www.libsdl.org/faq.php?action=listentriescategory=4#48)  I
 should use int main(int argc, char *argv[]), not int main() nor
 WinMain().

 Can anyone compile the above snippet on windows machine?
 I am using SDL library that is shipped with MinGW distribution that I'm 
 using.
 I also recompiled SDL haskell binding with official SDL windows binary
 (http://www.libsdl.org/release/SDL-devel-1.2.13-mingw32.tar.gz). But I
 get the same undefined reference to SDL_main linker error.

 Do I need to have specific main IO action so that SDL would work in Haskell?

 Thank you.
 Sam.
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] SDL program under MinGW

2008-12-06 Thread sam lee
Hi.

I am using Windows Vista.
I installed the following:

- ghc 6.8.3 (using official Windows binary installer)
- MinGW (from http://nuwen.net/mingw.html)
- SDL binding (from
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/SDL)

I am trying to compile the following program:

module Main where

import qualified Graphics.UI.SDL as SDL

main = do
SDL.init [SDL.InitEverything]
SDL.quit

I get the following error:

C:\Users\client\code\sandboxghc --make Example.hs
Linking Example.exe ...
C:\MinGW\lib/libSDLmain.a(SDL_win32_main.o)(.text+0x409):SDL_win32_main.c:
undefined reference to `SDL_main'

According to SDL faq
(http://www.libsdl.org/faq.php?action=listentriescategory=4#48)  I
should use int main(int argc, char *argv[]), not int main() nor
WinMain().

Can anyone compile the above snippet on windows machine?
I am using SDL library that is shipped with MinGW distribution that I'm using.
I also recompiled SDL haskell binding with official SDL windows binary
(http://www.libsdl.org/release/SDL-devel-1.2.13-mingw32.tar.gz). But I
get the same undefined reference to SDL_main linker error.

Do I need to have specific main IO action so that SDL would work in Haskell?

Thank you.
Sam.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] SDL program under MinGW

2008-12-06 Thread sam lee
I can compile 
http://darcs.haskell.org/~lemmih/hsSDL/hssdl/Examples/MacOSX/Test.hs

But I can't find the small Cabal file and c_main.c .
Does anyone have those files? I want to try if Test.hs links well on
windows platform.

 I think the binding needs to not include SDLMain and instead do the
initialization itself; see the rest of that FAQ question.

I can't grep for SDLMain in the binding. How should I modify the
binding code to use WinMain + proper initialization?

Thanks.
Sam.

On Sat, Dec 6, 2008 at 5:19 PM, Ryan Ingram [EMAIL PROTECTED] wrote:
 From the link you provided
 (http://www.libsdl.org/faq.php?action=listentriescategory=4#48)

 Q:I get Undefined reference to 'SDL_main' ...
 A:Make sure that you are declaring main() as:

 #include SDL.h

 int main(int argc, char *argv[])

 In particular notice that they want you to include SDL.h.  I suspect
 they are doing something scary like

 #define main(ac, av) SDL_Main(ac, av, maybe some other args)

 The problem is that the library itself is defining WinMain, so you've
 got a fight between the Haskell compiler (which wants to define the
 entry point for your program to start up the runtime), and the SDL
 library (which is trying to be helpful and failing).

 I think the binding needs to not include SDLMain and instead do the
 initialization itself; see the rest of that FAQ question.

 In fact, the MACOSX readme file in the Haskell SDL binding talks
 about exactly this problem.

  -- ryan

 On Sat, Dec 6, 2008 at 2:05 PM, sam lee [EMAIL PROTECTED] wrote:
 Hi.

 I am using Windows Vista.
 I installed the following:

 - ghc 6.8.3 (using official Windows binary installer)
 - MinGW (from http://nuwen.net/mingw.html)
 - SDL binding (from
 http://hackage.haskell.org/cgi-bin/hackage-scripts/package/SDL)

 I am trying to compile the following program:

module Main where

import qualified Graphics.UI.SDL as SDL

main = do
SDL.init [SDL.InitEverything]
SDL.quit

 I get the following error:

 C:\Users\client\code\sandboxghc --make Example.hs
 Linking Example.exe ...
 C:\MinGW\lib/libSDLmain.a(SDL_win32_main.o)(.text+0x409):SDL_win32_main.c:
 undefined reference to `SDL_main'

 According to SDL faq
 (http://www.libsdl.org/faq.php?action=listentriescategory=4#48)  I
 should use int main(int argc, char *argv[]), not int main() nor
 WinMain().

 Can anyone compile the above snippet on windows machine?
 I am using SDL library that is shipped with MinGW distribution that I'm 
 using.
 I also recompiled SDL haskell binding with official SDL windows binary
 (http://www.libsdl.org/release/SDL-devel-1.2.13-mingw32.tar.gz). But I
 get the same undefined reference to SDL_main linker error.

 Do I need to have specific main IO action so that SDL would work in Haskell?

 Thank you.
 Sam.
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] bug in time libs?

2008-12-01 Thread sam lee
I can't find document for System.Time .

But I can import System.Time . It's weird...
I can't find document for TimeDiff and related functions. I guess this
is all deprecated.

A related bug report: http://hackage.haskell.org/trac/ghc/ticket/2519

I would use Data.Time.Clock

Prelude :m + Data.Time.Clock
Prelude Data.Time.Clock getCurrentTime
Prelude Data.Time.Clock t0 - getCurrentTime
Prelude Data.Time.Clock t1 - getCurrentTime
Prelude Data.Time.Clock t0
Prelude Data.Time.Clock diffUTCTime t0 t1
-2.912s
Prelude Data.Time.Clock diffUTCTime t1 t0
2.912s
Prelude Data.Time.Clock :t diffUTCTime t1 t0
diffUTCTime t1 t0 :: NominalDiffTime

On Mon, Dec 1, 2008 at 10:05 PM, Jason Dusek [EMAIL PROTECTED] wrote:
  I'm on 6.10.1, using the libs provided with the binary of GHC
  for Mac OS X. I was fooling around with the time package in
  GHCi and something seems to be off. I have included a
  transcript.

  If I take a time diff, then the seconds are positive when the
  picos are negative and vice versa.

 --
 _jsn


  |...transcript...|


  Prelude System.Time :m + System.Time
  Prelude System.Time t0 - System.Time.getClockTime
  Prelude System.Time t1 - System.Time.getClockTime
  Prelude System.Time diffClockTimes t0 t1
  TimeDiff { tdYear = 0
   , tdMonth = 0
   , tdDay = 0
   , tdHour = 0
   , tdMin = 0
   , tdSec = -7
   , tdPicosec = 23884600
   }
  Prelude System.Time diffClockTimes t1 t0
  TimeDiff { tdYear = 0
   , tdMonth = 0
   , tdDay = 0
   , tdHour = 0
   , tdMin = 0
   , tdSec = 7
   , tdPicosec = -23884600
   }
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] '#' in literate haskell

2008-11-29 Thread sam lee
# is significant because it can be sh-bang line or pre-processor.

The only way I can think of is:

alias lhspp=sed 's/^#//'
ghc --make -F -pgmF lhspp  File.lhs

On Sat, Nov 29, 2008 at 10:07 PM, John MacFarlane [EMAIL PROTECTED] wrote:
 Can anyone explain why ghc does not treat the following
 as a valid literate haskell program?

 - test.lhs 
 # This is a test

 foo = reverse . words

 

 When I try to load this in ghci (or compile it using ghc),
 I get:

 test.lhs:1:2: lexical error at character 'T'

 It seems that the problem is the '#' character in the first
 column.  Replacing it with something else, or moving it to
 the right one space, solves the problem.

 The following literate haskell program, from
 http://notvincenz.blogspot.com/2008/01/literate-haskell-and-c.html,
 also fails to load for me, for the same reason (the leading
 '#' in line 8).

  literate-haskell-and-c.lhs ---
 /* c and lhs file


 module Foo where
 main = print Haskell


 */
 #include

 int main() {
  printf(C\n);
  return 0;
 }
 

 I've reproduced this with ghc 6.10.1 and ghc 6.8.3 (linux binaries
 from haskell.org) and with ghc 6.8.2 (Ubuntu intrepid).
 Interestingly, hugs (September 2006 version) has no trouble with
 test.lhs. I haven't tried ghc 6.6.

 I care about this because I'd like to use markdown conventions
 to format the comment parts of literate haskell programs.  Markdown
 supports atx-style headers, which begin with strings of '#'
 characters starting in the first column. I know that some people use
 markdown with literate haskell, so there must be something basic here
 that I'm missing!

 John

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] SOEGraphics in GHC?

2008-11-24 Thread sam lee
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HGL
or
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/soegtk


2008/11/24 Dmitri O.Kondratiev [EMAIL PROTECTED]:
 Please help, to locate in GHC distribution SOEGraphics library from
 Paul Hudak, book The Haskell School of Expression
 (http://www.haskell.org/soe/ )

 Thanks!

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What *not* to use Haskell for

2008-11-11 Thread sam lee
I haven't found multitrack audio recording applications written in
Haskell. These are usually written in C++ using Jack audio or ASIO.
This probably means that it is not a good idea to write real time
audio applications in Haskell at the moment.
So, probably avoid writing applications that use a high frequency
timer and IO that should be synchronous to the timer in Haskell.

On Tue, Nov 11, 2008 at 9:02 PM, wren ng thornton [EMAIL PROTECTED] wrote:
 Dave Tapley wrote:

 Hi everyone

 So I should clarify I'm not a troll and do see the Haskell light. But
 one thing I can never answer when preaching to others is what does
 Haskell not do well?

 Usually I'll avoid then question and explain that it is a 'complete'
 language and we do have more than enough libraries to make it useful and
 productive. But I'd be keen to know if people have any anecdotes,
 ideally ones which can subsequently be twisted into an argument for
 Haskell ;)

 With the appropriate caveats about particular subdomains (see final
 paragraph), I wouldn't use Haskell for scripting. That is, (1) for
 Bash-style programming where 95% of the code is just invoking *nix jobs, or
 (2) for very simple yet regex-heavy scripts where Perl/Awk/Sed is often
 used.

 Re #1: Honestly, I don't see anything other than a dedicated competitor
 being able to unseat Bourne/Bash at this task. Certainly a competitor would
 have much room for improvement-- what with being able to replace
 string-rewriting semantics with term-rewriting semantics, vastly improving
 type safety and catching innumerable bugs. However, with unsavory frequency,
 it is exactly those type-unsafe substitutions which can make shell scripting
 cleaner and more direct than a type-safe alternative. Having type safety as
 well as this sort of non-compositional structure would take a good deal of
 work to get right.

 Re #2: People often complain about spooky Perl that uses things like
 implicit $_ or other hidden variables. While these constructs can make any
 sizable project unmaintainable, for the quick and dirty jobs they're just
 what's needed to get things done with clarity. While ByteString code using
 regexes is just as fast in Haskell, it's often more than twice as long as
 the Perl, Sed, or Awk equivalents because many of the basic control
 structures (like Perl's -n, -p, -l,... flags) aren't already provided.


 That said, this isn't necessarily a bad thing for Haskell. Real
 programming languages often don't do so well in these areas (Perl being the
 exception), and they don't feel too bad about it. Both varieties of shell
 scripting are very much of the DSL nature; for programs with a significant
 amount of actual logic instead of mere plumbing or regexing, Haskell can
 certainly outshine these competitors. On the one hand, C and friends fight
 dirty and much work has been done so Haskell can join in on the bit-bashing
 glory. However, shell scripting is a whole different kind of imperative muck
 and very little work (that I've seen) has tried to get Haskell to jump down
 in the sewers with them.

 --
 Live well,
 ~wren
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] An interesting curiosity

2008-09-18 Thread sam lee
I think it's because + is left associative. and pattern matching is
top to bottom.
1 + 1 + 1 = (1 + 1) + 1 = found match: 1 + 1 = 3 = 3 + 1 = found
match: 3 + 1 = 3 = 3

On Thu, Sep 18, 2008 at 11:01 PM, Steven Shaw [EMAIL PROTECTED] wrote:
 2008/9/19 Evan Laforge [EMAIL PROTECTED]
 let {1 + 1 = 3; 3 + 1 = 3} in 1 + 1 + 1

 Which gives 3.

 Perhaps even more confusing is:

 let {1 + 1 = 3; 3 + 1 = 3} in 3 + 1 + 1

 which gives 3
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How to do this in FP way?

2008-06-15 Thread sam lee
I can think of 2 ways.

 module Main where

 import Control.Monad.State

First, normal way:

 diff (now, old) = (now - old, now)

diff takes now and old and returns result (now - old) and modified old (now).
For example,
diff (diff (1,0))
== diff (1 - 0, 1)
== diff (1, 1)
== (1 - 1, 1)
== (0, 1)

I think people use the word threaded to describe what diff is doing:
the variable old is threaded through many calls to diff.

 testDiff = diff . diff . diff . diff . diff . diff $ (2, 1)

testDiff returns (2,1)

Second way is using monads:

 diff' now = do
 old - get
 put now
 return (now - old)


diff' uses State monad.
If you're not familiar with monads, State monad does similar to what
diff function does (it threads the variable old).
But, being a monadic action, diff' looks like imperative version
syntactically. It gives illusion of having global variable (old).


 testDiff' = do
 result - diff' 2
 result - diff' result
 result - diff' result
 result - diff' result
 result - diff' result
 result - diff' result
 return result

 runTestDiff' = runState testDiff' 1

runTestDiff' also returns (2,1)

2008/6/15 Magicloud Magiclouds [EMAIL PROTECTED]:
 Hello,
 I am getting familiar with FP now, and I have a program design kind of
 question.
 Say I have something like this in C:
 static int old;
 int diff (int now) { /* this would be called once a second */
   int ret = now - old;
   old = now;
   return ret;
 }
 Because there is no variable in Haskell. So how to do this in a FP
 way?

 Thanks.

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] M1 + M2 = M3 where both computations in M1 and M2 can be used?

2008-05-12 Thread sam lee
Hi.

I want to compose two monads to build another monad where
computations of the two monads can be used inside.

I have:

- MonadTypeInfer : interface (class) for TypeInfer monad
- TypeInfer : a monad that has Map String Type (association of names and types)
- TypeInferT : transformer of above monad
- MonadEval : interface (class) for Eval monad
- Eval : a monad that has Map String Expr (association of names and
code/function body)
- EvalT : transformer of Eval
- tInfer :: Expr - TypeInfer Type -- given expr, returns type of it
in TypeInfer monad
- eval :: Expr - Eval Expr -- given expr, returns normalized expr in Eval monad

Problem: in repl, when user defines a function, it should type check
and register type of the function to TypeInfer monad's Map String
Type.
Also, it should store the expression of the function in Eval monad.

I build REPL monad using TypeInferT and EvalT.

 newtype REPL a = REPL { runREPL :: TypeInferT (EvalT IO) a }
   deriving(Monad, Functor, MonadIO, MonadTypeInfer, MonadEval)
 repl :: REPL ()
 repl = do
   input - prompt  
   case parse input of
 Left err - -- handle error
 Right expr - do
   t - tInfer expr -- BAD!! tInfer :: TypeInfer Type
   println (show t)
   result - eval expr -- BAD!! eval :: Eval Expr
   println (show result)
   repl

Should I make tInfer :: REPL Type, eval :: REPL Expr?
Is there a way to build a monad where you could use sub-monads'
(monads used to build current monad) computations?
I prefer keeping tInfer :: TypeInfer Type, eval :: Eval Expr
because tInfer never uses actions in Eval monad and vice versa.
It seems like what I am asking is to break the type system.
Maybe I should just make them run in REPL monad.

Thank you.
Sam.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] M1 + M2 = M3 where both computations in M1 and M2 can be used?

2008-05-12 Thread sam lee
  tInfer :: MonadTypeInfer m = Expr - m Type
  eval   :: MonadEval  m = Expr - m Expr

That solves!
I should've left out type annotation.


On Mon, May 12, 2008 at 10:38 AM, Twan van Laarhoven [EMAIL PROTECTED] wrote:
 sam lee wrote:


 
  Hi.
 
  I want to compose two monads to build another monad where
  computations of the two monads can be used inside.
 
  I have:
 
  - MonadTypeInfer : interface (class) for TypeInfer monad
  - TypeInfer : a monad that has Map String Type (association of names and
 types)
  - TypeInferT : transformer of above monad
  - MonadEval : interface (class) for Eval monad
  - Eval : a monad that has Map String Expr (association of names and
  code/function body)
  - EvalT : transformer of Eval
  - tInfer :: Expr - TypeInfer Type -- given expr, returns type of it
  in TypeInfer monad
  - eval :: Expr - Eval Expr -- given expr, returns normalized expr in Eval
 monad
 
 
  Is there a way to build a monad where you could use sub-monads'
  (monads used to build current monad) computations?
 

  A solution to this problem is to use type classes, and in particular
 MonadTrans. You can then give an instance of MonadTypeInfer for EvalT m
 where m is an instance of MonadTypeInfer, and similarly an instance
 MonadEval for TypeInferT m. How this is implemented depends on the Monads in
 question, but if you use the monad transformer library with newtype deriving
 you can just add deriving MonadTrans.

   class Monad m = MonadTypeInfer m where
   -- functions --
   tiStuff :: X - m Whatever

   class Monad m = MonadEval m where
   -- functions --

   instance Monad m = MonadTypeInfer (TypeInferT m) where
   -- implementation --
   tiStuff = ...

   instance Monad m = MonadEval (EvalT m) where
   -- implementation --

   instance MonadEval m = MonadTypeInfer (EvalT m) where
   -- lift the functions from TypeInfer through the EvalT type,
   -- MonadTrans from the mtl might help here
   tiStuff x = lift (tiStuff x)

   tInfer :: MonadTypeInfer m = Expr - m Type
   eval   :: MonadEval  m = Expr - m Expr


  Twan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe