Home > OS >  GHCI vs compiled function difference
GHCI vs compiled function difference

Time:11-07

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.

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?

CodePudding user response:

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.

  • Related