Re: [Haskell-cafe] extreme newbie: hugs prompt vs load module

2006-08-23 Thread Shao Chih Kuo

You can always load things inside ghci with:

:m

i.e.

Prelude> :m List
Prelude List> :m Control.Concurrent
Prelude Control.Concurrent> :m Control.Concurrent List
Prelude List Control.Concurrent>

George Young wrote:

[linux, ghci 6.4.3.20060820, hugs May 2006]

I have just started learning Haskell.  I have hugs and ghci under
linux, and I'm going through the Gentle Introduction to
Haskell, so far through section 4,
"case expressions and pattern matching".  I'm a python programmer, with
background in maclisp, scheme, T, C, C++, and a little J.  


I'm confused about what sort of things I can type at the interpreter
prompt, and what things have to be loaded as a module.  I keep trying
to treat the prompt like a lisp or python REPL, which is obviously
wrong.  Can someone set me straight?

Is there another tutorial that might be more appropriate for me?

I am finding haskell quite appealing.  I hope to start writing real (if
small) applications to do some data analysis from our Postgres DB.  Any
hints?

--George Young
  


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


Re: [Haskell-cafe] Useful: putCharLn {inspire by the Int->[Char] thread

2006-08-21 Thread Shao Chih Kuo

This might be easier:

Prelude> putStrLn $ return $ head "this and that"
t
Prelude>

Gene A wrote:

The thread on the use of "show" and print to display an Int value,
brought up a problem I had early on... the one of cleanly displaying a
Char value, on a line all by itself.. My first attempts:

This was just plain hard to read: with the character t being where it 
was:


Prelude> putChar $ head "this and that"
tPrelude>
---
So I tried this and of course ... type mismatch:

Prelude> putStrLn $ head "this and that"
:1:16:
   Couldn't match `String' against `Char'
 Expected type: [String]
 Inferred type: [Char]
   In the first argument of `head', namely `"this and that"'
   In the second argument of `($)', namely `head "this and that"'
--
So I did this... to manually turn it to a string and it does work, but
a little cumbersome to work into other function calls:
Prelude> putStrLn $ (head "this and that"):[]
t
-
so the definition of putCharLn came to life {may be in some library 
already

and I just haven't found it yet.. but it is in my toolbox now}:

Prelude> let putCharLn c = putStrLn (c:[])
Prelude>
and an application of it:

Prelude> putCharLn $ head "this and that"
t
---
now I also have the char to string conversion alone:

c2Str c = c:[]

Prelude> let c2Str c = c:[]
Prelude> c2Str 'A'
"A"
--
Now this is almost too trivial a thing to mention these gizmos...
what with all the monadic constructions that greater minds  toss
about on this list.. and I am trying to get understanding of that
still, but 
sometimes we just accept the unacceptable little irritants
rather than just code a solution, no matter how simple it is.
There are probably troves of simple workarounds out there
that seem too trivial to mention but hey, share 'em...
might hit a guy like me that says. "now why didn't I think to do that?"

happy days,
gene
___
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] REALLY simple STRef examples

2006-07-22 Thread Shao Chih Kuo
Yes, largely the choice to define foreach was made to try and make it 
look more imperative, I showed it to an imperative programmer to try and 
convince him that you could program in an imperative way in Haskell if 
you really wanted to, that and I thought it'd an imperative style would 
make an interesting addition to the evolution of a Haskell programmer.


Bulat Ziganshin wrote:

Hello Bryan,

Saturday, July 22, 2006, 4:40:58 AM, you wrote:
  

Forgive me for not understanding, but I was hoping you would explain a
choice you made in your code. Why did you define foreach and then use



  

foreach [1..n] (\x -> modifySTRef r (*x))
  


  

Instead of simply using



  

mapM_ (\x -> modifySTRef r (*x)) [1..n]
  


because it looks just like for/foreach loops in imperative languages.
look at this:

import Control.Monad
import Data.IORef

infixl 0 =:, +=, -=, .=, <<=
ref = newIORef
val = readIORef
a=:b = writeIORef a b
a+=b = modifyIORef a (\a-> a+b)
a-=b = modifyIORef a (\a-> a-b)
a.=b = ((a=:).b) =<< val a
for :: [a] -> (a -> IO b) -> IO ()
for = flip mapM_

newList = ref []
list <<= x   =  list =:: (++[x])
push list x  =  list =:: (x:)
pop list =  do x:xs<-val list; list=:xs; return x

main = do
  sum <- ref 0
  lasti <- ref undefined
  for [1..5] $ \i -> do
sum += i
lasti =: i
  sum .= (\sum-> 2*sum+1)
  print =<< val sum
  print =<< val lasti

  xs <- newList
  for [1..3] (push xs)
  xs <<= 10
  xs <<= 20
  print =<< val xs



  


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