Re: [Haskell-cafe] algorithm-for-finding-numerical-permutation-given-lexicographic-index

2013-04-03 Thread Tom Davie
permutationIndex :: Int → [Int] → [Int]
permutationIndex [] = []
permutationIndex xs =
  let len = length xs
  max = fac len
  divisor = max / len
  i = index / divisor
  el = xs !! i
   in permutationIndex (index - divisor * i) (filter (!= el) xs)

Of course, this is not very efficient, because you're using lists, and 
attempting to index into them and measure their lengths.  Perhaps a different 
data structure is in order.

Thanks

Tom Davie

On 3 Apr 2013, at 17:38, Lone Wolf  wrote:

> http://stackoverflow.com/questions/8940470/algorithm-for-finding-numerical-permutation-given-lexicographic-index
> 
> How would you rewrite this into Haskell?  The code snippet is in Scala. 
> 
> /**
> example: index:=15, list:=(1, 2, 3, 4)
> */ 
> def permutationIndex (index: Int, list: List [Int]) : List [Int] = 
>   if (list.isEmpty) list else {
> val len = list.size // len = 4
> val max = fac (len) // max = 24
> val divisor = max / len // divisor = 6
> val i = index / divisor // i = 2
> val el = list (i)
> el :: permutationIndex (index - divisor * i, list.filter (_ != el)) }
> 
> 
> ___
> 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] 9.3 - (2 * 4.5) => 0.3000000000000007

2013-01-16 Thread Tom Davie
Prelude> import Data.Ratio
Prelude Data.Ratio> 93 % 10 - (2 * 9 % 2)
3 % 10

Floating point sucks, avoid it if you can.

Thanks

Tom Davie

On 16 Jan 2013, at 13:25, ivan dragolov  wrote:

> 
> 9.3 - (2 * 4.5) => 0.3007
> 
> I expected 0.3
> 
> ?
> 
> -- 
> Иван Драголов
> dragolov.net
> 
> GSM: 0888 63 19 46
> GSM за SMS: 0878 82 83 93  
> facebook.com/ivan.dragolov
> twitter.com/dragolov
> ___
> 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] Existential Types (I guess)

2010-01-22 Thread Tom Davie
Aside from Neil's point about rank-2 polymorphism, you can of course just
parameterise your NumHolder type...

data Num a => NumHolder a = NumHolder a

instance Show a => Show NumHolder a where
  show (NumHolder x) = show x

instance Functor NumHolder where
  fmap f (NumHolder a) = NumHolder (f a)

It depends what you want to do with your NumHolder though.  What is the
purpose of this type?

Bob

On Fri, Jan 22, 2010 at 11:31 AM, Ozgur Akgun  wrote:

> Dear Cafe,
>
> I can write and use the following,
>
> data IntHolder = IntHolder Integer
>
> instance Show IntHolder where
> show (IntHolder n) = show n
>
> liftInt :: (Integer -> Integer) -> IntHolder -> IntHolder
> liftInt f (IntHolder c) = IntHolder (f c)
>
> But I cannot generalise it to *Num:*
>
> data NumHolder = forall a. Num a => NumHolder a
>
> instance Show NumHolder where
> show (NumHolder n) = show n
>
> liftNum :: (Num a) => (a -> a) -> NumHolder -> NumHolder
> liftNum f (NumHolder c) = NumHolder (f c)
>
> The error message I get is the following:
>
> Couldn't match expected type `a' against inferred type `a1'
>   `a' is a rigid type variable bound by
>   the type signature for `liftNum' at Lifts.hs:54:16
>   `a1' is a rigid type variable bound by
>the constructor `NumHolder' at Lifts.hs:55:11
> In the first argument of `f', namely `c'
> In the first argument of `NumHolder', namely `(f c)'
> In the expression: NumHolder (f c)
>
>
> Regards,
>
>
> --
> Ozgur Akgun
>
> ___
> 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] semantics of type synonym

2009-12-29 Thread Tom Davie
On Tue, Dec 29, 2009 at 2:47 PM, pbrowne  wrote:

> Hi,
> I am studying the underlying semantics behind Haskell and to what degree
> those semantics are actually implemented. I need to clarify what a *type
> synonym* actual means in relation to Haskell's logic (or formal
> semantics). I used the following type synonym:
>
> type Name = String
> getName(n) = n
>
> I checked the types with two tests:
> -- test 1
> :t "ww"
> "ww" :: [Char]
>
> -- test 2
> :t getName("ww")
> getName("ww") :: Name
>
> Obviously I get two different types.
> In the case of the function Haskells type system seems to pick up enough
> information to determine that “ww” is a Name.
> But I am not sure what is happening with the literal "ww" in the first
> test.
>

This isn't really Haskell doing anything, but a particular implementation...
In Haskell a type synonym is *exactly* that – Name is indistinguishable from
String, which in turn is indistinguishable from [Char].  The
compiler/interpretter is free to return any one of them as it choses.

What's happening here is that ghci(?) is returning the one it thinks is most
likely to be familiar to you.

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


Re: [Haskell-cafe] Lazy evaluation/functions

