[Haskell-cafe] How do you make constant expressions?

Felipe Lessa felipe.lessa at gmail.com
Sun Jul 18 16:26:27 EDT 2010


On Sun, Jul 18, 2010 at 5:19 PM, Eitan Goldshtrom
<thesourceofx at gmail.com> wrote:
> Silly question, but I can't find the answer on the net. I think I'm just
> using the wrong words in my search. I'm looking for a way to create constant
> expressions in Haskell. The C/C++ equivalent of what I'm talking about is
>
> #define NAME VALUE
>
> I want an expression, or really just numbers for what I'm doing, that the
> compiler will put into the program at the designated places, instead of
> storing it in memory like a variable.

Unless you want to pattern match the value, just say

  myconst :: Double -- or anything else
  myconst = 7.14
  {-# INLINE myconst #-}

Voilà!

This doesn't work for pattern matching, however, if you say

  f myconst = ...

then 'myconst' will be the name of that argument that could be
anything, and not just 7.14.

You could, however, use CPP as well

  {-# LANGUAGE CPP #-}
  #define MYCONST 7.14

  f MYCONST = ...

but I'm against using CPP unless you need it.

Cheers,

-- 
Felipe.


More information about the Haskell-Cafe mailing list