Re: [Haskell-cafe] Typing problems with basic arithmetic - help!

2005-09-26 Thread Henning Thielemann

On Sat, 24 Sep 2005, Marcin Tustin wrote:

> For some reason the following code is producing an error message from ghci 
> that the the patterns are non-exhaustive. Does anyone have any idea why that 
> could be, given that the patterns are, at least in my meaning, provably 
> exhaustive?
>
> choosenonuniqueset n (a:r)
> | (length r) >  (n-1) = [ (sort (a:x)) | x <- (choosenonuniqueset (n-1) 
> r)]
> `union`
> [ (sort (a:x)) | x <- (choosenonuniqueset n r)]
> | (length r) == (n-1) = [a:r]
> | (length r) <  (n-1) = []

You need to compare only once. Better write:

choosenonuniqueset n (a:r) =
  case compare (length r) (n-1) of
 GT -> ...
 EQ -> [a:r]
 LT -> []

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typing problems with basic arithmetic - help!

2005-09-26 Thread Henning Thielemann

On Fri, 23 Sep 2005, Marcin Tustin wrote:

>   Thanks for this: All I have to do now is fix the fact that my maths is 
> stupidly screwed!

'div' is the integer division and rounds down. You should either use
Rational (or Double) everywhere or use % which builds a ratio from two
integers.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typing problems with basic arithmetic - help!

2005-09-23 Thread ChrisK
Well...your recursion will fail if (a:r) is matched against the empty
set.  That will trigger your Exception.

So does your code avoid this ?  No, it does not.

cus 2 [1,2,3,4,5] recurses
to cus 1 [2,3,4,5]
   to cus 0 [3,4,5]
  to cus (-1) [4,5]
 to cus (-2) [5]
to cus (-3) [] ---Exception

You need a n<=0 guard case AND an empty list base case

chooseuniqueset _ [] = []
choosenonuniqueset n (a:r)
 | n<=0 = [] -- This needs to be the first guard

Also, you're sorting is poor.  The first case sorts too much and the
second case does no sorting.

Marcin Tustin wrote:
> For some reason the following code is producing an error message from ghci 
> that the the patterns are non-exhaustive. Does anyone have any idea why that 
> could be, given that the patterns are, at least in my meaning, provably 
> exhaustive?
> 
> choosenonuniqueset n (a:r)
> | (length r) >  (n-1) = [ (sort (a:x)) | x <- (choosenonuniqueset (n-1) 
> r)] 
> `union`
> [ (sort (a:x)) | x <- (choosenonuniqueset n r)]
> | (length r) == (n-1) = [a:r]
> | (length r) <  (n-1) = []
> 
> Error message is:
> 
> *Main> :reload
> Compiling Main ( birthday.hs, interpreted )
> Ok, modules loaded: Main.
> *Main> choosenonuniqueset 2 [1..5]
> *** Exception: birthday.hs:(22,0)-(27,29): Non-exhaustive patterns in 
> function choosenonuniqueset
> 
> *Main> choosenonuniqueset 5 [1..5]
> [[1,2,3,4,5]]
> *Main>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typing problems with basic arithmetic - help!

2005-09-23 Thread Marcin Tustin

For some reason the following code is producing an error message from ghci that 
the the patterns are non-exhaustive. Does anyone have any idea why that could 
be, given that the patterns are, at least in my meaning, provably exhaustive?

choosenonuniqueset n (a:r)
| (length r) >  (n-1) = [ (sort (a:x)) | x <- (choosenonuniqueset (n-1) r)] 
`union`
[ (sort (a:x)) | x <- (choosenonuniqueset n r)]
| (length r) == (n-1) = [a:r]
| (length r) <  (n-1) = []

Error message is:

*Main> :reload
Compiling Main ( birthday.hs, interpreted )
Ok, modules loaded: Main.
*Main> choosenonuniqueset 2 [1..5]
*** Exception: birthday.hs:(22,0)-(27,29): Non-exhaustive patterns in function 
choosenonuniqueset

*Main> choosenonuniqueset 5 [1..5]
[[1,2,3,4,5]]
*Main>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typing problems with basic arithmetic - help!

2005-09-23 Thread Marcin Tustin
Thanks for this: All I have to do now is fix the fact that my maths is 
stupidly screwed!

