Calculate and use a linear equation based on sets of x and y values The following asks for 2 sets of data (with corresponding x and y values), calculates a linear equation that matches the data as closely as possible, then allows you to "play around" with the equation by substituting x values. 

import Data.Char

slope :: [Float] -&gt; [Float] -&gt; Float
slope xs ys
    | xN /= yN = error $ "x len: " ++ show xN ++ " y len: " ++ show yN
    | otherwise = (xN * (sum $ coorProds xs ys) - xSum * ySum) /
        (xN * (sum $ squares xs) - (xSum * xSum))
    where
        (xN,yN) = (fromIntegral $ length xs, fromIntegral $ length ys)
        (xSum,ySum) = (sum xs, sum ys)

intercept :: [Float] -&gt; [Float] -&gt; Float
intercept xs ys
    | xN /= yN = error $ "x len: " ++ show xN ++ " y len: " ++ show yN
    | otherwise = (ySum - (b * xSum)) / xN
    where
        b = slope xs ys
        (xN,yN) = (fromIntegral $ length xs, fromIntegral $ length ys)
        (xSum,ySum) = (sum xs, sum ys)

getEquation :: [Float] -&gt; [Float] -&gt; ((Float -&gt; Float),Float,Float)
getEquation xs ys = (\x -&gt; (inter + (b * x)) , inter, b)
    where
        inter = intercept xs ys
        b = slope xs ys

coorProds :: [Float] -&gt; [Float] -&gt; [Float]
coorProds xs ys = map (\(x,y) -&gt; x * y) $ zip xs ys

squares :: [Float] -&gt; [Float]
squares = map (\x -&gt; x * x)

cutIntoPair :: String -&gt; (String,String)
cutIntoPair = (\(x,y)-&gt; (x,drop 1 y)) . break (== ' ')

isFloat :: String -&gt; Bool
isFloat = all (\c -&gt; isDigit c || c == '.')

toFloat :: String -&gt; Float
toFloat s
    | isFloat s = read s :: Float
    | otherwise = error $ show s ++ " is not a number"

getPairs :: IO ([Float],[Float])
getPairs = do
    i &lt;- getLine
    let (x,y) = cutIntoPair i
    if isFloat x
        then do
            (xs,ys) &lt;- getPairs
            return ((toFloat x : xs), (toFloat y : ys))
        else return ([],[])

useEquation :: (Float -&gt; Float) -&gt; IO ()
useEquation e = do
    putStr "When x = "; x &lt;- getLine
    putStrLn $ "y = " ++ (show $ e (toFloat x)) ++ "\n"
    useEquation e

showEquation :: Float -&gt; Float -&gt; String
showEquation i b = "y = " ++ show i ++ " + " ++ show b ++ "x"

main = do
    putStrLn $ "Enter x and y values seperated by a space\n\t" ++
        "Then type a non-number to continue"
    (xs,ys) &lt;- getPairs
    let (linEq,i,b) = getEquation xs ys
    putStrLn $ showEquation i b
    putStrLn "Enter x values to get their calculated y values"
    useEquation linEq


I'm looking for general feedback, and suggestions regarding the following points:


getEquation has a couple issues.


I was able to write it point-free when the function only returned the equation, but now that I'm wrapping everything in a tuple, so I can also return the calculated slope and intercept values (to show the equation later), it's not behaving as I expected. I tried writing it as a section,  like (inter + (b *), but that attempts to apply + to the function (b*), which obviously results in a "No instance" error. My solution was to just wrap it in a lambda, but that's quite ugly, in my opinion.
Is there a better way of dealing with the need to have the slope and intercept after the function is "constructed" than passing them back with the equation? The optimal solution would the ability to extract them out of the function, but I'm pretty sure that's not possible.

cutIntoPair looks ugly too. It splits a string at the first space, then returns the 2 parts. Break leaves the space in the second slot though, so to remove it, I resorted to passing the result to a lambda that leaves the first part of the tuple the same, but drops the first char of the second slot. I'd like to know if there is a cleaner way to go about this task.
The slope and intercept functions are slightly messy, but I had to cram a lot of math into the space. Tips on how to format it neater would be appreciated.


To use it, type x and y values separated by a space, pressing enter between sets. When you're done entering, type a non-number, like so:


2 4
4 8
12 24
k
I'd structure your program in a slightly different way. First, instead of passing around two arrays of float, why don't you introduce a Point data structure and work with it?
It would save you from checking that you get passed two array of the same length, which looks quite neat to me.

I think you could also consider introducing a Coefficients data structure to hold your pair of slope and intercept value. If you do that it will be much clearer what your function does.

Since you eventually want to have a function you can play with, you could create a toFunction :: Coefficients -&gt; Float -&gt;Float. In this way you decouple the computation of the coefficients that best fit with your data and the construction of the corresponding curve. This enables you to play not only with the curves you fit from data but also with curves you can define from their coefficients.

I think that a nicer way to compute the value could be by putting as much math as possible in some temporary variables you define in the where block. That could allow you to separate the high level computation you need to perform with all the other smaller steps required to compute some other intermediate results you need.

If you apply the changes I suggested you should come up with code similar to the following:

type Point = (Float, Float)
type Coefficients = (Float, Float)

toFunction :: Coefficients -&gt; Float -&gt; Float
toFunction (s, i) x = s * x + i

slope :: [Point] -&gt; Float
slope [] = error "Empty list of points"
slope (_ : []) = error "Single point"
slope points = (n*sumCoorProds - (sumX*sumY))/ (n*sumSquares - (sumX*sumX))
  where
    n = fromIntegral $ length points
    (xs, ys) = unzip points
    sumCoorProds = sum $ map ( \(x, y) -&gt; x * y ) points
    sumSquares   = sum $ map ( \(x) -&gt; x * x ) xs
    sumX         = sum xs
    sumY         = sum ys

intercept :: [Point] -&gt; Float
intercept points = (sumY - (s * sumX)) / n
      where
      (xs, ys) = unzip points
      n = fromIntegral $ length points
      s = slope points
      sumX = sum xs
      sumY = sum ys

getCoefficients :: [Point] -&gt; Coefficients
getCoefficients points = (slope points, intercept points)