2009-12-27 Thread Tom Davie
Lazy evaluation is an evaluation strategy that gives non-strict semantics.

A lazy function I'm not sure how to define.  It may be lazy language meaning
a function which is non-strict in one of it's arguments.

Bob

On Sun, Dec 27, 2009 at 1:16 PM, michael rice  wrote:

> I've seen the terms "lazy evaluation" and "lazy function." Is this just
> lazy language or are both these terms valid?
>
> Michael
>
>
> ___
> 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] Generating AST using Parsec

2009-12-27 Thread Tom Davie
This isn't quite what you're asking for, but by using the applicative
interface to parsers, you need do little more than spell out what your AST
looks like:

import Control.Applicative
import Control.Applicative.Infix

data Equation = String :=: Expression
data Expression = EApp fun arg | EInt Int | EId String

parseEquation :: Parser Equation
parseEquation = parseIdentifier <^(:=:)^> parseExpression

parseExpression :: Parser Expression
parseExpression =
  (EApp <$> parseExpression <*> parseExpression)
  <|> (EInt <$> parseInt)
  <|> (EId <$> parseIdentifier)

parseIdentifier :: Parser String
parseIdentifier = parseLowercaseChar <^(:)^> parseString

etc

Bob

On Sun, Dec 27, 2009 at 10:18 AM, CK Kashyap  wrote:

> Hi All,
> I recently came across the paper titled "Monadic Parser Combinators" -
> After going through it a few times, I think I am beginning to understand
> monads.
> However, the parser developed in the paper does not generate an AST - I
> feel, I'd grasp the whole thing a lot better if I could go over a sample
> that generates an AST from a simple expression (or even a standard language
> such as C or Java) ... Can someone please point me to a sample that
> generates AST - preferably with the simple parser combinator given in the
> paper.
> Regards,
> Kashyap
>
>
>
>
> ___
> 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] Re: Why?

2009-12-10 Thread Tom Davie
Non-strictness is not necessary for purity, but it sure gives you some nice
properties... Take for example

const x y = x

It would be really nice for this function to have the property "always
results in x no matter what you give it as it's second argument".  But for a
language which is strict, all instances where computing y non-terminates
also non-terminate.

So yes, non-strictness is very much a property you want in a language.

Bob

On Thu, Dec 10, 2009 at 3:30 PM, John D. Earle  wrote:

> My intuition says that laziness and purity are distinct whereas yours says
> that purity is a necessary condition. This is what needs to be reconciled.
>
> I believe that everyone is thinking that lazy evaluation and strict
> evaluation are similar activities whereas they are profoundly different.
> ___
> 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] Haskell job opportunity

2009-12-09 Thread Tom Davie
I have to admit, it's just one criterion too much for me.  I can manage to
satisfy all of them except for willing to work in Manhattan.

Bob

On Tue, Dec 8, 2009 at 5:54 PM, Tom Tobin  wrote:

> On Tue, Dec 8, 2009 at 11:09 AM, siki  wrote:
> > I've posted this before but did not get a whole lot of responses, so here
> it
> > is again:
> [...]
> > You should have at least a bachelor’s degree in computer science from a
> top
> > university
>
> Might I humbly suggest that this is going to severely limit your
> hiring options?  You're looking for the intersection of sets of people
> who:
>
> - Have a BS in computer science (cuts out a fair number of people)
> - Graduated from a "top university" (cuts out a *lot* of people)
> - Is familiar with Java (cuts out some people)
> - Is skilled with Haskell (a fair bet for many on this mailing list, at
> least)
> - Can work in the Manhattan area (cuts out a *lot* of people)
>
> I'm not sure how many people *exist* who meet all these criteria.  ;-)
>  I'd probably start by dropping your "top university" requirement,
> since I don't think it's all that relevant if you find your candidate
> has the skills you're looking for.  You might even find someone who
> fits yet doesn't have a CompSci BS degree; you can phrase it as "a BS
> in computer science or an equivalent strong background in theoretical
> computer science" or somesuch, as appropriate.
> ___
> 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] How to fulfill the "code-reuse" destiny of OOP?

2009-10-31 Thread Tom Davie
On 10/31/09, Magicloud Magiclouds  wrote:
> After all, I never think OO as an oppsite way to all other things. The
> idea is so general that if you say I cannot use it in Haskell at all,
> that would make me feel weird. The only difference between languages
> is, some are easy to be in OO style, some are not.

Wow, someone drank the cool aid!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Applicative but not Monad

2009-10-31 Thread Tom Davie
On 10/31/09, Heinrich Apfelmus  wrote:
> The only possible monad instance would be
>
>return x = Const mempty
>fmap f (Const b) = Const b
>join (Const b)   = Const b

Your join doesn't seem to have the right type... Unless I'm missing something.

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


Re: [Haskell-cafe] Applicative but not Monad

2009-10-30 Thread Tom Davie
On Fri, Oct 30, 2009 at 5:59 PM, Luke Palmer  wrote:

