add something to a list

2002-02-17 Thread christophe certain

Hi,

I'm a poor lonesome newbie in Haskell world, and I would like to add a string
typed on the prompt to a list of strings which is already defined.

It would look like something like :

type Path = [String]

currentPath::Path
currentPath = []

getpiece ::IO String
getpiece  =  do c -getLine
return c

putpiece:: String-Path
putpiece a = a:currentPath

and then I could combine the two functions, but obviously it doesn't work.
I dare understand that it's impossible isn't it ?

Maybe the only way is to create a new [String] each time I want to add a new 
string ? No ?

Christophe Certain
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: add something to a list

2002-02-17 Thread Adrian Hey

On Sunday 17 February 2002 08:20, christophe certain wrote:
 Hi,

 I'm a poor lonesome newbie in Haskell world, and I would like to add a
 string typed on the prompt to a list of strings which is already defined.

 It would look like something like :

 type Path = [String]

 currentPath::Path
 currentPath = []

 getpiece ::IO String
 getpiece  =  do c -getLine
   return c

 putpiece:: String-Path
 putpiece a = a:currentPath

 and then I could combine the two functions, but obviously it doesn't work.
 I dare understand that it's impossible isn't it ?

 Maybe the only way is to create a new [String] each time I want to add a
 new string ? No ?

 Christophe Certain

You seem to expect currentPath to be updated by putpiece? This won't happen
in Haskell. Once you've declared
   currentPath=[]
it will always be []. 

Values never change. If you want the functional equivalent of accumulator 
variables they have to be an argument of a recursive function. So try this..

getPath :: Path - IO Path
getPath currentPath = do
piece - getLine
if piece ==   then return currentPath
else getPath (piece:currentPath)

initialCurrentPath::Path
initialCurrentPath = []

main :: IO ()
main = do
path - getPath initialCurrentPath
putStrLn (show path)

Regards
--
Adrian Hey



 
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: add something to a list

2002-02-17 Thread Cagdas Ozgenc



 You seem to expect currentPath to be updated by putpiece? This won't
happen
 in Haskell. Once you've declared
currentPath=[]
 it will always be [].

 Values never change. If you want the functional equivalent of accumulator
 variables they have to be an argument of a recursive function. So try
this..

 getPath :: Path - IO Path
 getPath currentPath = do
 piece - getLine
 if piece ==  then return currentPath
 else getPath (piece:currentPath)

 initialCurrentPath::Path
 initialCurrentPath = []

 main :: IO ()
 main = do
 path - getPath initialCurrentPath
 putStrLn (show path)

 Regards
 --
 Adrian Hey


Hi Adrian,

How can I add a function that sorts this list that I read from the user and
accumulate using the function that you described? I am not asking for a sort
algorithm of course, I am just wondering how to feed the IO Path as an input
to a sort function? Is it suppose to look like this:

sort :: IO Path - IO Path

or

sort :: IO Path - Path

How do you iterate over IO Path?

Thanks for taking time.

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: add something to a list

2002-02-17 Thread Jay Cox

On Sun, 17 Feb 2002, Cagdas Ozgenc wrote:

 Hi Adrian,

 How can I add a function that sorts this list that I read from the user and
 accumulate using the function that you described? I am not asking for a sort
 algorithm of course, I am just wondering how to feed the IO Path as an input
 to a sort function? Is it suppose to look like this:

 sort :: IO Path - IO Path


It could.  (to make it simpler you may need a sort' :: Path - Path
function though, as in.

 sort getpath = do x - getpath  -- x has type Path
   return (sort' x)
  where sort' =   -- sort' is typed as above and produces
  -- a sorted list list of
  -- type Path


 or

 sort :: IO Path - Path

The point of the IO monad is to thread the state of the World (which
your program modifies)  explicitly through your program.  The pure
functions are the plumbing.  A function of type IO Path - Path cannot
modify the world state since the world state is not the result of function
application *.




 How do you iterate over IO Path?

Um, I cant think of another good introductory way than that sort :: IO
Path - IO Path I mentioned above.


Jay Cox



* Minus the obvious facts that haskell implementions obviously do:
   1. modify the world state as executing functions create new
  datastructures that need to be allocated, may cause garbage collection,
  maybe cause new heap allocation (do they?) or otherwise
  cause program termination (run out of stack/heap/ ... ), etc.
   2. unless it uses sort unsafeIO and/or other hacks.  unsafeIO
  generally should not be used.


PS:  Anybody got any other suggestions for IO monad entry-level docs?
 I suppose Haskell Wiki on haskell.org might be good place to allude
 to.



___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: add something to a list

2002-02-17 Thread Mark Carroll

On Sun, 17 Feb 2002, Jay Cox wrote:
(snip)
 PS:  Anybody got any other suggestions for IO monad entry-level docs?
(snip)

Simon's Tackling the Awkward Squad paper was a revelation for me.

-- Mark

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe