Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Haskell help

Name: Anonymous 2010-05-05 18:52

here's a program I wrote...can anyone tell me what I'm doing wrong? inb4 use <insert fave language>

roots :: (Float, Float, Float) -> (Float, Float)
roots (a,b,c) = (x1, x2) where
  x1 = e + (sqrt d) / (2 * a)
  x2 = e - (sqrt d) / (2 * a)
  d = (b * b) - (4 * a * c)
  e = - b / (2 * a)
main :: IO Float -> IO
main = do{
           print "a = "; a <- readLn;
           print "b = "; b <- readLn;
           print "c = "; c <- readLn;
           ans <- roots (a,b,c); print ans;
         }

Name: Anonymous 2010-05-05 18:59

what I'm doing wrong?
Using Haskell

Name: Anonymous 2010-05-05 19:07

>>2
Thanks, dipshit.

Name: Anonymous 2010-05-05 19:07

>>3
THANK MY ANUS

Name: Anonymous 2010-05-05 19:27

>>1
I'm not installing a Haskell interpreter to check this out, but I have a funny feeling that the order in the where clause matters and that the x1 and x2 should come after d and e. Of course, it would be a lot easier if you would give us the output. Also, High school maths would tell you that the output of the quadratic formula can be two complex numbers, and I doubt Haskell's Float covers that (but I am open to being proved wrong)

Name: 5 2010-05-05 19:28

Also, here's how you do it in a real language
(define (solve-quadratic a b c)
           (let ((determinant (- (expt b 2)
                                 (* 4 a c)))
                 (two-a (* a 2)))
             (values (/ (+ (- b) (sqrt determinant)) two-a)
                     (/ (- (- b) (sqrt determinant)) two-a))))

Name: Anonymous 2010-05-05 19:51

>>1
Easy solution, I hope.
x1 = e + (sqrt d) / (2 * a)
x2 = e - (sqrt d) / (2 * a)
d = (b * b) - (4 * a * c)
e = - b / (2 * a)
x1 = e + (sqrt d) / (2 * a)
x2 = e - (sqrt d) / (2 * a)

You should only have to make sure your math is correct.

Name: Anonymous 2010-05-05 20:04

solveCubic s1 s2 s3 = -- solve the polynomial  t^3 - s1 t^2 + s2 t - s3 = 0
 let w = exp(2*sqrt(-1)*pi/3) :: Complex Double
     k = 2*s1 - s1*s2 + 15*s3
     h = s1^6 - 9*s1^4*s2 + 27*s1^2*s2^2 - 27*s2^3
     yc = (1/2)*(k + sqrt(k^2 - 4*h))
     zc = (1/2)*(k - sqrt(k^2 - 4*h))
     y' = yc ** (1/3)
     z' = zc ** (1/3) in
    ( (1/3)*(s1 + y' + z')
    , (1/3)*(s1 + w*y' + w^2*z')
    , (1/3)*(s1 + w^2*y' + w*z')
    )

Name: Anonymous 2010-05-05 20:05

>>8
typical haskell programmer.

Name: Anonymous 2010-05-05 20:07

>>1,7,8
What the hell is with Haskellers and not using real variable names.

Name: Anonymous 2010-05-05 20:45

main :: IO Float -> IO
"IO" isn't a value you can return. You need to perform some type of I/O. In this case, it should be unit I/O: IO (). This is the first step towards getting a meaningful error message.

Secondly, your main function doesn't take any parameters. The entire type should be "main :: IO ()".

Then you have to figure out how to perform I/O properly. Remember that the type signature of roots is not IO (Float,Float). This means that in a Do block, you shouldn't use "<-" to bind its value. Instead, just use let ans = roots (a,b,c) -- voilà, it works!


>>7
GTFO

Name: Anonymous 2010-05-05 21:26

>>10
I'm not a Haskel programmer.  I'm just pointing out that not even Haskel is so powerful that it can break causality.

Name: Anonymous 2010-05-05 21:46

>>12
DON'T TELL ME WHAT I CAN'T DO!

Name: Anonymous 2010-05-05 21:59

>>1
Also, why are you using braces? Indentation looks much nicer.
And you should rather have roots :: Float -> Float -> Float -> (Float, Float), it's customary for functions to be curried.

Name: Anonymous 2010-05-05 22:48

>>14
oh dear god this ->sucks

Name: Anonymous 2010-05-05 22:55

Something more idiomatic might be like this:
main :: IO ()
main = do a <- ask "a = "
          b <- ask "b = "
          c <- ask "c = "
          let ans = roots' a b c
          print ans

ask :: (Show a, Read b) => a -> IO b
ask s = do print s
           readLn

roots' :: (Floating a) => a -> a -> a -> (a, a)
roots' a b c = (x1, x2) where
    x1 = left + right
    x2 = left - right
    left = -b / (2 * a)
    right = sqrt ((b^2) - 4 * a * c) / (2 * a)

Name: >>16 2010-05-05 23:36

...or better yet:
roots :: (Floating a) => a -> a -> a -> (a, a)
roots a b c = (-b) `plusOrMinus` sqrt (b^2 - 4*a*c) `allOver` (2*a)
    where  plusOrMinus x y = (x+y, x-y)
           allOver (x, y) z = (x/z, y/z)

Name: Anonymous 2010-05-05 23:44

Why are people torturing themselves with learning this son of a bitch language? It's not even hip.

Name: Anonymous 2010-05-05 23:53

>>18
butthurt scheme weenie

Name: Anonymous 2010-05-06 0:46

>>19
butthurt
Back to wherever you came from, please.

Name: Anonymous 2010-05-06 1:45

>>19
You got the language wrong, also >>20.

Name: Anonymous 2010-05-06 2:18

roots a b c = both (/ (2 * a)) $ (-b +) &&& (-b -) $ sqrt (b * b - 4 * a * c)
    where both = join (***)


>>18-21
back to /pr/, please.

Name: Anonymous 2010-05-06 3:05

>>22
I think I'll try to learn how to use arrows now.

Name: Anonymous 2010-05-06 6:14

>>23
Upvoted

Name: Anonymous 2010-05-06 7:09

inb4 haskellers are allowed to use primes in front of their bindans

Name: Anonymous 2010-05-06 7:28

>>22
>>23
GTFO arrow fags

Name: Anonymous 2011-09-28 0:54

>>26
[quote]GTFO arrow fags[/quote]
NO U

Name: Anonymous 2013-01-18 23:23

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List