Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - Trying to apply a function to lines of multiple numbers

I am new to Haskell and I am trying to apply a function (gcd) to input on standard in, which is line separated and each line contains no less or more than two numbers. Here is an example of my input:

3
10 4
1 100
288 240

I am currently breaking up each line into a tuple of both numbers, but I am having trouble figuring out how to separate these tuples and apply a function to them. Here is what I have so far:

import Data.List

main :: IO ()
main = do
  n <- readLn :: IO Int
  content <- getContents
  let  
    points = map (\[x, y] -> (x, y)). map (map (read::String->Int)). map words. lines $ content
    ans = gcd (fst points :: Int) (snd points :: Int)
  print ans

Any information as two a good place to start looking for this answer would be much appreciated. I have read through the Learning Haskell tutorial and have not found any information of this particular problem.

like image 243
midnightconman Avatar asked Mar 30 '15 23:03

midnightconman


People also ask

What is a function in Haskell?

Haskell - Functions. Functions play a major role in Haskell, as it is a functional programming language. Like other languages, Haskell does have its own functional definition and declaration. Function declaration consists of the function name and its argument list along with its output.

What is the correct syntax for'E'in Haskell?

Its syntax should be the tersest; just as the most common letter in English, 'e', is encoded as a single 'dot' in the Morse alphabet. In the next tutorial we'll learn how to define a function in Haskell.

What is a lambda expression in Haskell?

We sometimes have to write a function that is going to be used only once, throughout the entire lifespan of an application. To deal with this kind of situations, Haskell developers use another anonymous block known as lambda expression or lambda function. A function without having a definition is called a lambda function.

How can I loop an expression in Haskell?

Haskell does not provide any facility of looping any expression for more than once. Instead, Haskell wants you to break your entire functionality into a collection of different functions and use recursion technique to implement your functionality.


1 Answers

You are pretty close. There is no reason to convert to a tuple or list of tuples before calling gcd.

main = do 
  contents <- getContents 
  print $ map ((\[x,y] -> gcd (read x) (read y)) . words) . lines $ contents 

All the interesting stuff is between print and contents. lines will split the contents into lines. map (...) applies the function to each line. words splits the line into words. \[x,y] -> gcd (read x) (read y) will match on a list of two strings (and throw an error otherwise - not good practice in general but fine for a simple program like this), read those strings as Integers and compute their GCD.

If you want to make use of lazy IO, in order to print each result after you enter each line, you can change it as follows.

main = do 
  contents <- getContents 
  mapM_ (print . (\[x,y] -> gcd (read x) (read y)) . words) . lines $ contents 
like image 196
user2407038 Avatar answered Sep 22 '22 05:09

user2407038