Jump to content

Haskell problem

- - - - -

  • Please log in to reply
1 reply to this topic

#1
zmir

zmir

    Newbie

  • Members
  • Pip
  • 1 posts
...

Edited by zmir, 09 August 2011 - 12:14 PM.


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
All right, are you using this program by piping file data through stdin?
echo "Peter" | ./my_program
getContents retrieves the entirety of the user input up until an EOF is encountered, and is evaluated lazily. What this means is that in methods that print values without reaching the end of the input, this produces a very simple method of writing interactive user input using lines.

Also, your count method wouldn't compile. Length can't be passed to x as x is expected to be a Char type, which doesn't accept a function of type ([a] -> Int) as an argument. I think you're trying to do something more like this:
count :: Ord a => [a] -> [(a, Integer)]
count = map (\x -> (head x, length x)) . group . sort
Which is an okay solution, but won't work too well if you don't separate the input by lines first, as the program will wait for the user to finish writing the input, as the length function does not return until it goes through the entire list. You should split up your input and go through it line by line in freqs.
freqs :: String -> IO ()
freqs str = sequence_ (map (putStrLn . show) $ map count $ lines str)
This should produce results each time you press enter. :) Then main can be implemented like this:
main = getContents >>= freqs

Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users