views:

2308

answers:

3

I'm still new and trying to create a list for use in a function and want to keep it as small as possible which happens to be logBase x y. but I'm having trouble getting logBase into something I can use in this list.

[1 .. (logBase x y)]

Any suggestions?

A: 

You probably want some sort of rounding, truncate, floor, or ceiling function. Ints and Floats are different types (as you've seen) and the compiler won't let you mix them. I'll find a reference in a minute.

Novelocrat
+5  A: 

You probably want one of the functions list here. Hoogle and Hayoo! are great tools for this kind of thing since they let you put in the type of the function you want and get back a list of functions. With Haskell's rich type system this can be a very helpful tool, much more so than dynamically typed languages or even statically typed languages like C or Java.

Amuck
+4  A: 

You don't post what type error you get, but I imagine it is something like this:

Prelude> let x = 2
Prelude> let y = 7
Prelude> [1 .. (logBase x y)] 

<interactive>:1:7:
    No instance for (Floating Integer)
      arising from a use of `logBase' at <interactive>:1:7-17
    Possible fix: add an instance declaration for (Floating Integer)
    In the expression: (logBase x y)
    In the expression: [1 .. (logBase x y)]
    In the definition of `it': it = [1 .. (logBase x y)]

The problem is that:

Prelude> :t logBase
logBase :: (Floating a) => a -> a -> a

returns a type in the Floating class, while other variables in your program (1, 'x', 'y') are of integral type.

I presume you want a sequence of Integers?

Prelude> :set -XNoMonomorphismRestriction
Prelude> let x = 2
Prelude> let y = 42
Prelude> [1 .. truncate (logBase x y)] 
[1,2,3,4,5]

Use truncate, celing or floor.

Don Stewart