On Wed, 9 Feb 2000, Stefan Friedel wrote:

> Hi everybody, I have a problem. I'm new to haskell and
> I have to write a function that takes the following
> list and finds the average by using recursion and
> adding the numbers together. I'm completely STUCK!
> Thank you. 
> 
> > sales :: Int -> Float
> > sales n
> >     | n == 0 = 23.89
> >     | n == 1 = 89.30
> >     | n == 2 = 203.59
> >     | n == 3 = 157.67
> >     | n == 4 = 58.35
> >     | n == 5 = 78.75
> >     | n == 6 = 199.35
> >     | n == 7 = 359.25
> >     | n == 8 = 539.67
> >     | n == 9 = 259.58
> >     | n > 9 && n < 52 = 350.00
> 
That may be because what you present is *not* a list, but a function, a
function with the type Int -> Float as you wrote yourself.

A list is something constructed by the two constructor (functions):

(:) :: a -> [a] -> [a]  -- Note, this is an *infix* operator. 
[]  :: [a]              -- The empty list

To begin with, lets construct the list I guess you want.

We start with [], the empty list. then we add the first number:

259.58 : []       

and the next:

539.67 : 259.58 : []

and so on:

23.89 : 89.30 : 203.59 : 157.67 : 58.35 : 78.75 : 199.35 : 359.25 : 539.67
: 259.58 : []

There is a special syntactic sugar for lists, they can instead be written
as:

[23.89,89.3,203.59,157.67,58.35,78.75,199.35,359.25,539.67,259.58]

But remember, this is only syntactic sugar.

But hold on, there were more numbers, but we do not want to enumerate them
all. Lets use a predefined function (actually defined in Prelude.hs).

replicate 42 350.00

This will create a list with 42 350.00 elements.

[23.89,89.3,203.59,157.67,58.35,78.75,199.35,359.25,539.67,259.58] ++
(replicate 42 350.00)

This will concatenate (++) the two lists.

Now, you have your list.

To continue, I suggest you go to:

http://www.haskell.org/
  The Haskell Bookshelf
    Papers available on the Web
      General Introductions to Haskell


(comp.lang.functional might be a better forum for this kinds of questions)
/Lars L


Reply via email to