On Fri, Sep 23, 2005 at 02:23:10PM -0400, J. Garrett Morris wrote:
> Haskell doesn't do automatic numeric conversion for you - you need to
> use fromIntegral to go from an integral type (like 2) to a floating
> point type (as you'd need for division) and you can use round or trunc
> to go the other direction.  There's similarly a fromRational function
> to convert from rational numbers to just about anything else you'd
> need.  A couple of applications of those functions - probably mainly
> around your divisions - should fix things up.
> 
>  /g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typing problems with basic arithmetic - help!

2005-09-23 Thread robert dockins
For the version with type signatures, you are trying to divide integers 
using the (/) function.  This function expects values in the class 
Fractional, and Integer isn't a member.  Replace with div, which does 
integer division.


pnorep days n = (numeratorex days n) `div` (denominatorex days n)


Marcin Tustin wrote:

I'm trying to do some basic arithmetic code (in fact it calculates the 
probability of a birthday clash given year length and group size), and GHC and 
Hugs are both not liking the code:

[EMAIL PROTECTED] ~]$ ghci burthday.hs
   ___ ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |  GHC Interactive, version 6.4, for Haskell 98.
/ /_\\/ __  / /___| |  http://www.haskell.org/ghc/
\/\/ /_/\/|_|  Type :? for help.

Loading package base-1.0 ... linking ... done.
Compiling Main ( burthday.hs, interpreted )

burthday.hs:14:37:
No instance for (Fractional Integer)
  arising from use of `/' at burthday.hs:14:37
Probable fix: add an instance declaration for (Fractional Integer)
In the definition of `pnorep':
pnorep days n = (numeratorex days n) / (denominatorex days n)
Failed, modules loaded: none.
Prelude> :reload
Compiling Main ( burthday.hs, interpreted )
Ok, modules loaded: Main.
*Main> pnorep 365 30

:1:0:
Ambiguous type variable `a' in the constraints:
  `Fractional a' arising from use of `pnorep' at :1:0-5
  `Integral a' arising from use of `pnorep' at :1:0-5
Probable fix: add a type signature that fixes these type variable(s)
*Main> 


The first time is with the type annotations, the second without:

import Ratio
import Numeric

numeratorex days n = (numdays days n) - (subtractex days n) -- :: Rational 
numdays days n = days * n

--numdays :: Integer -> Integer -> Integer
subtractex days n = (days * (days - 1)) / 2 --:: Rational
denominatorex days n = (days^n) 
-- denominatorex :: Integer -> Integer -> Integer

pnorep days n = (numeratorex days n) / (denominatorex days n)

Help!

Thanks!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Typing problems with basic arithmetic - help!

2005-09-23 Thread Marcin Tustin
I'm trying to do some basic arithmetic code (in fact it calculates the 
probability of a birthday clash given year length and group size), and GHC and 
Hugs are both not liking the code:

[EMAIL PROTECTED] ~]$ ghci burthday.hs
   ___ ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |  GHC Interactive, version 6.4, for Haskell 98.
/ /_\\/ __  / /___| |  http://www.haskell.org/ghc/
\/\/ /_/\/|_|  Type :? for help.

Loading package base-1.0 ... linking ... done.
Compiling Main ( burthday.hs, interpreted )

burthday.hs:14:37:
No instance for (Fractional Integer)
  arising from use of `/' at burthday.hs:14:37
Probable fix: add an instance declaration for (Fractional Integer)
In the definition of `pnorep':
pnorep days n = (numeratorex days n) / (denominatorex days n)
Failed, modules loaded: none.
Prelude> :reload
Compiling Main ( burthday.hs, interpreted )
Ok, modules loaded: Main.
*Main> pnorep 365 30

:1:0:
Ambiguous type variable `a' in the constraints:
  `Fractional a' arising from use of `pnorep' at :1:0-5
  `Integral a' arising from use of `pnorep' at :1:0-5
Probable fix: add a type signature that fixes these type variable(s)
*Main> 

The first time is with the type annotations, the second without:

import Ratio
import Numeric

numeratorex days n = (numdays days n) - (subtractex days n) -- :: Rational 
numdays days n = days * n
--numdays :: Integer -> Integer -> Integer
subtractex days n = (days * (days - 1)) / 2 --:: Rational
denominatorex days n = (days^n) 
-- denominatorex :: Integer -> Integer -> Integer
pnorep days n = (numeratorex days n) / (denominatorex days n)

Help!

Thanks!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe