Return value of forM in Haskell - list

If I have a list input = ["1", "-2", "14", "3"] with length nand I want to convert it into an Int list, what is the return value of
forM [0..(n-1)] $ \i -> do
let t = read (input!!(i)) :: Int
return ()
I figured out that t will contain the Int value of each string from the input list but I don't understand how should I put them in a new list. Note that this forM syntax is not written by me. I researched how I should work with monads but I still don't really get it since I couldn't find a single example.

If I have a list input = ["1", "-2", "14", "3"] with length n and I want to convert it into an Int list.
Then you should use:
map read input :: [Int]
Indeed:
Prelude> input = ["1", "-2", "14", "3"]
Prelude> map read input :: [Int]
[1,-2,14,3]
I figured out that t will contain the Int value of each string from the input list but I don't understand how should I put them in a new list.
Variables in (pure) functional languages, like Haskell, are immutable. That means that once a variable is set to a value, you can not set it to a different value.
In fact in Haskell, one seldomly use looping mechanisms like forM, etc. The M suffix usually says you are working with Monads, but here you do not need monads at all. I strongly advice not to use do, etc. before you have some basic understanding on how a monad works. See for example the A fistful of Monads chapter on Learn You A Haskell [lyah].

Related

Outputting a list of lists in Haskell?

I am a complete beginner to Haskell but I'm being asked to create a sudoku solver. I've been making some steady progress with it but one of the things it is asking me to do is print a valid representation of a sudoku puzzle s. The Puzzle data type is defined as a list of lists, so [[Maybe Int]] and this is composed of Block values ([Maybe Int], representing a row).
Function signature is this:
printPuzzle :: Puzzle -> IO ()
How do I output this? I know this may be a simple question and I'm missing the point but I'm still not at the stage where I've got my ahead around the syntax yet. Any help would be much appreciated!
Simple pretty-printing of this can be done really succinctly with something like the following:
import Data.Char (intToDigit)
showRow :: [Maybe Int] -> String
showRow = map (maybe ' ' intToDigit)
showPuzzle :: [[Maybe Int]] -> [String]
showPuzzle = map showRow
printPuzzle :: [[Maybe Int]] -> IO ()
printPuzzle = mapM_ putStrLn . showPuzzle
showRow takes a single row from your grid and prints it - using the maybe function from Data.Maybe, we can write this as a quick map from each Maybe Int value to either a default "blank space" value or the character representing the number (using intToDigit).
showPuzzle simply maps showRow over the outer list.
printPuzzle just uses the previous pure definitions to give the impure action which prints a grid, by putStrLn'ing the pretty-print of each row.
A quick demo:
> printPuzzle [[Just 1, Nothing, Just 3],
[Nothing, Just 3, Just 6],
[Just 2, Just 4, Just 5]]
1 3
36
245
Though you can easily modify the above code to print something more explicit, like:
1X3
X36
245

How to add tuples to list after reading from a text file in Haskell

