Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Indenting (Chadda? Fouch?)
   2. Re:  Indenting (Brent Yorgey)
   3. Re:  Indenting (Brent Yorgey)
   4. Re:  Indenting (Daniel Fischer)
   5.  Keyboard input (legajid)
   6. Re:  Keyboard input (Daniel Fischer)
   7.  freelancing jobs in haskell (Vinod Parthasarathy)
   8. Re:  Keyboard input (legajid)


----------------------------------------------------------------------

Message: 1
Date: Sat, 5 Dec 2009 09:34:56 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Indenting
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Fri, Dec 4, 2009 at 9:26 PM, John Moore <[email protected]> wrote:
> Hi,
>      I'm a bit stuck. I want to indent my answer by using a count function
> to indent my answer. Say for example I want to show the levels in a simple
> arithmethic expression.
> like 3*(4+5)
> I want to be able to print out :
>  4+5 = 9
>     3*9 = 27 (notice the indentation as I go through an expression.
>
> I tried this but it a mess, I tried to use prettyprint but this seemed very
> complicated.

I'm not sure I really understand what your problem is...
If you just want to indent a String, it is easy to do with :

> indent :: Int -> String -> String
> indent n s = replicate n ' ' ++ s

If you want to use this with your evaluation of a small arithmetic
language, I would suggest making your evaluator returns a list of its
reductions (with the Writer Monad maybe) and use :

> zipWith indent [2,4..] reductionsList

-- 
Jedaï


------------------------------

Message: 2
Date: Sat, 5 Dec 2009 11:35:03 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Indenting
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Dec 04, 2009 at 08:26:52PM +0000, John Moore wrote:
> 
> type Indent = (Int)
> spacing :: Int -> Indent
> spacing n = Nothing
> spacing n (x:xs) = if (n==1)
>                     then (Just n)
>                    else spacing n xs
>                     where (n > 1)

Hmm, you seem to be a bit confused.  What exactly is spacing supposed
to do?  What is its type?  Your type signature says that it has type
Int -> Int, but the second equation spacing n (x:xs) = ...  suggests
that it takes TWO arguments, with the second being a list of some
sort; also, it seems to be returning things of type Maybe Int
(Nothing, Just n).  So, what is it supposed to do?  Is it computing an
*amount* of indentation? Is it supposed to take a String and put
spaces on the front? or...?

-Brent


------------------------------

Message: 3
Date: Sat, 5 Dec 2009 15:09:01 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Indenting
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sat, Dec 05, 2009 at 05:58:25PM +0000, John Moore wrote:
> Hi Brent,
> Sorry about the confusion below is the whole program, which may help or not?
> What i want to do is when the program prints out the answers , I would like
> it to be able to automatically by use of a function indent the answers
> depending how far down the answer goes.

OK, I think I understand what you want to do, although I'm not exactly
clear on how you want the indentation to work.

Anyway, I've interspersed my comments in the code below.
 
> import Maybe

Just FYI, "Maybe" is the old Haskell-98 name for the module, but
nowadays the standard name is "Data.Maybe".

> data Expression = Val Float
>                 | Add Expression Expression
>                 | Subtract Expression Expression
>                 | Multiply Expression Expression
>                 | Divide Expression Expression
>   | Let String Expression Expression
>   | Var String
>          deriving Show
> type Dict =[(String,Expression)]
> emptyDict :: Dict
> emptyDict = []
> addEntry :: String->Expression ->Dict -> Dict
> addEntry n e d = (n,e): d
> lookupEntry :: String -> Dict -> Maybe Expression
> lookupEntry n [] = Nothing
> lookupEntry n (x:xs) = if (n == k)
>    then (Just v)
>                 else lookupEntry n xs
>    where (k,v) = x

I would write lookupEntry as follows:

  lookupEntry _ [] = Nothing
  lookupEntry n ((k,v):xs) | n == k    = Just v
                           | otherwise = lookupEntry n xs

Note that you can do a nested pattern match ((k,v):xs), and the use of
guards instead of if...then...else.

But actually, lookupEntry is already in the standard Prelude, it is
called 'lookup'!  So no need to reimplement it yourself.

> evalIO :: Dict -> Expression -> IO Float

This function is rather poor Haskell style, because it mixes up two
separate things: evaluating the expression, and printing the
result. Instead, I would do something like this:

  eval :: Dict -> Expression -> ([String], Double)

where the output is the result paired with a "trace".  (Note I have
used Double, a double-precision floating point number, instead of
Float, which is single-precision; there's usually very little reason
to use Float instead of Double.)  Given eval, you can recover evalIO as follows:

  evalIO :: Dict -> Expression -> IO Double
  evalIO d e = do
    let (trace, result) = eval d e
    mapM_ putStrLn trace
    return result

But this is a lot nicer because it cleanly separates the evaluation
from the IO, and you can now do anything you like with the trace ---
print it to a file, process it further, etc; you are not tied down to
printing it on the screen.

Now, if you just want the indentation to increase at each step, like so

xxxxx
 xxxxx
  xxxxx
   xxxxx
    xxxxx
     ...

then you can also separate this out: just return a trace with no
indentation, and then apply a function to the trace to indent
successive lines, something like

  zipWith (++) (map (flip replicate ' ') [0..]) trace

If, on the other hand, you want the indentation to correspond to how
"deep" within the expression the evaluation is taking place, then you
can recursively pass along an extra parameter to your evaluation
function, like so:

  eval :: Dict -> Expression -> ([String], Double)
  eval d e = evalIndented 0 d e where
    evalIndented i d (Val x) = ([], x)
    evalIndented i d (Add x y) = 
      let (tx, vx) = evalIndented (i+1) d x
          (ty, vy) = evalIndented (i+1) d y
      in  (replicate i ' ' ++ "Add " ++ show vx ++ " and " ++ show vy, vx + vy)

...and so on.

I hope this is helpful!

-Brent


------------------------------

Message: 4
Date: Sat, 5 Dec 2009 22:09:42 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Indenting
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Samstag 05 Dezember 2009 21:09:01 schrieb Brent Yorgey:
> If, on the other hand, you want the indentation to correspond to how
> "deep" within the expression the evaluation is taking place, then you
> can recursively pass along an extra parameter to your evaluation
> function, like so:
>
>   eval :: Dict -> Expression -> ([String], Double)
>   eval d e = evalIndented 0 d e where
>     evalIndented i d (Val x) = ([], x)
>     evalIndented i d (Add x y) =
>       let (tx, vx) = evalIndented (i+1) d x
>           (ty, vy) = evalIndented (i+1) d y
>       in  (replicate i ' ' ++ "Add " ++ show vx ++ " and " ++ show vy, vx +
> vy)
>
> ...and so on.

I would even suggest

eval :: Dict -> Expression -> ([(Int,String)],Double)
eval d e = evalIndented 0 d e
      where
        evalIndented _ _ (Val x) = ([],x)
        evalIndented i d (Add x y) =
            let ...
            in ((i,"Add " ++ show vx ++ " and " ++ show vy):tx ++ ty, vx + vy)
        ...

then you can easily extract the traces of subexpressions at a given depth and 
print those 
with adjusted indentation and change indentation schemes (indent two/four 
spaces further 
per level).



------------------------------

Message: 5
Date: Sat, 05 Dec 2009 23:20:22 +0100
From: legajid <[email protected]>
Subject: [Haskell-beginners] Keyboard input
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello,
in order to get familiar with keyboard data entry, i'm writing a program 
for the well-known Hangman game.
Each entry must change several lists (letters found and remaining letters).
I have three main difficulties with data entry :

First When i enter a letter (A) , i get a good response (displaying old 
word and letters, message "guess ok") and an unattended one that says 
"guess false", thus displaying the result from my entry.
I don't understand why the process seems to execute twice, the fisrt one 
being correct, the second not.

Second difficulty : the  "procedure"  for data entry is written twice 
("guess a letter ..." prompt). I think it would be a good idea to write 
it once as a "function" that would display the prompt then get a 
character from the keyboard.

Third difficulty : in both cases (guess ok or false), i have to ask for 
a new entry; to avoid writing this twice again, i write it before the 
recursive calls of process_guess. Writing a function would perhaps allow 
to call it directly as a parameter of process_guess ?

Being new to haskell,  recursive functions now seem clear to me but i 
feel that IOs make writing a program more difficult. And i've not yet 
tried data base access...

Please, say me if the way i wrote my program is a good or bad approach.

Below an example of a run :


Here's my program.
Could you help me.
Thanks,
Didier

solution="HANGMAN"
word="H-----N"
letters=['A'..'Z']

hangman = do
  -- enter a letter
  putStrLn "Guess a letter (9 to end):"
  guess <- getChar
  -- process
  process_guess guess letters word
 
process_guess pg pletters pword =do
  let pguess = toUpper pg
  putStrLn pword
  putStrLn pletters
  if pguess == '9'
    then do putStrLn "Done"
    else do
      if pguess `elem` pletters
        then do putStrLn "Guess OK \n"
        else do putStrLn "Guess false \n"
     
      -- Enter a new letter before going on, in both cases
      putStrLn "Guess a letter (9 to end):"
      guess <- getChar

      if pguess `elem` pletters
        -- guess ok -> remove guess from available letters then add 
guess to word
        then do process_guess guess (newletters pletters pguess) 
(newword pword pguess solution)
       
        -- guess false -> remove guess from available letters, leave 
word unchanged
        else do process_guess guess (newletters pletters pguess) pword
       

newletters l g =
  filter (/= g) l
 

newword [] _ _ = []
newword (w:ws) g (s:ss) =
  if w == '-'
    then
      if s == g
        then g : newword ws g ss
        else w : newword ws g ss
    else
      w : newword ws g ss


toUpper c
  | isLower c = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
  | otherwise = c

isLower c = c >= 'a' && c <= 'z'



------------------------------

Message: 6
Date: Sun, 6 Dec 2009 00:24:48 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Keyboard input
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Samstag 05 Dezember 2009 23:20:22 schrieb legajid:
> Hello,
> in order to get familiar with keyboard data entry, i'm writing a program
> for the well-known Hangman game.
> Each entry must change several lists (letters found and remaining letters).
> I have three main difficulties with data entry :
>
> First When i enter a letter (A) , i get a good response (displaying old
> word and letters, message "guess ok") and an unattended one that says
> "guess false", thus displaying the result from my entry.
> I don't understand why the process seems to execute twice, the fisrt one
> being correct, the second not.

I don't get that behaviour, for me, after each guess, it displays only one 
message.

>
> Second difficulty : the  "procedure"  for data entry is written twice
> ("guess a letter ..." prompt). I think it would be a good idea to write
> it once as a "function" that would display the prompt then get a
> character from the keyboard.

Also, the entered letter appears before the word, which is not nice.
You can either

import System.IO

and at the beginning of hangman

 hSetEcho stdin False

and at the end

do putStrLn "Done"
   hSetEcho stdin True

or, without futzing with the echo setting:

import Data.Char (toUpper)
import Data.List (delete)

guessLetter :: IO Char
guessLetter = do
    putStrLn "Guess a letter (9 to end): "
    c <- getChar
    putChar '\b'
    return (toUpper c)

>
> Third difficulty : in both cases (guess ok or false), i have to ask for
> a new entry; to avoid writing this twice again, i write it before the
> recursive calls of process_guess. Writing a function would perhaps allow
> to call it directly as a parameter of process_guess ?

It would be cleaner to not have the guess as a parameter for the game loop.

>
> Being new to haskell,  recursive functions now seem clear to me but i
> feel that IOs make writing a program more difficult. And i've not yet
> tried data base access...
>
> Please, say me if the way i wrote my program is a good or bad approach.
>
> Below an example of a run :

Forgot that, didn't you?

>
>
> Here's my program.
> Could you help me.
> Thanks,
> Didier
>
> solution="HANGMAN"
> word="H-----N"
> letters=['A'..'Z']

It would be good to have a list of letters to be guessed,

wletters = "AGMN"

and check whether the guess appears there (-> Guess OK)
or not (-> Guess false :()
and remove correctly guessed letters from this, so when there are no more 
letters to be 
guessed you can detect it and

putStrLn "Congratulations"

to end the game.

The list of letters as allowed guesses is unnecessary, just check whether

'A' <= g && g <= 'Z'

where g is toUpper (entered letter), or don't care whether what is entered is a 
letter or 
something else.

>
> hangman = do
>   -- enter a letter
>   putStrLn "Guess a letter (9 to end):"
>   guess <- getChar
>   -- process
>   process_guess guess letters word

hangman = hangloop wletters word

>
> process_guess pg pletters pword =do

hangloop "" pword = do
    putStrLn pword
    putStrLn "Congratulations, you've solved it!"
hangloop pletters pword = do
    -- best to display the word first
    putStrLn pword
    g <- guessLetter
    if g == '9'
      then putStrLn "Bye"
      else do
            -- check whether the guess is good
            if g `notElem` pletters
              then do
                putStrLn "Wrong guess"
                hangloop pletters pword
              else do
                putStrLn "Good guess"
                let nletters = delete g pletters
                    nword = newword pword g solution
                hangloop nletters nword

>   let pguess = toUpper pg
>   putStrLn pword
>   putStrLn pletters
>   if pguess == '9'
>     then do putStrLn "Done"
>     else do
>       if pguess `elem` pletters
>         then do putStrLn "Guess OK \n"
>         else do putStrLn "Guess false \n"
>
>       -- Enter a new letter before going on, in both cases
>       putStrLn "Guess a letter (9 to end):"
>       guess <- getChar
>
>       if pguess `elem` pletters
>         -- guess ok -> remove guess from available letters then add
> guess to word
>         then do process_guess guess (newletters pletters pguess)
> (newword pword pguess solution)
>
>         -- guess false -> remove guess from available letters, leave
> word unchanged
>         else do process_guess guess (newletters pletters pguess) pword
>
>
> newletters l g =
>   filter (/= g) l

if every letter appears only once in l, Data.List.delete g l is perhaps better.

>
>
> newword [] _ _ = []
> newword (w:ws) g (s:ss) =
>   if w == '-'
>     then
>       if s == g
>         then g : newword ws g ss
>         else w : newword ws g ss
>     else
>       w : newword ws g ss

better pattern-match:

newword "" _ _ = ""
newword ('-':ws) g (s:ss)
    | g == s    = g:newword ws g ss
newword (w:ws) g (s:ss) = w:newword ws g ss

or, for the nonempty case:

newword (w:ws) g (s:ss)
    | w == '-' && s == g  = g:newword ws g ss
    | otherwise = w:newword ws g ss

>
>

-- these are in Data.Char

> toUpper c
>
>   | isLower c = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
>   | otherwise = c
>
> isLower c = c >= 'a' && c <= 'z'



------------------------------

Message: 7
Date: Sun, 6 Dec 2009 15:16:29 +0530
From: Vinod Parthasarathy <[email protected]>
Subject: [Haskell-beginners] freelancing jobs in haskell
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
I recently learnt Haskell as part of my post graduate course work in
Computer Science. I am looking for freelancing websites which offer online
jobs in Haskell. If any of you know any such website where I could look for
freelancing jobs, could you please let me know? I am from India, and am also
looking for jobs in India in Haskell programming. Any help in that direction
would also be helpful.

Vinod.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091206/3592f03d/attachment-0001.html

------------------------------

Message: 8
Date: Mon, 07 Dec 2009 22:52:42 +0100
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] Keyboard input
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hello,
ok for the responses.
However, about the strange behaviour of getChar, i forgot to say i'm 
running on Windows XP with GHci 6.10.4
It seems that getChar buffers all characters, including newline; so, 
typing A + enter is equivalent to typing 2 characters successively.
To successfully solve the problem, i did the following :

guessLetter :: IO Char
guessLetter = do
    putStrLn "Guess a letter (9 to end): "
    cl <- getLine
    let c= extr cl
    -- c <- getChar
    -- putChar '\b'
    return (toUpper c)

extr :: String -> Char
extr (c:cs)=c


Notice that, on my system, putChar has no visible effect.

Seeking for info, i found this mail : 
http://old.nabble.com/How-to-getCh-on-MS-Windows-command-line--td20414545.html
that talks about a similar issue on Windows

Didier.



Daniel Fischer a écrit :
> Am Samstag 05 Dezember 2009 23:20:22 schrieb legajid:
>   
>> Hello,
>> in order to get familiar with keyboard data entry, i'm writing a program
>> for the well-known Hangman game.
>> Each entry must change several lists (letters found and remaining letters).
>> I have three main difficulties with data entry :
>>
>> First When i enter a letter (A) , i get a good response (displaying old
>> word and letters, message "guess ok") and an unattended one that says
>> "guess false", thus displaying the result from my entry.
>> I don't understand why the process seems to execute twice, the fisrt one
>> being correct, the second not.
>>     
>
> I don't get that behaviour, for me, after each guess, it displays only one 
> message.
>
>   
>> Second difficulty : the  "procedure"  for data entry is written twice
>> ("guess a letter ..." prompt). I think it would be a good idea to write
>> it once as a "function" that would display the prompt then get a
>> character from the keyboard.
>>     
>
> Also, the entered letter appears before the word, which is not nice.
> You can either
>
> import System.IO
>
> and at the beginning of hangman
>
>  hSetEcho stdin False
>
> and at the end
>
> do putStrLn "Done"
>    hSetEcho stdin True
>
> or, without futzing with the echo setting:
>
> import Data.Char (toUpper)
> import Data.List (delete)
>
> guessLetter :: IO Char
> guessLetter = do
>     putStrLn "Guess a letter (9 to end): "
>     c <- getChar
>     putChar '\b'
>     return (toUpper c)
>
>   
>> Third difficulty : in both cases (guess ok or false), i have to ask for
>> a new entry; to avoid writing this twice again, i write it before the
>> recursive calls of process_guess. Writing a function would perhaps allow
>> to call it directly as a parameter of process_guess ?
>>     
>
> It would be cleaner to not have the guess as a parameter for the game loop.
>
>   
>> Being new to haskell,  recursive functions now seem clear to me but i
>> feel that IOs make writing a program more difficult. And i've not yet
>> tried data base access...
>>
>> Please, say me if the way i wrote my program is a good or bad approach.
>>
>> Below an example of a run :
>>     
>
> Forgot that, didn't you?
>
>   
>> Here's my program.
>> Could you help me.
>> Thanks,
>> Didier
>>
>> solution="HANGMAN"
>> word="H-----N"
>> letters=['A'..'Z']
>>     
>
> It would be good to have a list of letters to be guessed,
>
> wletters = "AGMN"
>
> and check whether the guess appears there (-> Guess OK)
> or not (-> Guess false :()
> and remove correctly guessed letters from this, so when there are no more 
> letters to be 
> guessed you can detect it and
>
> putStrLn "Congratulations"
>
> to end the game.
>
> The list of letters as allowed guesses is unnecessary, just check whether
>
> 'A' <= g && g <= 'Z'
>
> where g is toUpper (entered letter), or don't care whether what is entered is 
> a letter or 
> something else.
>
>   
>> hangman = do
>>   -- enter a letter
>>   putStrLn "Guess a letter (9 to end):"
>>   guess <- getChar
>>   -- process
>>   process_guess guess letters word
>>     
>
> hangman = hangloop wletters word
>
>   
>> process_guess pg pletters pword =do
>>     
>
> hangloop "" pword = do
>     putStrLn pword
>     putStrLn "Congratulations, you've solved it!"
> hangloop pletters pword = do
>     -- best to display the word first
>     putStrLn pword
>     g <- guessLetter
>     if g == '9'
>       then putStrLn "Bye"
>       else do
>             -- check whether the guess is good
>             if g `notElem` pletters
>               then do
>                 putStrLn "Wrong guess"
>                 hangloop pletters pword
>               else do
>                 putStrLn "Good guess"
>                 let nletters = delete g pletters
>                     nword = newword pword g solution
>                 hangloop nletters nword
>
>   
>>   let pguess = toUpper pg
>>   putStrLn pword
>>   putStrLn pletters
>>   if pguess == '9'
>>     then do putStrLn "Done"
>>     else do
>>       if pguess `elem` pletters
>>         then do putStrLn "Guess OK \n"
>>         else do putStrLn "Guess false \n"
>>
>>       -- Enter a new letter before going on, in both cases
>>       putStrLn "Guess a letter (9 to end):"
>>       guess <- getChar
>>
>>       if pguess `elem` pletters
>>         -- guess ok -> remove guess from available letters then add
>> guess to word
>>         then do process_guess guess (newletters pletters pguess)
>> (newword pword pguess solution)
>>
>>         -- guess false -> remove guess from available letters, leave
>> word unchanged
>>         else do process_guess guess (newletters pletters pguess) pword
>>
>>
>> newletters l g =
>>   filter (/= g) l
>>     
>
> if every letter appears only once in l, Data.List.delete g l is perhaps 
> better.
>
>   
>> newword [] _ _ = []
>> newword (w:ws) g (s:ss) =
>>   if w == '-'
>>     then
>>       if s == g
>>         then g : newword ws g ss
>>         else w : newword ws g ss
>>     else
>>       w : newword ws g ss
>>     
>
> better pattern-match:
>
> newword "" _ _ = ""
> newword ('-':ws) g (s:ss)
>     | g == s    = g:newword ws g ss
> newword (w:ws) g (s:ss) = w:newword ws g ss
>
> or, for the nonempty case:
>
> newword (w:ws) g (s:ss)
>     | w == '-' && s == g  = g:newword ws g ss
>     | otherwise = w:newword ws g ss
>
>   
>>     
>
> -- these are in Data.Char
>
>   
>> toUpper c
>>
>>   | isLower c = toEnum (fromEnum c - fromEnum 'a' + fromEnum 'A')
>>   | otherwise = c
>>
>> isLower c = c >= 'a' && c <= 'z'
>>     
>
>
>   



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 18, Issue 5
****************************************

Reply via email to