Re: [Haskell-cafe] question regarding the $ apply operator

2011-07-18 Thread Ting Lei

Thanks, Brandon,

That's a very clear explanation. I remembered that it's low, but forgot that 
it's this low.
Maybe it's because of my mis-understanding that in C, infix operators have 
lower precedence.

Just curious, the following is not allowed in Haskell either for the same 
reason. 

applySkip i f ls = (take i) ls ++ $ f $ drop i ls

I read somewhere that people a couple of hundreds of years ago can manage to 
express things using ($)-like notation without any parenthesis at all.
Is it possible to define an operator to achieve this which works with infix 
operators? If so, then ($) and parentheses are then really equivalent.

Best,

Ting

Date: Tue, 19 Jul 2011 02:22:47 -0400
Subject: Re: [Haskell-cafe] question regarding the $ apply operator
From: allber...@gmail.com
To: tin...@hotmail.com
CC: haskell-cafe@haskell.org

On Tue, Jul 19, 2011 at 02:16, Ting Lei  wrote:





I have a naive question regarding the basic use of the $ operator, and I am 
confused why certain times it doesn't seem to work.
e.g.
The following works:

applySkip i f ls = (take i) ls ++ f (drop i ls)


But the following doesn't:

applySkip i f ls = (take i) ls ++ f $ drop i ls

($) has lower precedence than almost every other operator, so your "doesn't 
work" translates to

> applySkip i f ls = ((take i) ls ++ f) $ (drop i ls)
-- 
brandon s allbery  allber...@gmail.com

wandering unix systems administrator (available) (412) 475-9364 vm/sms


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


Re: [Haskell-cafe] question regarding the $ apply operator

2011-07-18 Thread Brandon Allbery
On Tue, Jul 19, 2011 at 02:16, Ting Lei  wrote:

> I have a naive question regarding the basic use of the $ operator, and I am
> confused why certain times it doesn't seem to work.
> e.g.
> The following works:
>
> applySkip i f ls = (take i) ls ++ f (drop i ls)
>
> But the following doesn't:
>
> applySkip i f ls = (take i) ls ++ f $ drop i ls
>

($) has lower precedence than almost every other operator, so your "doesn't
work" translates to

> applySkip i f ls = ((take i) ls ++ f) $ (drop i ls)

-- 
brandon s allbery  allber...@gmail.com
wandering unix systems administrator (available) (412) 475-9364 vm/sms
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] question regarding the $ apply operator

2011-07-18 Thread Arlen Cuss
> applySkip i f ls = (take i) ls ++ f (drop i ls)
> 
> But the following doesn't:
> 
> applySkip i f ls = (take i) ls ++ f $ drop i ls

The issue is with operator precedence. The above is equivalent to:

> applySkip i f ls = ((take i) ls ++ f) (drop i ls)

(++) binds more strongly than ($).

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


[Haskell-cafe] question regarding the $ apply operator

2011-07-18 Thread Ting Lei

Hi

I have a naive question regarding the basic use of the $ operator, and I am 
confused why certain times it doesn't seem to work.
e.g.
The following works:

applySkip i f ls = (take i) ls ++ f (drop i ls)

But the following doesn't:

applySkip i f ls = (take i) ls ++ f $ drop i ls

which gives me an error:

The first argument of ($) takes one argument,
but its type `[a0]' has none
In the expression: (take i) ls ++ f $ drop i ls
In an equation for `applySkip':
applySkip i f ls = (take i) ls ++ f $ drop i ls


FYI, I am using GHC 7.0.3 for this.
Thanks in advance for comments.


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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Donn Cave
Quoth "Richard O'Keefe" ,
[ ... re Werner Kuhn "An Image-Schematic Account of Spatial Categories" ... ]

> class BUILDING building where
>   
> class BUILDING house => HOUSE house where
>   
> any instance of HOUSE *will* have in its interface everything that
> any instance of BUILDING will.

But ... for those who might be overly influenced by a superficial
similarity to OOP here ... the idea that the above relationship
makes House a subclass of Building isn't really a natural way to
look at it.  It's true that a House instance will also "have
in its interface everything that any instance of Building will",
but only because the programmer is constrained to do that.  To
consider the whole outline: 

  class Building building where
... specify the behavior of buildings here, if any
  class Building house => House house where
... specify additional behavior of houses here, if any
  data Shed = Shed "shed"
  instance Building Shed where
... define Building functions for Shed
  instance House Shed where
... define House functions for Shed

The Building => House constraint doesn't add anything, it just
checks that each instance of House also is an instance of Building.
If you omit this constraint, everything will work the same - even
the constraint will still be there, implicitly, albeit not at the
same level.  To me, it's somewhat misleading to use words like
"inheritance" and "subclass" for the relationship between Building
and House, unless there's some feature of the system I missed.

Donn

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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Richard O'Keefe

On 19/07/2011, at 11:09 AM, Patrick Browne wrote:
> data Emperor = Emperor
> data Robin = Robin
> 
> class Bird a where
>   fly :: a -> a -> a
>   walk :: a -> a -> a
> 
> instance Bird Robin where
>  fly x y = y
>  walk x y = x
> 
> instance Bird Emperor where
> -- No fly method
>   walk x y = y

Note that "no 'fly' method is visibly presented" is
not at all the same thing as "no 'fly' method EXISTS".

From the report:
   "The default class method for v sub i is used if
no binding for it is given in a particular
instance declaration (see Section 4.3.2)."
so the absence of a method in 'instance Bird Emperor'
doesn't mean that it doesn't _have_ one, just that it
inherits the default class method.



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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Richard O'Keefe

On 19/07/2011, at 5:09 AM, Patrick Browne wrote:

> On 18/07/2011 13:52, Ketil Malde wrote:
>> I'm not sure the question makes sense, if "fly" is a method of class
>> Bird, then it can't also be a member of class Penguin.
> 
> I am actually doing a language comparison and I was checking out a paper
> that said:
> "Type classes allow for partial inheritance, so that penguins can be
> birds without flying behavior."

That would be "An Image-Schematic Account of Spatial Categories"
by Werner Kuhn, I take it?  His e-mail address is at the top of
the paper, so why not ask him?  I cannot find anything in the
Haskell 2010 report that says anything about partial inheritance.

In his example,
class BUILDING building where 
class BUILDING house => HOUSE house where 
any instance of HOUSE *will* have in its interface everything that
any instance of BUILDING will.  As has already been pointed out,
the implementation of something may be (or call) 'undefined',
but this is rather boring; even Smalltalk-80 let you plug in
"self shouldNotImplement" as the definition of a method.

If you want a language where a subclass can remove features from the
interface(s) of its superclass(es), you probably want Eiffel, not Haskell.

> But as pointed out by Jerzy my question is silly because can penguins
> can fly:
> On 17/07/2011 11:14, Jerzy Karczmarczuk wrote:
>>> PS. Penguins DO fly.
>>> http://www.telegraph.co.uk/news/worldnews/1583517/Flying-penguins-found-by-BBC-programme.html

And what a fine April Fool's joke that was.  But even in that joke,
flying was something "no other penguins can do".


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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Maciej Marcin Piechotka
On Tue, 2011-07-19 at 01:13 +0200, Yves Parès wrote:
> Oh, I got it: You want to have:
> 
> class Bird b where
> 
> class Penguin p where
> 
> instance (Penguin b) => Bird b where
>fly = -- fly method for penguins
> 

I haven't followed the thread carefully but why does the bird have to be
a penguin?

--

As a side note - I agree with Christopher Done that the answer is that
you shouldn't require for bird to fly.

Regards


signature.asc
Description: This is a digitally signed message part
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Felipe Almeida Lessa
On Mon, Jul 18, 2011 at 3:14 PM, Jerzy Karczmarczuk
 wrote:
> That's why I suggested how you might do that: for some datatypes, say the
> Emperors, you specify some special flying method (e.g. dummy or bombing), or
> you don't specify it at all. And the Emperors won't fly.

Instead of flying, they'll throw exceptions around. =/

-- 
Felipe.

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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Yves Parès
Oh, I got it: You want to have:

class Bird b where

class Penguin p where

instance (Penguin b) => Bird b where
   fly = -- fly method for penguins

2011/7/19 Patrick Browne 

> On 18/07/2011 19:14, Jerzy Karczmarczuk wrote:
> > That's why I suggested how you might do that: for some datatypes, say
> > the Emperors, you specify some special flying method (e.g. dummy or
> > bombing), or you don't specify it at all. And the Emperors won't fly.
>
> -- Here is my attempt
> data Emperor = Emperor
> data Robin = Robin
>
> class Bird a where
>   fly :: a -> a -> a
>   walk :: a -> a -> a
>
> instance Bird Robin where
>  fly x y = y
>  walk x y = x
>
> instance Bird Emperor where
> -- No fly method
>   walk x y = y
>
> class (Bird a) => Penguin a where
> -- I do not think that I can override the walk method in the sub-class
> -- must be done in instance of Penguin
>
> instance Penguin Emperor where
> -- How can I override the walk method in the instance Penguin?
> -- walk x y = x
>
>
> Regards,
> Pat
>
> This message has been scanned for content and viruses by the DIT
> Information Services E-Mail Scanning Service, and is believed to be clean.
> http://www.dit.ie
>
> ___
> 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] partial inheritance

2011-07-18 Thread Patrick Browne
On 18/07/2011 19:14, Jerzy Karczmarczuk wrote:
> That's why I suggested how you might do that: for some datatypes, say
> the Emperors, you specify some special flying method (e.g. dummy or
> bombing), or you don't specify it at all. And the Emperors won't fly.

-- Here is my attempt
data Emperor = Emperor
data Robin = Robin

class Bird a where
   fly :: a -> a -> a
   walk :: a -> a -> a

instance Bird Robin where
  fly x y = y
  walk x y = x

instance Bird Emperor where
-- No fly method
   walk x y = y

class (Bird a) => Penguin a where
-- I do not think that I can override the walk method in the sub-class
-- must be done in instance of Penguin

instance Penguin Emperor where
-- How can I override the walk method in the instance Penguin?
-- walk x y = x


Regards,
Pat

This message has been scanned for content and viruses by the DIT Information 
Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

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


Re: [Haskell-cafe] ANN: cabal-dev 0.8

2011-07-18 Thread Bryan O'Sullivan
On Mon, Jul 18, 2011 at 1:10 PM, Rogan Creswick  wrote:

> We're happy to announce the release of cabal-dev 0.8! This version is
> available on hackage now, and contains many bug fixes and improvements,
> as outlined in the full release notes below.
>

Wonderful! This is absolutely one of those indispensable tools for build
automation and sanity preservation. I use cabal-dev to manage the builds for
all of my Haskell projects, under many different configurations:
https://jenkins.serpentine.com/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Failure to install Network.CGI

2011-07-18 Thread Jason Dagit
On Mon, Jul 18, 2011 at 11:45 AM, Albert Y. C. Lai  wrote:
> On 11-07-17 12:29 AM, william murphy wrote:
>>
>> : cannot satisfy -package-id
>> network-2.3.0.2-24fdc6b92867c7236e81708f93cae7d0
>
> Look at the output of "ghc -v" and be very horrified.
>
> The problem is not lacking packages or being outdated. The problem is
> possessing too many packages and upgrading too much. The solution is nuking.

Once you've reinstalled, or "nuked", you can use cabal-dev to never
run into this problem again:
http://www.reddit.com/r/haskell/comments/f3ykj/psa_use_cabaldev_to_solve_dependency_problems/

Jason

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


[Haskell-cafe] ANN: cabal-dev 0.8

2011-07-18 Thread Rogan Creswick
We're happy to announce the release of cabal-dev 0.8! This version is
available on hackage now, and contains many bug fixes and improvements,
as outlined in the full release notes below.

--Rogan


cabal-dev release 0.8
==

The 0.8 release of `cabal-dev` fixes many of the bugs that have been
reported by `cabal-dev` users. It also provides more flexible, easier
configuration and improves tool support for working within the
`cabal-dev` sandbox (ghc-pkg suport and better ghci support). Upgrading
is recommended for all users.

Please report any bugs or submit enhancement requests to
.

Much of the work on this release (and previous releases) of
`cabal-dev` was funded by Galois, Inc. 

About cabal-dev
==

`cabal-dev` is a Haskell build tool that augments Cabal and
cabal-install to make it convenient to work with a sandboxed set of
packages. It limits the effect of your build on unrelated parts of
your system. Among other things, `cabal-dev` satisfies the following
needs:

* Avoid conflicts between libraries by working only with the set of
  packages that are needed for the current project.

* Enable depending on a patched dependency without that patched
  dependency accidentally being used by another project's build.

* Enable the simultaneous development of multiple interdependent
  packages without the development versions leaking to other project
  build environments.

Significant effort has been made to ensure that `cabal-dev` works across
platforms (Mac, Windows, and Linux) and with the versions of GHC that
are most commonly in use (6.10-7.0).

What's new
==

Details can be found on github at
.

Bugs fixed:

* `cabal-dev` no longer passes `--verbose` to cabal-install commands that
  do not accept it. 

* `cabal-dev` now recognizes and passes through short flags to
  cabal-install. In particular, this bug frequently came up when
  attempting to pass a flag (`-f`) to `cabal-dev install`.
  

* `cabal-dev` now supports all cabal-install commands, including
  `test`. 

* We also made changes that should address inconsistencies with the
  remote repository (Hackage) cache location, but those issues remain
  open until we have confirmation from end users (you!) that these
  changes actually fix those issues.
   and
  

Enhancements and new features:

* `cabal-dev ghci` now invokes ghci with the exact same set of flags
  that would be used in a cabal-install build (using a trick copied
  from Leksah). This means that it will now have the appropriate
  package visibility, language settings, and source directories set
  when it is invoked.

  Note that in order for `cabal-dev ghci` to work, all dependencies
  must already be installed in the sandbox, and the package must
  already be configured.

* `cabal-dev buildopts` will print the set of build options that would
  be passed to GHC when invoking `cabal build`. You can then use these
  flags for other tools that invoke GHC (e.g. the doctest package
  ).

* `cabal-dev ghc-pkg` will invoke `ghc-pkg` with the appropriate flags
  to examine or alter the package database inside the `cabal-dev`
  sandbox.

* `cabal-dev` now has more flexible support for cabal-install
  configuration:

  * By default `cabal-dev` will now use any settings from your
cabal-install configuration file, except those that must be
overridden in order to sandbox the build. This makes it more
convenient to e.g. use an alternate remote repository with
`cabal-dev`.

  * `cabal-dev` now supports merging of additional cabal-install
configuration settings, to make it easy to work with multiple
cabal-install configurations (`--extra-config-file=...`).

* `cabal-dev` now has a flag to pass explicit options to cabal-install
  in case there are any conflicts between `cabal-dev` flags
  (`--cabal-install-arg`).

* `cabal-dev` now has a flag to specify a particular cabal-install
  executable to use (`--with-cabal-install=...`).



smime.p7s
Description: S/MIME Cryptographic Signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Failure to install Network.CGI

2011-07-18 Thread Albert Y. C. Lai

On 11-07-17 12:29 AM, william murphy wrote:

: cannot satisfy -package-id
network-2.3.0.2-24fdc6b92867c7236e81708f93cae7d0


Look at the output of "ghc -v" and be very horrified.

The problem is not lacking packages or being outdated. The problem is 
possessing too many packages and upgrading too much. The solution is nuking.


See my http://www.vex.net/~trebla/haskell/sicp.xhtml for complete 
information and more ways to be horrified.


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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Jerzy Karczmarczuk

Patrick Browne :

> I was checking out a paper that said: "Type classes allow for partial 
inheritance, so that penguins can be birds without flying behavior."
> ... as pointed out by Jerzy my question is silly because can penguins 
can fly ...


No, the question is not silly because of that crazy Adelie tribe. The 
question is doubtful, because the Haskell type classes is not an 
object-oriented system of classes, subclasses, etc.  When you declare 
Rationals, you don't "inherit" the multiplication for them as Numbers. 
This "inheritance" you wish may manifest itself when you specify the 
*instances*.


That's why I suggested how you might do that: for some datatypes, say 
the Emperors, you specify some special flying method (e.g. dummy or 
bombing), or you don't specify it at all. And the Emperors won't fly.


Jerzy Karczmarczuk


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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Patrick Browne
On 18/07/2011 13:52, Ketil Malde wrote:
> I'm not sure the question makes sense, if "fly" is a method of class
> Bird, then it can't also be a member of class Penguin.

I am actually doing a language comparison and I was checking out a paper
that said:
"Type classes allow for partial inheritance, so that penguins can be
birds without flying behavior."
My question is concerned with the language constructs to do this rather
than the semantic validity or desirability of such construct.

But as pointed out by Jerzy my question is silly because can penguins
can fly:
On 17/07/2011 11:14, Jerzy Karczmarczuk wrote:
>> PS. Penguins DO fly.
>> http://www.telegraph.co.uk/news/worldnews/1583517/Flying-penguins-found-by-BBC-programme.html
>> 
> 



This message has been scanned for content and viruses by the DIT Information 
Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

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


[Haskell-cafe] How to use Yesod OAuth

2011-07-18 Thread Haisheng Wu
Hello,
  I'm trying to use Yesod oAuth plugin to a SNS site which is very similar
to Twitter.

  I add oAuth into the authPlugin and I'm able to see the auth URL at login
page.
  I click the URL and it forwards me to a page ask me input that SNS site
account.
  I fill in my account then the browser navigate to the authorize page
display a authorised code (8 digits).

  Then, how I suppose to continue? (I think this is the 2nd phase of the
oauth process)

  I expect there will be a text box in my App where I can fill in the
authorised code.
  Or even the oAuth plugin automatically get the authorised code and
continue to the next oauth phase.

  Appreciate your help!

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


Re: [Haskell-cafe] Trying to make a calculator

2011-07-18 Thread Johannes Waldmann
> But now my assignment is a lot tougher than I thought so I'll need some 
> guidelines.

I'm pretty sure your course instructor has already given you 
the exact guidelines that are required, in her lecture. 

There's hundreds of ways to "make a calculator",
and it really depends on what the teacher wants to emphasize,
in the context of the lecture.


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


Re: [Haskell-cafe] Trying to make a calculator

2011-07-18 Thread Vo Minh Thu
2011/7/18 Emanuil Boyanov :
> Hi guys, I'm new to Haskell, I learn it for couple of months now and we've
> learned only simple functions like map, zip etc. But now my assignment is a
> lot tougher than I thought so I'll need some guidelines. I've googled my
> assignment and I saw a person with the exact assignment so I am gonna copy
> the task then I'm gonna show you what I've done so far.
> Here it is: Make calculator function.
>  The function argument is a list of strings and also form such  list, as
> each string of the argument made definite action.
> - If the string has the form of an arithmetic expression - calculate
> this expression. The string result becomes part of the list-    result.
> If the expression contains a variable which is not assigned value, the
> result is displayed "undefined".
>  - If the string has the form     Name =  value calculated from the last
> expression is assigned to the variable with the corresponding name in the
> list, and in the result list    is formed a string with type     name = ...
>     where the site stands blank corresponding value.     If there is not a
> calculated expression to be assigned to form a string "no value".
> - If the string is non-blank, but there is a species different from the
> above two case, form the string "error".
> - If the string is empty, incl. when it contains only spaces, in the result
> there is  not form a string.     Expressions consist of integers without
> sign variables,
> operations
>    + (Addition), - (subtraction), * (multiplication) and / (divide)
>  In the beginning, end or between the elements of each row can have  spaces
> - they are irrelevant to its correctness.
> Example:
> the list-argument     ["3 +7 / 2", "2 + x", "= s", "2 * s +4", "", "2 + +4 /
> 5"]     function should provide a result-list     ["6", "undefined", "s =
> 6", "16",
> "error"].
> =
> So what I've done so far is:
> calculator [] = []
> calculator [" "] = []
> calculator [c] = [c]
> calculator (c:" ":cs) = c:calculator cs
> calculator (" ":cs) = calculator cs
> calculator (c:" ":" ":cs) = calculator (c:" ":cs)
> calculator (c:cs)
> | (head c `elem` ['a'..'z']) && (head(tail c) `elem` ['+','-','*','/']) &&
> (last(take (length c-2)c) `elem` ['1','2','3','4','5','6','7','8','9'])  =
>  calculator cs
>         | otherwise = "error":calculator cs
> I try at first to use pattern matching for the blank strings in the first
> list, so that I can get rid off them in the second list. Am I even on the
> right way here?
> I found the "elem" function and I'm trying to check if the first char of the
> string is a letter or a number, then if the second char is one of the
> operations and so on. Problem is that I have no idea how can I check if the
> variable is defined already or how to save a variable and then give that
> variable the new value - is that even possible? :] And another thing `elem`
> [1..9] isn't working - how can I make a single char of a string to hold an
> integer..
> Could you give me some guidelines or at least tell me if I'm on the right
> way here so I can move forward and improve my program.
> Thanks a lot!

Hello,

You should work by smaller steps.

For instance, you can begin by abstracting the syntax. At the very
beginning, you get some strings (a list of statements) and you
transform them in something much simpler to use. Here, there is also
an intermediary step to make things easier.

Let's say you have "  1 + 2 ". You can make things simpler by working
on ["1", "+", "2"]. Here, the problem with blancs is already dealt
with. You just have a list of tokens. Then, to make it simple later on
to differentiate between a number and an operator (and a variable),
you can define your own representation, for instance:

data Token = TNum Int | TOp Op | ...
data Op = Add | Sub | ...

Now the list  ["1", "+", "2"] becomes [TNum 1, TOp Add, TNum 2]. Now
you can use pattern matching on the Token and easily know what you
have.

For your variable assignment problem, you have to provide an
environment, i.e. a mapping between variable name and their value. If
you use a recursive function to evaluate your little language, you can
provide that environement as a supplementary argument.

Try to proceed that way, one step at a time, and come back if you have
any problem.

Cheers,
Thu

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


[Haskell-cafe] Trying to make a calculator

2011-07-18 Thread Emanuil Boyanov
Hi guys, I'm new to Haskell, I learn it for couple of months now and we've
learned only simple functions like map, zip etc. But now my assignment is a
lot tougher than I thought so I'll need some guidelines. I've googled my
assignment and I saw a person with the exact assignment so I am gonna copy
the task then I'm gonna show you what I've done so far.
Here it is: Make calculator function.
 The function argument is a list of strings and also form such  list, as
each string of the argument made definite action.
- If the string has the form of an arithmetic expression - calculate this
 expression. The string result becomes part of the list-result.
If the expression contains a variable which is not assigned value, the
result is displayed "undefined".
 - If the string has the form Name =  value calculated from the last
expression is assigned to the variable with the corresponding name in the
list, and in the result listis formed a string with type name = ...
where the site stands blank corresponding value. If there is not a
calculated expression to be assigned to form a string "no value".
- If the string is non-blank, but there is a species different from the
above two case, form the string "error".
- If the string is empty, incl. when it contains only spaces, in the result
there is  not form a string. Expressions consist of integers without
sign variables,
operations
   + (Addition), - (subtraction), * (multiplication) and / (divide)
 In the beginning, end or between the elements of each row can have  spaces
- they are irrelevant to its correctness.

Example:
the list-argument ["3 +7 / 2", "2 + x", "= s", "2 * s +4", "", "2 + +4 /
5"] function should provide a result-list ["6", "undefined", "s =
6", "16",
"error"].

=

So what I've done so far is:

calculator [] = []
calculator [" "] = []
calculator [c] = [c]
calculator (c:" ":cs) = c:calculator cs
calculator (" ":cs) = calculator cs
calculator (c:" ":" ":cs) = calculator (c:" ":cs)
calculator (c:cs)
| (head c `elem` ['a'..'z']) && (head(tail c) `elem` ['+','-','*','/']) &&
(last(take (length c-2)c) `elem` ['1','2','3','4','5','6','7','8','9'])  =
 calculator cs
| otherwise = "error":calculator cs

I try at first to use pattern matching for the blank strings in the first
list, so that I can get rid off them in the second list. Am I even on the
right way here?
I found the "elem" function and I'm trying to check if the first char of the
string is a letter or a number, then if the second char is one of the
operations and so on. Problem is that I have no idea how can I check if the
variable is defined already or how to save a variable and then give that
variable the new value - is that even possible? :] And another thing `elem`
[1..9] isn't working - how can I make a single char of a string to hold an
integer..

Could you give me some guidelines or at least tell me if I'm on the right
way here so I can move forward and improve my program.
Thanks a lot!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] I confused myself with generics, and now I confused ghc too

2011-07-18 Thread Ari Rahikkala
I want to implement generic comparison on top of syb-with-class, and
based on looking at syb, it seems I could base it on something like
gzipWithQ from syb's Data.Generics.Twins. However, there's no
Data.Generics.SYB.WithClass.Twins. So I set out to see if I can write
one myself (well, one with just enough stuff to support gzipWithQ
anyway) Here's what I've got so far: http://hpaste.org/49168

The problem is that GHCi seems to be somewhat confused, or at least
confusing, about the type of gzipWithQ here. When I ask with :t, it
reports the type as:

gzipWithQ
  :: (Data ctx a2, Data ctx a3, Data ctx a) =>
 Proxy ctx
 -> (forall a0.
 Data ctx a0 =>
 a0 -> forall a1. Data ctx a1 => a1 -> r)
 -> a2
 -> a3
 -> [r]

If I actually replace gzipWithQ's annotated type with this, GHCi will
reject the program. And in any case with GenericQ being defined as
"type GenericQ ctx r = Data ctx a => a -> r", isn't this closer to
gzipWithQ's type with the type synonyms expanded?

gzipWithQ :: forall ctx a r.
 Data ctx a =>
 Proxy ctx
  -> (Data ctx a0 => a0 -> (Data ctx a1 => a1 -> r))
  -> (Data ctx a2 => a2 -> (Data ctx a3 => a3 -> [r]))

(GHCi accepts this as gzipWithQ's type so I assume it's the same one.
I assume those class constraints after the => come with implicit
foralls? Because it seems to mean the same thing if I put foralls in
there)

I have no idea how to even start figuring out how to make gzipWithQ
and its type work. I only got this far by copying code from
Data.Generics.Twins and passing down the proxy parameter and/or
context type parameter (is it even possible for this approach to
work?). What I can tell is that GHCi will still change the type like
that even if gzipWithQ = undefined, so I'll have to change the type
and not just the body of the function. Is this the kind of thing that
all those newtypes in Data.Generics.Twins are for? Should I read up on
type theory?

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


Re: [Haskell-cafe] partial inheritance

2011-07-18 Thread Ketil Malde
Patrick Browne  writes:

> Is it possible to model partial inheritance using Haskell type classes?
> For example, if the class Bird has a flying method can we represent
> Penguins as a sub-class of Bird without a flying method?

I'm not sure the question makes sense, if "fly" is a method of class
Bird, then it can't also be a member of class Penguin.

You can of course make instances of Bird without implementing the "fly"
method (whether they are also instances of Penguin or not), or by
implementing it as "undefined".

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants

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


Re: [Haskell-cafe] ANNOUNCE: docidx-1.0.0

2011-07-18 Thread Andy Gimblett
With apologies, I now announce docidx-1.0.1 which fixes a problem kindly 
pointed out by Jack Henahan.  If you tried to install docidx using "cabal 
install" over the weekend and failed, please try again now.

The docidx-1.0.0.tar.gz uploaded to hackage on Friday was missing most of its 
source code (everything except its Main module in fact), because I hadn't added 
an "other-modules" field to docidx.cabal.  I hadn't actually tried to build 
from that tarball, so I didn't spot this: I had assumed that if I could "cabal 
install" in my source directory, then the tarball (produced by cabal sdist) 
would work too.  (And yes, I've tested this version before announcing it! :-) )

Apologies again, and many thanks to Jack for pointing this out.

-Andy


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