Haskell Printing a hardcoded value from a function

126 views Asked by At

I'm beginning my first exploration of functional programming with Haskell. I'm struggling a bit with printing a hardcoded value. I created a model representing a car and the gears it can go to. What I want to do is simply print my hardcoded civicNinetyOne when I call printCar. But I keep getting an error when I load the file. I posted the error below, it's an indentation error but from what I've read here at LearnYouAHaskell that call function declaration is correct. Can someone point me to the cause of this issue? Thank you

Error

first_program.hs:10:1: error:
    parse error (possibly incorrect indentation or mismatched brackets)
   |
10 | printCar:: () -> Car    | ^

Code

data Car = Car{
   gears :: [String],
   currentGear :: String,
   brokeGears :: [String],
   shiftStroke:: Strokes
  }  

let civicNinetyOne = Car ["gear1", "gear2", "gear3", "gear4", "gear5"] "gear1" ["gear4"] [("gear1","pull", "gear2"), ("gear2","push", "gear3"), ("gear3","skipShift", "gear5")] 

printCar:: () -> Car
printCar = civicNinetyOne
1

There are 1 answers

2
chepner On BEST ANSWER

printCar takes an argument like any other function; it's argument type is (), which means there is only one value (also spelled ()) that can be used to call it.

civicNinetyOne, on the other hand, is a value with type Car, not a function of type () -> Car, so cannot itself be assigned to printCar.

The correct definition is

printCar :: () -> Car
printCar () = civicNinetyOne

and it would be called as

> printCar ()
Car {gears = ["gear1","gear2","gear3","gear4","gear5"], currentGear = "gear1", brokeGears = ["gear4"], shiftStroke = [("gear1","pull","gear"),("gear2","push","gear3"),("gear3","skipShift","gear5")]}   

For completeness, the let (as pointed out in the comments) is optional in recent versions of GHCi and required in older versions of GHCi, but it is forbidden in a module. (The interactive interpreter behaves somewhat like an implied do block.)