I am trying to create a program in Haskell that reads text from a text file and adds them to a list.
My idea is:
type x = [(String, Integer)]
where the String is each word from the text and Integer is how many times that word occurs in the text. So I want to create a tuple of those values and add it to a list. I then want to print the contents of the list.
I know how to read a text file in Haskell, but am unsure as to what to do next. I am new to programming in Haskell and have predominantly been programming in Java which is very different.
EDIT:
This is what I have so far from the suggestions. I am able to write to an output text file with the text received from the file and make it lower case. The issues I am having is using the other functions because it says:
Test.hs:14:59: Not in scope: ‘group’
Here is the code:
import System.IO
import Data.Char(toLower)
main = do
contents <- readFile "testFile.txt"
let lowContents = map toLower contents
let outStr = countWords (lowContents)
let finalStr = sortOccurrences (outStr)
print outStr
-- Counts all the words
countWords :: String -> [(String, Int)]
countWords fileContents = countOccurrences (toWords fileContents)
-- Split words
toWords :: String -> [String]
toWords s = words s
-- Counts, how often each string in the given list appears
countOccurrences :: [String] -> [(String, Int)]
countOccurrences xs = map (\xs -> (head xs, length xs)) . group . sortOccurrences xs
-- Sort list in order of occurrences.
sortOccurrences :: [(String, Int)] -> [(String, Int)]
sortOccurrences sort = sortBy sort (comparing snd)
Please can anyone help me with this.
Haskell features a fairly expressive type system (much more so than Java) so it's a good idea to consider this issue purely in terms of types, in a top-down fashion. You mentioned that you already know how read a text file in Haskell, so I'll assume you know how to get a String which holds the file contents.
The function you'd like to define is something like this. For now, we'll set the definition to undefined such that the code typechecks (but yields an exception at runtime):
countWords :: String -> [(String, Int)]
countWords fileContents = undefined
Your function maps a String (the file contents) to a list of tuples, each of which associating some word with the count how often that word appeared in the input. This sounds like one part of the solution will be a function which can split a string into a list of words such that you can then process that to count the words. I.e. you'll want something like this:
-- Splits a string into a list of words
toWords :: String -> [String]
toWords s = undefined
-- Counts, how often each string in the given list appears
countOccurrences :: [String] -> [(String, Int)]
countOccurrences xs = undefined
With these at hand, you can actually define the original function:
countWords :: String -> [(String, Int)]
countWords fileContents = countOccurrences (toWords fileContents)
You now nicely decomposed the problem into two sub-problems.
Another nice aspect of this type-driven programm is that Hoogle can be told to go look for functions for a given type. For instance, consider the type of the toWords function we sketched earlier:
toWords :: String -> [String]
toWords s = undefined
Feeding this to Hoogle reveals a nice function: words which seems to do just what we want! So we can define
toWords :: String -> [String]
toWords s = words s
The only thing missing is coming up with an appropriate definition for countOccurrences. Alas, searching for this type on Hoogle doesn't show any ready-made solutions. However, there are three functions which will be useful for coming up with our own definition: sort, group and map:
The sort function does, what the name suggests: it sorts a list of things:
λ: sort [1,1,1,2,2,1,1,3,3]
[1,1,1,1,1,2,2,3,3]
The group function groups consecutive(!) equal elements, yielding a list of lists. E.g.
λ: group [1,1,1,1,1,2,2,3,3]
[[1,1,1,1,1],[2,2],[3,3]]
The map function can be used to turn the list of lists produced by group into a list of tuples, giving the length of each group:
λ: map (\xs -> (head xs, length xs)) [[1,1,1,1,1],[2,2],[3,3]]
[(1,5),(2,2),(3,2)]
Composing these three functions allows you to define
countOccurrences :: [String] -> [(String, Int)]
countOccurrences xs = map (\xs -> (head xs, length xs)) . group . sort $ xs
Now you have all the pieces in place. Your countWords is defined in terms to toWords and countOccurrences, each of which having a proper definition.
The nice thing about this type-driven approach is that writing down the funciton signatures will help both your thinking as well as the compiler (catching you when you violate assumptions). You also, automatically, decompose the problem into smaller problems, each of which you can test independently in ghci.
Data.Map is the easiest way to do this.
import qualified Data.Map as M
-- assuming you already have your list of words:
listOfWords :: [String]
-- you can generate your list of tuples with this
listOfTuples :: [(String, Integer)]
listOfTuples = M.toList . M.fromListWith (+) $ zip listOfWords (repeat 1)

converting list to tuple

