Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have Show display the name of a function?

Tags:

haskell

As a simple exercise to get me acquainted with Haskell, after idling around on Youtube and stumbling into the American Countdown game show, I wanted to make a solver for the Numbers game.

You get 6 numbers and need to combine them with (+) (-) (*) (/) in order to get a given result.

enter image description here

What I've got so far is the very brain-dead,

let operands = [75, 2, 6, 3, 8, 7] :: [Double]
let goal = 623 :: Double
let operations = [(+), (-), (*), (/)]

show (head [(a, x, b, y, c, z, d, t, e) |
      a <- operands,
      b <- filter (\ q -> q /= a) operands,
      c <- filter (\ q -> q /= a && q /= b) operands,
      d <- filter (\ q -> q /= a && q /= b && q /= c) operands,
      e <- filter (\ q -> q /= a && q /= b && q /= c && q /= d) operands,
      x <- operations,
      y <- operations,
      z <- operations,
      t <- operations,
      t (z (y (x a b) c) d) e == goal])

...but obviously Show doesn't know what to do with functions.

No instance for (Show (Double -> Double -> Double))
  arising from a use of `show'
Possible fix:
  add an instance declaration for (Show (Double -> Double -> Double))

How can I work around this? Do I need to mess with type and data constructors to make my own functions that can print or is there some easier way around it?

like image 984
badp Avatar asked Nov 28 '22 09:11

badp


1 Answers

Another option:

data Operation = Add | Subtract | Multiply | Divide deriving (Show)

apply :: Operation -> Double -> Double -> Double
apply Add      = (+)
apply Subtract = (-)
apply Multiply = (*)
apply Divide   = (/)
like image 155
sdcvvc Avatar answered Dec 09 '22 13:12

sdcvvc