> On Fri, Oct 30, 2009 at 10:39 AM, Tom Davie  wrote:
> > Of note, there is a sensible monad instance for zip lists which I *think*
> > agrees with the Applicative one, I don't know why they're not monads:
> > instance Monad (ZipList a) where
> >   return = Ziplist . return
> >   join (ZipList []) = ZipList []
> >   join (ZipList (a:as)) = zlHead a `zlCons` join (map zlTail as)
>
> IIRC, that doesn't satisfy the associativity law, particularly when
> you are joining a list of lists of different lengths.  2 minutes of
> experimenting failed to find me the counterexample though.
>

Cool, thanks Luke, that explains why this is available in Stream, but not in
ZipList too.

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


Re: [Haskell-cafe] Applicative but not Monad

2009-10-30 Thread Tom Davie
Of note, there is a sensible monad instance for zip lists which I *think*
agrees with the Applicative one, I don't know why they're not monads:

instance Monad (ZipList a) where
  return = Ziplist . return
  join (ZipList []) = ZipList []
  join (ZipList (a:as)) = zlHead a `zlCons` join (map zlTail as)

I'll provide an alternative though, Const a is an applicative, but not a
monad.

Bob

On Fri, Oct 30, 2009 at 5:25 PM, Eugene Kirpichov wrote:

> Yes. ZipList.
> http://en.wikibooks.org/wiki/Haskell/Applicative_Functors
>
> 2009/10/30 Yusaku Hashimoto :
> > Hello cafe,
> > Do you know any data-type which is Applicative but not Monad?
> >
> > Cheers,
> > -~nwn
> > ___
> > Haskell-Cafe mailing list
> > Haskell-Cafe@haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
>
>
>
> --
> Eugene Kirpichov
> Web IR developer, market.yandex.ru
> ___
> 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] GHC and OS X (10.4) [SOLVED]

2004-09-28 Thread Tom Davie
Thanks to a lot of help from Gregory Wright, it is now possible to
install ghc 6.2.1 on Tiger - it takes quite a bit of fiddling before
it will work, but there is a way.  The package available at
Haskell.org will not work, however the darwin ports version will given
that gcc 3.1 is installed on the system - this however is not the
easiest thing in the world to make work.  You can however get it
installed by downloading XCode 1.5 from apple developer connection,
going into the packages directory on that disk image, moving the
gcc3.1 package onto your hard disk and editing it's contents slightly.
Do show package contents on the installer, and go into
Contents/Resources, then edit VolumeCheck with your favourite text
editor, edit line 28 to say
 if (CheckVersion("$SYSTEM_VERS", "10.5", "ProductVersion", ">=")) {
The package should now install happily on tiger, you can then use port
install ghc to install ghc and it's dependancies.

Hope this helps anyone digging in the archives for a solution.

Tom Davie

On Thu, 2 Sep 2004 22:59:59 -0400, Gregory Wright <[EMAIL PROTECTED]> wrote:
> 
> Hi Tom,
> 
> You might try building ghc using darwinports
> (darwinports.opendarwin.org).
> It works under both Jaguar and Panther. I maintain the port, and would
> be
> interested in your experience on 10.4-beta.  (The darwinports version
> doesn't
> use /Library/Frameworks, instead it keeps everything in a unix-style
> lib/
> hierarchy.)
> 
> The downside is that it takes a few hours to build.
> 
> Best Wishes,
> Greg Wright
> 
> 
> 
> 
> On Sep 2, 2004, at 5:06 PM, Tom Davie wrote:
> 
> > Hi,
> >   I've been attempting to use GHC on a beta copy of Mac OS X 10.4,
> > I've been attmepting to use the panther version of the install
> > package, but have hit a problem with tinkering with it - I get the
> > following error when I attempt to run ghc:
> > Verenia:~/Documents/Development/XBridgeAI tatd100$ ghc
> > dyld: Library not found:
> > HaskellSupport.framework/Versions/A/HaskellSupport
> >   Referenced from: /usr/local/lib/ghc-6.2.1/ghc-6.2.1
> >   Reason: file not found
> > Trace/BPT trap
> >
> > The framework is present in /Library/Frameworks, and
> > /Library/Frameworks is in dyld's framework search path.
> >
> > Any Mac/Haskell gurus able to help I would much appreciate it.
> >
> > Thanks
> >
> > Tom Davie
> > ___
> > Haskell-Cafe mailing list
> > [EMAIL PROTECTED]
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
> 
>
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] GHC and OS X (10.4)

2004-09-02 Thread Tom Davie
Hi,
  I've been attempting to use GHC on a beta copy of Mac OS X 10.4,
I've been attmepting to use the panther version of the install
package, but have hit a problem with tinkering with it - I get the
following error when I attempt to run ghc:
Verenia:~/Documents/Development/XBridgeAI tatd100$ ghc
dyld: Library not found: HaskellSupport.framework/Versions/A/HaskellSupport
  Referenced from: /usr/local/lib/ghc-6.2.1/ghc-6.2.1
  Reason: file not found
Trace/BPT trap

The framework is present in /Library/Frameworks, and
/Library/Frameworks is in dyld's framework search path.

Any Mac/Haskell gurus able to help I would much appreciate it.

Thanks

Tom Davie
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe