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. Re:  foldr for Nats ([email protected])
   2. Re:  Translating imperative algorithms to Haskell
      (Marc Dominik Migge)
   3. Re:  consing an element to a list inside a file ([email protected])
   4. Re:  Translating imperative algorithms to Haskell
      (Mikhail Novikov)
   5. Re:  Translating imperative algorithms to Haskell (Felipe Lessa)
   6. Re:  Re: ffi woes (Stephen Tetley)
   7. Re:  Translating imperative algorithms to Haskell
      (Mikhail Novikov)


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

Message: 1
Date: Fri, 19 Feb 2010 21:24:15 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] foldr for Nats
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

nobody here, who can tell me how it can work?


-------- Original-Nachricht --------
> Datum: Thu, 04 Feb 2010 21:58:45 +0100
> Von: [email protected]
> An: Stephen Tetley <[email protected]>, Daniel Fischer 
> <[email protected]>
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] foldr for Nats

> @Stephan thanks for the code so far.
> @Daniel it should look like foldrNat a b S(S(S(S(Z)))) gives a(a(a(a(b))))
> 
> 
> The thing is the definition have to be
> -------- Original-Nachricht --------
> > Datum: Mon, 1 Feb 2010 21:56:14 +0000
> > Von: Stephen Tetley <[email protected]>
> > An: [email protected]
> > CC: [email protected]
> > Betreff: Re: [Haskell-beginners] foldr for Nats
> 
> > Hello
> > 
> > I'm suspecting this isn't homework as you've waited a week so would
> > presumably have missed a deadline.
> > 
> > As Daniel Fischer wrote, one view of folds is that they replace the
> > constructors of a data type, code follows...
> > 
> > 
> > data Nat = Z | S Nat   deriving (Eq,Ord,Show)
> > 
> > 
> > -- Look at the type of foldr...
> > 
> > -- *GHCi> :t foldr
> > -- foldr :: (a -> b -> b) -> b -> [a] -> b
> > 
> > -- It has 2 'constructor replacements':
> > -- (a -> b -> b) & b
> > 
> > -- Replacing Z is easy, we can get some code to compile
> > -- by avoiding the hard bit with a wildcard pattern "_"...
> > 
> > foldrNat1 :: unknown -> b -> Nat -> b
> > foldrNat1 _ b Z = b
> > 
> > -- What to do about the constructor (S ..) takes a bit more
> > -- thought or at least some experimenting. I'll do the later...
> > 
> > -- One thing to try, is to simply translate foldr with as few
> > -- changes as possible:
> > 
> > -- foldr            :: (a -> b -> b) -> b -> [a] -> b
> > -- foldr _ z []     =  z
> > -- foldr f z (x:xs) =  f x (foldr f z xs)
> > 
> > -- Unfortunately this leads to a problem:
> > 
> > foldrNat2 :: (Nat -> b -> b) -> b -> Nat -> b
> > foldrNat2 f b Z     = b     -- Z case is the same as before
> > foldrNat2 f b (S n) = f undefined (foldrNat2 f b n) -- Arggh! undefined
> > 
> > -- undefined is useful for prototyping, but its a real
> > -- problem for running code!
> > 
> > -- Actually I had another problem as well...
> > --
> > -- The difference between Nat and [a] is that List 'carries' some data
> > -- therefore (Nat -> b -> b) on Nat is not equivalent to (a -> b -> b)
> > -- on [a].
> > 
> > -- So rather than change the type signature first, get rid of the
> > -- undefined and see what happens
> > 
> > foldrNat3 f b Z     = b
> > foldrNat3 f b (S n) = f (foldrNat3 f b n)
> > 
> > -- *GHCi> :t foldrNat3
> > -- > (t -> t) -> t -> Nat -> t
> > 
> > -- GHCi likes to call type variables t, but the signature is equal to
> > 
> > -- foldrNat3 :: (b -> b) -> b -> Nat -> b
> > 
> > 
> > -- This looks promising - it typechecks!
> > -- So try a test:
> > 
> > fromNat :: Nat -> Int
> > fromNat n = foldrNat3 (+1) 0 n
> > 
> > demo1 = fromNat (S (S (S Z)))   -- 3 ??
> > 
> > -- By experimenting we seem to have a good answer,
> > -- other people might prefer a more rigorous proof though.
> 
> -- 
> NEU: Mit GMX DSL über 1000,- ¿ sparen!
> http://portal.gmx.net/de/go/dsl02
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


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

