Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GHCI vs compiled function difference

The goal is to input a float and return a string containing only the absolute value of the digits before the dot.

Basically putting in -14.4 should return "14"

When I type in the sequence of instructions via GHCI I run into no problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Yet when I use it in a compiled function like this:

testFront :: Float -> String
testFront x = tillDot $ show $ abs $ x

tillDot :: String -> String
tillDot x = case x of
  [] -> ""
  x -> takeWhile (/= '.') x

then suddenly I run into the error shown in the the screenshot.

enter image description here

I figured out that I had to put the negative number using ().

The question is: Can I somehow do this within the file? Something that automatically puts my input into ()? Or do I have to write a seperate function that does that?

>Solution :

The question is: Can I somehow do this within the file? Something that automatically puts my input into ()? Or do I have to write a seperate function that does that?

The reason you need to wrap these into parenthesis is because there is a difference between:

f - 14

and:

f (-14)

Indeed, for the former it will subtract 14 from the value of a variable named f. If f is for example 25, it will return 11.

For the latter it will call a function f with -14 as argument.

Except for a minus, no operator has an unary form. The parenthesis are thus necessary to disambiguate between the unary and binary form.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading