Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  defining functions in ghci (Joe Van Dyk)
   2. Re:  defining functions in ghci (Amy de Buitl?ir)
   3. Re:  defining functions in ghci (Jason Dusek)
   4. Re:  defining functions in ghci (Tom Tobin)
   5. Re:  possible incorrect indentation (jean verdier)
   6.  foldr for Nats ([email protected])
   7. Re:  foldr for Nats (Stephen Tetley)
   8. Re:  foldr for Nats (Daniel Fischer)
   9. Re:  ghc: unrecognised flags: -I (Lyndon Maydwell)
  10.  Brainfuck interpreter stack overflow (Edgar Klerks)
  11. Re:  Brainfuck interpreter stack overflow (Stephen Tetley)


----------------------------------------------------------------------

Message: 1
Date: Mon, 25 Jan 2010 15:38:39 -0800
From: Joe Van Dyk <[email protected]>
Subject: [Haskell-beginners] defining functions in ghci
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

This messes me up:

$ ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
double x =Loading package base ... linking ... done.
Prelude> double x = x + x
<interactive>:1:9: parse error on input `='


Why does that happen?

-- 
Joe Van Dyk
http://fixieconsulting.com


------------------------------

Message: 2
Date: Mon, 25 Jan 2010 23:56:43 +0000
From: Amy de Buitl?ir <[email protected]>
Subject: Re: [Haskell-beginners] defining functions in ghci
To: Joe Van Dyk <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

You need to put "let" in front of any sort of definition in GHCI.

let double x = x + x

2010/1/25 Joe Van Dyk <[email protected]>:
> Prelude> double x = x + x
> <interactive>:1:9: parse error on input `='


------------------------------

Message: 3
Date: Mon, 25 Jan 2010 16:00:41 -0800
From: Jason Dusek <[email protected]>
Subject: Re: [Haskell-beginners] defining functions in ghci
To: Amy de Buitl?ir <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

  You can do several at a time like this:


    Prelude> let { x = 21 ; y = 7 }

  Unfortunately, the offside rule is hard to use from the prompt.

--
Jason Dusek


------------------------------

Message: 4
Date: Mon, 25 Jan 2010 18:12:30 -0600
From: Tom Tobin <[email protected]>
Subject: Re: [Haskell-beginners] defining functions in ghci
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Mon, Jan 25, 2010 at 5:38 PM, Joe Van Dyk <[email protected]> wrote:
> This messes me up:
>
> $ ghci
> GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
> double x =Loading package base ... linking ... done.
> Prelude> double x = x + x
> <interactive>:1:9: parse error on input `='
>
> Why does that happen?

As others pointed out, you have to use "let" here; I don't know how
much you've experimented with monads yet (e.g., IO), but the reason is
that you're actually working in a monad in GHCi.


------------------------------

Message: 5
Date: Tue, 26 Jan 2010 09:15:45 +0100
From: jean verdier <[email protected]>
Subject: Re: [Haskell-beginners] possible incorrect indentation
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain

Indent the if like this:

if (c=='y')
  then evaluate e'
  else putStrLn "OK, finished"

I don't know the syntax rule of why you should write it like this. The
point is that your then and else should be more indented than your if.
The following also works:

if (c=='y') then evaluate e'
  else putStrLn "OK, finished"


On Mon, 2010-01-25 at 22:45 +0000, John Moore wrote:
> data Expression = Val Integer
>                 | Add Expression Expression
>                 | Subtract Expression Expression
>                 | Multiply Expression Expression
>                 | Divide Expression Expression
>          deriving Show
> demo1 = (Add(Add(Add(Val 6)(Val 5))(Val 10))(Val 7)) evalStep ::
> Expression ->  Expression
> evalStep (Val x)=  (Val x) evalStep (Add x y)
>    = case x of
>        (Val a) -> case y of
>                     (Val b) -> Val (a+b)
>                     left -> Add x (evalStep y)
>        right -> Add y(evalStep x) 
>  
> evaluate :: Expression -> Expression -- Base case
> evaluate (Val a) = Val a 
> -- Recursive case
> evaluate e = do
>              putStrLn "Evaluating one more step"              e' <-
> return (evalStep e)
>              putStrLn ("Result is "++(show e'))
>              putStrLn "Do another step (y/n)? :"
>              c <- getChar
>              if (c=='y') then
>                 evaluate e'
>             else
>                putStrLn "OK, finished"



------------------------------

Message: 6
Date: Tue, 26 Jan 2010 11:52:45 +0100
From: [email protected]
Subject: [Haskell-beginners] foldr for Nats
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hello,
I have:
data Nat = Z | S Nat deriving (Eq,Ord,Show)
and should write a function that works for Nats like foldr for list where:
instance Enum Nat where
    toEnum i | i < 0 = error "foo"
                    | i == 0 = Z
                    | otherwise = S (toEnum (i-1))
Can someone give me a hint how to do this?

-- 
Nur noch bis 31.01.2010: DSL-Komplettpaket für 16,99 Euro/mtl.!*
http://portal.gmx.net/de/go/dsl02


------------------------------

Message: 7
Date: Tue, 26 Jan 2010 11:05:20 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] foldr for Nats
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi

One hint is that your data type for natural numbers is remarkably like
the data type for lists...

data List a = [] | a : List a

The difference is that List has an element of type 'a' at the non-zero cases.

Best wishes

Stephen


------------------------------

Message: 8
Date: Tue, 26 Jan 2010 12:23:06 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] foldr for Nats
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Dienstag 26 Januar 2010 11:52:45 schrieb [email protected]:
> Hello,
> I have:
> data Nat = Z | S Nat deriving (Eq,Ord,Show)
> and should write a function that works for Nats like foldr for list
> where: instance Enum Nat where
>     toEnum i | i < 0 = error "foo"
>
>                     | i == 0 = Z
>                     | otherwise = S (toEnum (i-1))
>
> Can someone give me a hint how to do this?

I don't understand what "works for Nats like foldr for lists" means.
A very nice explanation of how foldr works is

foldr f z
   (a1  :  (a2  :  (a3  :  (...  :  (an  :  []) ...))))
~> (a1 `f` (a2 `f` (a3 `f` (... `f` (an `f` z) ...))))

So we replace the constructor [] with the base value z, and the constructor 
(:) with the function f.

Maybe it's meant to be similar, foldNat f z should replace the constructor 
Z with the base value and the constructor S with the function.


------------------------------

Message: 9
Date: Tue, 26 Jan 2010 20:35:13 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] ghc: unrecognised flags: -I
To: Chadda? Fouch? <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

I found the issue, and I think it may point to a bug in the cabal
configuration.

I had:

extra-include-dirs:
  -- /opt/local/include

In my ~/.cabal/config, and as far as I could tell it was prefixing the
empty list of include paths with -I. It should probably omit the empty
list entirely. I had a quick look in the cabal configuration source,
but I think I'm out of my depth where that is concerned.

Anyway, issue resolved for me :-)

On Wed, Jan 6, 2010 at 8:22 PM, Lyndon Maydwell <[email protected]> wrote:
> Indeed, it's a capital "I" for Include.
>
> On Wed, Jan 6, 2010 at 8:15 PM, Chaddaï Fouché <[email protected]> 
> wrote:
>> On Wed, Jan 6, 2010 at 3:19 AM, Lyndon Maydwell <[email protected]> wrote:
>>> Hi list.
>>>
>>> I'm trying to cabal install a few different packages and keep getting
>>> this error:
>>>
>>> ghc: unrecognised flags: -I
>>
>> Keep in mind that in many font a capitalized i looks uncomfortably
>> like a small L.
>>
>> --
>> Jedaï
>>
>


------------------------------

Message: 10
Date: Tue, 26 Jan 2010 17:06:25 +0100
From: Edgar Klerks <[email protected]>
Subject: [Haskell-beginners] Brainfuck interpreter stack overflow
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

He Stephen

Thanks for the tip. I will test the performance of Data.Sequence against
Immutable arrays.

 I also have another question about deriving monads. I have modified
Data.List.Zipper, so it supports 2 extra directions (up and down):

data Zipper a = Zip [a] [a] [a] [a]

Now I made a functor out of it:
**
instance Functor Zipper where
        -- fmap :: (a -> b) -> f a -> f b
        f `fmap` (Zip ls rs ds us) = Zip  (f `fmap` ls) (f `fmap` rs) (f
`fmap` ds) (f `fmap` us)

This works. Now I try to make an monad out of it:

instance Monad Zipper where
     return a = Zip [a] [] [] []
     (Zip ls rs ds us) >>= f = Zip (ls >>= f) (rs >>= f) (ds >>= f) (us >>=
f)

This doesn't work like expected, but when using the newtype derive mechanism
it works.

Thanks for all your help so far.

Best Wishes,

Edgar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100126/7fdc60fe/attachment-0001.html

------------------------------

Message: 11
Date: Tue, 26 Jan 2010 19:12:02 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Brainfuck interpreter stack overflow
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Edgar

I'm not sure what a 'lawful' monad would be for your zipper, so I can
sympathize with the author of the package on Hackage who omitted one.
Monads have three laws which instances of the Monad class are obliged
to obey, but the language cannot check that they do (i.e the type
system can't check you obey them):

http://www.haskell.org/haskellwiki/Monad_Laws

A text book on Haskell would cover the monad laws if it covers monads,
for instance in Paul Hudak's book they are on page 254.



For a more general zipper, Chung-chieh Shan has a monad:

http://conway.rutgers.edu/~ccshan/wiki/blog/posts/WalkZip3/


However that monad looks very similar to a resumption monad
(resumption monads are commonly used to model concurrency as they can
pause computations and so yield to other threads). This seems
appropriate to a zipper - I think that Gerard Huet, the inventor of
the zipper, originally used it to move a cursor around a tree
structure within an interactive editor.

A quick web search reveals another one here, I've never looked at this
one before but it looks close to a state monad:

http://www.haskell.org/haskellwiki/Zipper_monad


>From your definition, I suspect if you use GHCs deriving mechanism it
will make a quadruple list monad which will obviously compile and run
but doesn't seem appropriate.

Best wishes

Stephen


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 19, Issue 26
*****************************************

Reply via email to