Message: 2
Date: Fri, 19 Feb 2010 22:11:11 +0100
From: Marc Dominik Migge <[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
        Haskell
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hi,

I recommend you read John Hughe's article "Why Functional Programming
Matters"[1]. As one of his examples he implements alpha-beta-pruning. I'm
not really a Haskell expert, but I think the main difference will be in the
kind of data structures you use to implement the algorithms. Many people
have recommended Chris Okasaki's PhD thesis [2] to me for an in-depth
treatment of the topic.

[1]: 
http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html<http://www.cs.chalmers.se/%7Erjmh/Papers/whyfp.html>
[2]: 
http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf<http://www.cs.cmu.edu/%7Erwh/theses/okasaki.pdf>

Best regards,
Marc


On Fri, Feb 19, 2010 at 9:07 PM, Mikhail Novikov <[email protected]>wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello!
>
> Four days ago I decided to try to learn Haskell by participating in Google
> AI Challenge (http://csclub.uwaterloo.ca/contest/). I went with LYAH and
> Real-World Haskell and I had no problems with functional core of Haskell, as
> I have lispy background. The problems begun when I tried to move my code to
> use different kind of decision algorithms, like negascout or flood fill.
>
> I have found a ready made solution for negascout (
> http://hackage.haskell.org/packages/archive/game-tree/0.1.0.0/doc/html/src/Data-Tree-Game_tree-Negascout.html),
> but I was quite shocked by it's complexity, especially considering the
> relative easy of imperative algorithm.(
> http://en.wikipedia.org/wiki/Negascout). IMHO it is just TOO extremely
> complex and hard to read :/
>
> In flood fill situation is similar - I need to track all the colored
> squares among 4 lines of recursion and I couldn't find a reasonable way to
> do it without going to infinite recursion or having lots of duplicates.
>
> So basically, I wonder if there is some generic way to transfer imperative
> algorithms with local state to functional style without making them overly
> complex.
>
> Thanks a lot, Mikhail
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkt+73cACgkQPHyh4sfuKrl7NgCeKHrIdJbHrSse6z7eRrmAi6ck
> yYoAoIRJZLq6RVUcCG2Zf5Xc5tRW5YBk
> =oUX7
> -----END PGP SIGNATURE-----
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100219/953fc6d0/attachment-0001.html

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

Message: 3
Date: Fri, 19 Feb 2010 22:13:00 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] consing an element to a list inside a
        file
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I checked it by using :t
readCal :: IO Calendar and readCal2 :: Calendar -> IO Calendar
so the function to call (readCal) is what it should be.

thanks!



-------- Original-Nachricht --------
> Datum: Fri, 19 Feb 2010 01:54:05 +0100
> Von: [email protected]
> An: Daniel Fischer <[email protected]>
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] consing an element to a list inside a file

> ok, looks much easier and works :)
> 
> But what are now the correct signatures for the function? Normally the
> "one and only" function should be: readCal :: IO Calendar
> My function are: readCal = readCal2 datelist
> and: readCal2 datelist = do... which returns the Calendar (the list of
> dates)
> 
> 
> -------- Original-Nachricht --------
> > Datum: Fri, 19 Feb 2010 00:41:56 +0100
> > Von: Daniel Fischer <[email protected]>
> > An: [email protected]
> > CC: [email protected]
> > Betreff: Re: [Haskell-beginners] consing an element to a list inside a
> file
> 
> > Am Freitag 19 Februar 2010 00:25:18 schrieb [email protected]:
> > > ok, now I did it by just calling from the original function by to a
> new
> > > one with an empty list at the beginning.
> > >
> > > How can I add the date at the end of the list? By creating a new list
> > > from the new date and using concat or is there a simpler way?
> > >
> > 
> > Better stick it to the front by (:) and reverse the list just before 
> > printing.
> 
> -- 
> NEU: Mit GMX DSL über 1000,- ¿ sparen!
> http://portal.gmx.net/de/go/dsl02
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

-- 
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01


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

Message: 4
Date: Fri, 19 Feb 2010 23:16:22 +0200
From: Mikhail Novikov <[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello!

Thanks, I'll take a look. I was going to buy Okasaki's book for a very long 
time, probably I should finally do it :)

Best regards, Mikhail

On 02/19/2010 11:07 PM, Marc Dominik Migge wrote:
> Hi,
> 
> I recommend you read John Hughe's article "Why Functional Programming
> Matters"[1]. As one of his examples he implements alpha-beta-pruning. I'm
> not really a Haskell expert, but I think the main difference will be in the
> kind of data structures you use to implement the algorithms. Many people
> have recommended Chris Okasaki's PhD thesis [2] to me for an in-depth
> treatment of the topic.
> 
> [1]: http://www.cs.chalmers.se/~rjmh/Papers/whyfp.html
> [2]: http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf
> 
> Best regards,
> Marc
> 
> 
> On Fri, Feb 19, 2010 at 9:07 PM, Mikhail Novikov <[email protected]>wrote:
> 
> Hello!
> 
> Four days ago I decided to try to learn Haskell by participating in Google
> AI Challenge (http://csclub.uwaterloo.ca/contest/). I went with LYAH and
> Real-World Haskell and I had no problems with functional core of Haskell, as
> I have lispy background. The problems begun when I tried to move my code to
> use different kind of decision algorithms, like negascout or flood fill.
> 
> I have found a ready made solution for negascout (
> http://hackage.haskell.org/packages/archive/game-tree/0.1.0.0/doc/html/src/Data-Tree-Game_tree-Negascout.html),
> but I was quite shocked by it's complexity, especially considering the
> relative easy of imperative algorithm.(
> http://en.wikipedia.org/wiki/Negascout). IMHO it is just TOO extremely
> complex and hard to read :/
> 
> In flood fill situation is similar - I need to track all the colored
> squares among 4 lines of recursion and I couldn't find a reasonable way to
> do it without going to infinite recursion or having lots of duplicates.
> 
> So basically, I wonder if there is some generic way to transfer imperative
> algorithms with local state to functional style without making them overly
> complex.
> 
> Thanks a lot, Mikhail
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
>>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkt+/6YACgkQPHyh4sfuKrkrdgCfaH4/z1RrQX33dtoO1QXWQshl
LBUAn18H9/MdMjbGzeTJZ9+bxVYs5tfJ
=9ZhZ
-----END PGP SIGNATURE-----


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

Message: 5
Date: Fri, 19 Feb 2010 19:18:59 -0200
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Feb 19, 2010 at 10:07:19PM +0200, Mikhail Novikov wrote:
> So basically, I wonder if there is some generic way to transfer
> imperative algorithms with local state to functional style
> without making them overly complex.

It may be worthwhile to not translate at all.  ST monad works as
a charm :).  Haskell is one of the best imperative languages.

--
Felipe.


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

Message: 6
Date: Fri, 19 Feb 2010 21:28:36 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Re: ffi woes
To: "Jose A. Ortega Ruiz" <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi

I think I've found a version 30 from Debian's archive - the essid
string is fixed length, rather than sized by essid_len (even though
essid_len is now in the struct). Is this the same as the version you
have?


/* Structure for storing all wireless information for each device
 * This is a cut down version of the one above, containing only
 * the things *truly* needed to configure a card.
 * Don't add other junk, I'll remove it... */
typedef struct wireless_config
{
  char          name[IFNAMSIZ + 1];     /* Wireless/protocol name */
  int           has_nwid;
  iwparam       nwid;                   /* Network ID */
  int           has_freq;
  double        freq;                   /* Frequency/channel */
  int           freq_flags;
  int           has_key;
  unsigned char key[IW_ENCODING_TOKEN_MAX];     /* Encoding key used */
  int           key_size;               /* Number of bytes */
  int           key_flags;              /* Various flags */
  int           has_essid;
  int           essid_on;
  char          essid[IW_ESSID_MAX_SIZE + 2];   /* ESSID (extended network) */
  int           essid_len;
  int           has_mode;
  int           mode;                   /* Operation mode */
} wireless_config;

Best wishes

Stephen


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

Message: 7
Date: Sat, 20 Feb 2010 00:10:59 +0200
From: Mikhail Novikov <[email protected]>
Subject: Re: [Haskell-beginners] Translating imperative algorithms to
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello!

Maybe I should look into ST monad for this. :) I am still interested if there 
is a way to make this kind of functions in purely functional way.

Best regards, Mikhail

On 02/19/2010 11:18 PM, Felipe Lessa wrote:
> On Fri, Feb 19, 2010 at 10:07:19PM +0200, Mikhail Novikov wrote:
>> So basically, I wonder if there is some generic way to transfer
>> imperative algorithms with local state to functional style
>> without making them overly complex.
> 
> It may be worthwhile to not translate at all.  ST monad works as
> a charm :).  Haskell is one of the best imperative languages.
> 
> --
> Felipe.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkt/DHMACgkQPHyh4sfuKrlfBQCfdnN8qaHmP1B2YPqVqifwTAAU
HLgAnih0r81xjOOmd7SYDWPjOWZ75YaK
=5pBO
-----END PGP SIGNATURE-----


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

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


End of Beginners Digest, Vol 20, Issue 30
*****************************************

Reply via email to