I have few command-line options (5 for example) and I want to convert them to tuple. The problem is that I expect them to appear in correct order, so tuple can be easily built from list using pattern-match, but in real life options can be provided in random order, so I don't know if head of the list contain Verbose option or log file name?
I tried to think how to do that using continuation-passing style, however nothing useful comes into my mind.
Is that ever possible?
I think that I can "sort" the list to have it in predicted order, but it does not look good.
Also I could get rid of the tuple and create data record - however that will still lead up to checking the type of attribute and set the correct field of the record. Still a lot of typing.
Given what you describe, I think you have two options. Of the two, I would say that converting to a dictionary would be easiest, but converting to a Tuple would work and only be a little clumsy
So, take this definition:
options :: [OptDescr (String, String)]
options = [Option ['a'] ["alpha"] (ReqArg (\a -> ("alpha", a)) "empty") "",
Option ['b'] ["beta"] (ReqArg (\a -> ("beta", a)) "empty") "",
Option ['g'] ["gamma"] (ReqArg (\a -> ("gamma", a)) "empty") ""]
main = do
args <- getArgs
let (opts, nonopts, errs) = getOpt Permute options args
putStrLn $ show opts
From this, a couple of my example outputs are:
[("beta","b"),("alpha","a")]
[("alpha","a"),("gamma","g"),("beta","b")]
and so on. Same order as on the command line. But, because of the way I set it up above, I basically have an association list, so... if I in particular want a Tuple that has the values (alpha, beta, gamma), then my best option is...
(lookup "alpha" opts, lookup "beta" opts, lookup "gamma" opts)
You resulting data type would be (Maybe String, Maybe String, Maybe String), in the order of "alpha", "beta", and "gamma".

Haskell - Convert x number of tuples into a list [duplicate]

I have a question about tuples and lists in Haskell. I know how to add input into a tuple a specific number of times. Now I want to add tuples into a list an unknown number of times; it's up to the user to decide how many tuples they want to add.
How do I add tuples into a list x number of times when I don't know X beforehand?
There's a lot of things you could possibly mean. For example, if you want a few copies of a single value, you can use replicate, defined in the Prelude:
replicate :: Int -> a -> [a]
replicate 0 x = []
replicate n | n < 0 = undefined
| otherwise = x : replicate (n-1) x
In ghci:
Prelude> replicate 4 ("Haskell", 2)
[("Haskell",2),("Haskell",2),("Haskell",2),("Haskell",2)]
Alternately, perhaps you actually want to do some IO to determine the list. Then a simple loop will do:
getListFromUser = do
putStrLn "keep going?"
s <- getLine
case s of
'y':_ -> do
putStrLn "enter a value"
v <- readLn
vs <- getListFromUser
return (v:vs)
_ -> return []
In ghci:
*Main> getListFromUser :: IO [(String, Int)]
keep going?
y
enter a value
("Haskell",2)
keep going?
y
enter a value
("Prolog",4)
keep going?
n
[("Haskell",2),("Prolog",4)]
Of course, this is a particularly crappy user interface -- I'm sure you can come up with a dozen ways to improve it! But the pattern, at least, should shine through: you can use values like [] and functions like : to construct lists. There are many, many other higher-level functions for constructing and manipulating lists, as well.
P.S. There's nothing particularly special about lists of tuples (as compared to lists of other things); the above functions display that by never mentioning them. =)
Sorry, you can't1. There are fundamental differences between tuples and lists:
A tuple always have a finite amount of elements, that is known at compile time. Tuples with different amounts of elements are actually different types.
List an have as many elements as they want. The amount of elements in a list doesn't need to be known at compile time.
A tuple can have elements of arbitrary types. Since the way you can use tuples always ensures that there is no type mismatch, this is safe.
On the other hand, all elements of a list have to have the same type. Haskell is a statically-typed language; that basically means that all types are known at compile time.
Because of these reasons, you can't. If it's not known, how many elements will fit into the tuple, you can't give it a type.
I guess that the input you get from your user is actually a string like "(1,2,3)". Try to make this directly a list, whithout making it a tuple before. You can use pattern matching for this, but here is a slightly sneaky approach. I just remove the opening and closing paranthesis from the string and replace them with brackets -- and voila it becomes a list.
tuplishToList :: String -> [Int]
tuplishToList str = read ('[' : tail (init str) ++ "]")
Edit
Sorry, I did not see your latest comment. What you try to do is not that difficult. I use these simple functions for my task:
words str splits str into a list of words that where separated by whitespace before. The output is a list of Strings. Caution: This only works if the string inside your tuple contains no whitespace. Implementing a better solution is left as an excercise to the reader.
map f lst applies f to each element of lst
read is a magic function that makes a a data type from a String. It only works if you know before, what the output is supposed to be. If you really want to understand how that works, consider implementing read for your specific usecase.
And here you go:
tuplish2List :: String -> [(String,Int)]
tuplish2List str = map read (words str)
1 As some others may point out, it may be possible using templates and other hacks, but I don't consider that a real solution.
When doing functional programming, it is often better to think about composition of operations instead of individual steps. So instead of thinking about it like adding tuples one at a time to a list, we can approach it by first dividing the input into a list of strings, and then converting each string into a tuple.
Assuming the tuples are written each on one line, we can split the input using lines, and then use read to parse each tuple. To make it work on the entire list, we use map.
main = do input <- getContents
let tuples = map read (lines input) :: [(String, Integer)]
print tuples
Let's try it.
$ runghc Tuples.hs
("Hello", 2)
("Haskell", 4)
Here, I press Ctrl+D to send EOF to the program, (or Ctrl+Z on Windows) and it prints the result.
[("Hello",2),("Haskell",4)]
If you want something more interactive, you will probably have to do your own recursion. See Daniel Wagner's answer for an example of that.
One simple solution to this would be to use a list comprehension, as so (done in GHCi):
Prelude> let fstMap tuplist = [fst x | x <- tuplist]
Prelude> fstMap [("String1",1),("String2",2),("String3",3)]
["String1","String2","String3"]
Prelude> :t fstMap
fstMap :: [(t, b)] -> [t]
This will work for an arbitrary number of tuples - as many as the user wants to use.
To use this in your code, you would just write:
fstMap :: Eq a => [(a,b)] -> [a]
fstMap tuplist = [fst x | x <- tuplist]
The example I gave is just one possible solution. As the name implies, of course, you can just write:
fstMap' :: Eq a => [(a,b)] -> [a]
fstMap' = map fst
This is an even simpler solution.
I'm guessing that, since this is for a class, and you've been studying Haskell for < 1 week, you don't actually need to do any input/output. That's a bit more advanced than you probably are, yet. So:
As others have said, map fst will take a list of tuples, of arbitrary length, and return the first elements. You say you know how to do that. Fine.
But how do the tuples get into the list in the first place? Well, if you have a list of tuples and want to add another, (:) does the trick. Like so:
oldList = [("first", 1), ("second", 2)]
newList = ("third", 2) : oldList
You can do that as many times as you like. And if you don't have a list of tuples yet, your list is [].
Does that do everything that you need? If not, what specifically is it missing?
Edit: With the corrected type:
Eq a => [(a, b)]
That's not the type of a function. It's the type of a list of tuples. Just have the user type yourFunctionName followed by [ ("String1", val1), ("String2", val2), ... ("LastString", lastVal)] at the prompt.

How do I add x tuples into a list x number of times?

I have a question about tuples and lists in Haskell. I know how to add input into a tuple a specific number of times. Now I want to add tuples into a list an unknown number of times; it's up to the user to decide how many tuples they want to add.
How do I add tuples into a list x number of times when I don't know X beforehand?
There's a lot of things you could possibly mean. For example, if you want a few copies of a single value, you can use replicate, defined in the Prelude:
replicate :: Int -> a -> [a]
replicate 0 x = []
replicate n | n < 0 = undefined
| otherwise = x : replicate (n-1) x
In ghci:
Prelude> replicate 4 ("Haskell", 2)
[("Haskell",2),("Haskell",2),("Haskell",2),("Haskell",2)]
Alternately, perhaps you actually want to do some IO to determine the list. Then a simple loop will do:
getListFromUser = do
putStrLn "keep going?"
s <- getLine
case s of
'y':_ -> do
putStrLn "enter a value"
v <- readLn
vs <- getListFromUser
return (v:vs)
_ -> return []
In ghci:
*Main> getListFromUser :: IO [(String, Int)]
keep going?
y
enter a value
("Haskell",2)
keep going?
y
enter a value
("Prolog",4)
keep going?
n
[("Haskell",2),("Prolog",4)]
Of course, this is a particularly crappy user interface -- I'm sure you can come up with a dozen ways to improve it! But the pattern, at least, should shine through: you can use values like [] and functions like : to construct lists. There are many, many other higher-level functions for constructing and manipulating lists, as well.
P.S. There's nothing particularly special about lists of tuples (as compared to lists of other things); the above functions display that by never mentioning them. =)
Sorry, you can't1. There are fundamental differences between tuples and lists:
A tuple always have a finite amount of elements, that is known at compile time. Tuples with different amounts of elements are actually different types.
List an have as many elements as they want. The amount of elements in a list doesn't need to be known at compile time.
A tuple can have elements of arbitrary types. Since the way you can use tuples always ensures that there is no type mismatch, this is safe.
On the other hand, all elements of a list have to have the same type. Haskell is a statically-typed language; that basically means that all types are known at compile time.
Because of these reasons, you can't. If it's not known, how many elements will fit into the tuple, you can't give it a type.
I guess that the input you get from your user is actually a string like "(1,2,3)". Try to make this directly a list, whithout making it a tuple before. You can use pattern matching for this, but here is a slightly sneaky approach. I just remove the opening and closing paranthesis from the string and replace them with brackets -- and voila it becomes a list.
tuplishToList :: String -> [Int]
tuplishToList str = read ('[' : tail (init str) ++ "]")
Edit
Sorry, I did not see your latest comment. What you try to do is not that difficult. I use these simple functions for my task:
words str splits str into a list of words that where separated by whitespace before. The output is a list of Strings. Caution: This only works if the string inside your tuple contains no whitespace. Implementing a better solution is left as an excercise to the reader.
map f lst applies f to each element of lst
read is a magic function that makes a a data type from a String. It only works if you know before, what the output is supposed to be. If you really want to understand how that works, consider implementing read for your specific usecase.
And here you go:
tuplish2List :: String -> [(String,Int)]
tuplish2List str = map read (words str)
1 As some others may point out, it may be possible using templates and other hacks, but I don't consider that a real solution.
When doing functional programming, it is often better to think about composition of operations instead of individual steps. So instead of thinking about it like adding tuples one at a time to a list, we can approach it by first dividing the input into a list of strings, and then converting each string into a tuple.
Assuming the tuples are written each on one line, we can split the input using lines, and then use read to parse each tuple. To make it work on the entire list, we use map.
main = do input <- getContents
let tuples = map read (lines input) :: [(String, Integer)]
print tuples
Let's try it.
$ runghc Tuples.hs
("Hello", 2)
("Haskell", 4)
Here, I press Ctrl+D to send EOF to the program, (or Ctrl+Z on Windows) and it prints the result.
[("Hello",2),("Haskell",4)]
If you want something more interactive, you will probably have to do your own recursion. See Daniel Wagner's answer for an example of that.
One simple solution to this would be to use a list comprehension, as so (done in GHCi):
Prelude> let fstMap tuplist = [fst x | x <- tuplist]
Prelude> fstMap [("String1",1),("String2",2),("String3",3)]
["String1","String2","String3"]
Prelude> :t fstMap
fstMap :: [(t, b)] -> [t]
This will work for an arbitrary number of tuples - as many as the user wants to use.
To use this in your code, you would just write:
fstMap :: Eq a => [(a,b)] -> [a]
fstMap tuplist = [fst x | x <- tuplist]
The example I gave is just one possible solution. As the name implies, of course, you can just write:
fstMap' :: Eq a => [(a,b)] -> [a]
fstMap' = map fst
This is an even simpler solution.
I'm guessing that, since this is for a class, and you've been studying Haskell for < 1 week, you don't actually need to do any input/output. That's a bit more advanced than you probably are, yet. So:
As others have said, map fst will take a list of tuples, of arbitrary length, and return the first elements. You say you know how to do that. Fine.
But how do the tuples get into the list in the first place? Well, if you have a list of tuples and want to add another, (:) does the trick. Like so:
oldList = [("first", 1), ("second", 2)]
newList = ("third", 2) : oldList
You can do that as many times as you like. And if you don't have a list of tuples yet, your list is [].
Does that do everything that you need? If not, what specifically is it missing?
Edit: With the corrected type:
Eq a => [(a, b)]
That's not the type of a function. It's the type of a list of tuples. Just have the user type yourFunctionName followed by [ ("String1", val1), ("String2", val2), ... ("LastString", lastVal)] at the prompt.