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.  question >>= [style, efficiency] (Joel Neely)
   2.  (no subject) (Ashley Banks)
   3. Re:  (no subject) (Daniel Seidel)
   4.  haskell IO help (Ashley Banks)
   5.  Re: question >>= [style, efficiency] (Ertugrul Soeylemez)
   6. Re:  question >>= [style, efficiency] (John Dorsey)
   7. Re:  Context reducion Stack overflow (Brent Yorgey)
   8. Re:  parallel program in haskell in 5 steps (Jack Kennedy)


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

Message: 1
Date: Wed, 6 May 2009 08:23:10 -0500
From: Joel Neely <[email protected]>
Subject: [Haskell-beginners] question >>= [style, efficiency]
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

I can think of a variety of ways to put a value at the end of a list
of the same type.

-- cutesy-clever
putAtEnd :: [a] -> a -> [a]
putAtEnd xs x = reverse (x : reverse xs)

-- by hand
atEnd :: [a] -> a -> [a]
atEnd [] z = [z]
atEnd (x:xs) z = x : atEnd xs z

-- obvious brute force
infixr 9 +:
(+:) :: [a] -> a -> [a]
(+:) xs x = xs ++ [x]

I'd appreciate feedback/advice on whether:
1) I've missed any clearly-better alternatives;
2) any of those is obviously better or worse than the others;
3) I should have known where to look for the answer myself (instead of
brute-force Googling).

I've semi-inadvertently mixed two issues above: infix-vs-prefix and
algorithm design. TIA for suggestions on either.

-jn-

-- 
Beauty of style and harmony and grace and good rhythm depend on
simplicity. - Plato


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

Message: 2
Date: Wed, 6 May 2009 13:28:18 +0000
From: Ashley Banks <[email protected]>
Subject: [Haskell-beginners] (no subject)
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"


I havent done much IO at all in haskell, only within the function itself.
However I want to get the input from the interface for the function and
havent done this before.

// Say I have this as my data type and list of films
data Film = Film String String Int [String]
-- List of films

testDatabase :: [Film]
testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry",
"Dave", "Zoe"]) ]

// with functions such as:

becomeFan :: String -> String -> [Film] -> [Film]
becomeFan _ _ [] = []
becomeFan filmName fanName ((Film title director year fans):xs) 
| filmName == title = (Film title director year fanName:fans) : xs
| otherwise = (Film title director year fans) : becomeFan filmName fanName
xs

filmsInGivenYear :: Int -> [Film] -> [String]
filmsInGivenYear filmYear films = [ title | (Film title director year fans)
<- films, year == filmYear]

// I want to ask the user what function they want to use, I have this so far

main :: IO()
do putStr "Hi there! what is your name: "
fanName = getLine
do putStr "1 = Insert film, 2 = Become a Fan, 3 = The number of fans of a
film, 4 = Film released in a year: "
input = getLine
read input :: Int
(if input == 1 then main x = insertFilm [] else if input == 2 then main x =
becomeFan [] else if input == 3 then main x = numberOfFans [])

// I thought using the if statement would work, but now I cant think how to
gather the needed input for the function they have chosen?

thanks

apple
_________________________________________________________________
Beyond Hotmail — see what else you can do with Windows Live.
http://clk.atdmt.com/UKM/go/134665375/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090506/1a624f35/attachment-0001.htm

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

Message: 3
Date: Wed, 06 May 2009 16:57:49 +0200
From: Daniel Seidel <[email protected]>
Subject: Re: [Haskell-beginners] (no subject)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed

Ashley Banks wrote:
> I havent done much IO at all in haskell, only within the function itself.
> However I want to get the input from the interface for the function and
> havent done this before.
> 
> // Say I have this as my data type and list of films
> data Film = Film String String Int [String]
> -- List of films
> 
> testDatabase :: [Film]
> testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry",
> "Dave", "Zoe"]) ]
> 
> // with functions such as:
> 
> becomeFan :: String -> String -> [Film] -> [Film]
> becomeFan _ _ [] = []
> becomeFan filmName fanName ((Film title director year fans):xs) 
> | filmName == title = (Film title director year fanName:fans) : xs
> | otherwise = (Film title director year fans) : becomeFan filmName fanName
> xs
> 
> filmsInGivenYear :: Int -> [Film] -> [String]
> filmsInGivenYear filmYear films = [ title | (Film title director year fans)
> <- films, year == filmYear]
> 
> // I want to ask the user what function they want to use, I have this so far
> 
> main :: IO()
> do putStr "Hi there! what is your name: "
> fanName = getLine
> do putStr "1 = Insert film, 2 = Become a Fan, 3 = The number of fans of a
> film, 4 = Film released in a year: "
> input = getLine
> read input :: Int
> (if input == 1 then main x = insertFilm [] else if input == 2 then main x =
> becomeFan [] else if input == 3 then main x = numberOfFans [])

that part is kind of confusing. My Suggestion: separate functions for 
the "submenus". And a main function calling them:

submenu "2" fanName =
   do putStr "What film you want to become a fan of? "
      filmName <- getLine
      putStr (show  (becomeFan filmName fanName testDatabase))
      return ()
submenu _ _ =
     do putStr "The function is not supported yet."
        return ()


main :: IO()
main =
   do putStr "Hi there! what is your name: "
      fanName <- getLine
      putStr "1 = Insert film, 2 = Become a Fan, 3 = The number of fans 
of a film, 4 = Film released in a year, 5 = Exit: "
      input   <- getLine
      (if input /= "5"
       then
        do submenu input fanName
           main
       else return ())

The example should work, but is not very useful, for just adding 
yourself to the fan list of a movie in testDatabase. You might need to 
pass around the database. It's not like manipulating a global object as 
you might do in imperative languages.

> 
> // I thought using the if statement would work, but now I cant think how to
> gather the needed input for the function they have chosen?
> 
> thanks
> 
> apple
> 
> 
> ------------------------------------------------------------------------
> Get the New Internet Explore 8 Optimised for MSN. Download Now 
> <http://extras.uk.msn.com/internet-explorer-8/?ocid=T010MSN07A0716U>
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 4
Date: Wed, 6 May 2009 15:04:45 +0000
From: Ashley Banks <[email protected]>
Subject: [Haskell-beginners] haskell IO help
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="windows-1252"




From: [email protected]
To: [email protected]
Subject: 
Date: Wed, 6 May 2009 13:28:18 +0000








I havent done much IO at all in haskell, only within the function itself.
However I want to get the input from the interface for the function and
havent done this before.

// Say I have this as my data type and list of films
data Film = Film String String Int [String]
-- List of films

testDatabase :: [Film]
testDatabase = [(Film "Casino Royale" "Martin Campbell" 2006 ["Garry",
"Dave", "Zoe"]) ]

// with functions such as:

becomeFan :: String -> String -> [Film] -> [Film]
becomeFan _ _ [] = []
becomeFan filmName fanName ((Film title director year fans):xs) 
| filmName == title = (Film title director year fanName:fans) : xs
| otherwise = (Film title director year fans) : becomeFan filmName fanName
xs

filmsInGivenYear :: Int -> [Film] -> [String]
filmsInGivenYear filmYear films = [ title | (Film title director year fans)
<- films, year == filmYear]

// I want to ask the user what function they want to use, I have this so far

main :: IO()
do putStr "Hi there! what is your name: "
fanName = getLine
do putStr "1 = Insert film, 2 = Become a Fan, 3 = The number of fans of a
film, 4 = Film released in a year: "
input = getLine
read input :: Int
(if input == 1 then main x = insertFilm [] else if input == 2 then main x =
becomeFan [] else if input == 3 then main x = numberOfFans [])

// I thought using the if statement would work, but now I cant think how to
gather the needed input for the function they have chosen?

thanks

apple
Get the New Internet Explore 8 Optimised for MSN. Download Now
_________________________________________________________________
Share your photos with Windows Live Photos – Free.
http://clk.atdmt.com/UKM/go/134665338/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090506/b8fe824e/attachment-0001.htm

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

Message: 5
Date: Wed, 6 May 2009 17:25:54 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: question >>= [style, efficiency]
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Hello Joel,

any algorithm to append something to a list must have at least O(n)
complexity, where n is the length of the original list.  That said, the
third algorithm appears to be the best to me.  It has exactly O(n) and
is fully lazy, i.e. does not do anything at all until the list is
consumed up to the appendage.


Greets,
Ertugrul.


Joel Neely <[email protected]> wrote:

> I can think of a variety of ways to put a value at the end of a list
> of the same type.
> 
> -- cutesy-clever
> putAtEnd :: [a] -> a -> [a]
> putAtEnd xs x = reverse (x : reverse xs)
> 
> -- by hand
> atEnd :: [a] -> a -> [a]
> atEnd [] z = [z]
> atEnd (x:xs) z = x : atEnd xs z
> 
> -- obvious brute force
> infixr 9 +:
> (+:) :: [a] -> a -> [a]
> (+:) xs x = xs ++ [x]
> 
> I'd appreciate feedback/advice on whether:
> 1) I've missed any clearly-better alternatives;
> 2) any of those is obviously better or worse than the others;
> 3) I should have known where to look for the answer myself (instead of
> brute-force Googling).
> 
> I've semi-inadvertently mixed two issues above: infix-vs-prefix and
> algorithm design. TIA for suggestions on either.
> 
> -jn-
> 



-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/




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

Message: 6
Date: Wed, 6 May 2009 11:30:14 -0400
From: John Dorsey <[email protected]>
Subject: Re: [Haskell-beginners] question >>= [style, efficiency]
To: Joel Neely <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Joel,

> I can think of a variety of ways to put a value at the end of a list
> of the same type.
[...]
> I'd appreciate feedback/advice on whether:
> 1) I've missed any clearly-better alternatives;
> 2) any of those is obviously better or worse than the others;
> 3) I should have known where to look for the answer myself (instead of
> brute-force Googling).

Appending to a normal list is inherently messy and expensive.  If it's
doesn't have to be a normal list, though, things can get better.  Look into
"difference lists" as one example.  Append for cheap while building, then
get a list for linear-time cost when you're finished.

The best data structure depends on what you need it to do.

That said, your "obvious brute force" is the best of the ones you wrote,
specifically because it's "obvious".  It's clear what it does, it reuses
existing functions well, and it's at least roughly as good as the others.

John



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

Message: 7
Date: Wed, 6 May 2009 13:59:02 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Context reducion Stack overflow
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1

On Tue, May 05, 2009 at 03:07:16PM -0300, Marco Túlio Gontijo e Silva wrote:
> > and then your automatic lifting instance would be something like
> > 
> >   instance (F f, M f2 a) => M (f :.: f2) a
> > 
> > where :.: denotes functor composition.
> 
> Ok, I think this is another possibility.  But how could I define :.:?

  newtype (g :.: f) a = O { unO :: g (f a) }

Something like this is already defined in both the TypeCompose and 
category-extras libraries.

-Brent


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

Message: 8
Date: Wed, 6 May 2009 18:33:18 -0700
From: Jack Kennedy <[email protected]>
Subject: Re: [Haskell-beginners] parallel program in haskell in 5
        steps
To: Michael Snoyman <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Does this happen for everyone, or just me?

On Tue, May 5, 2009 at 2:05 PM, Jack Kennedy <[email protected]> wrote:

> I am compiling (Windows by the way) using the line from the tutorial:
> ghc -O2 --make par.hs -threaded
>
> and running with the line
>
> par +RTS -N2
>
> CPU usage for the process flits around a little, but stays in the 45% - 55%
> range.
>
>
> On Tue, May 5, 2009 at 12:51 PM, Michael Snoyman <[email protected]>wrote:
>
>> How are you running the program? You have to explicitly tell the
>> compiler/interpreter to use multiple system threads.
>>
>> Michael
>>
>> On Tue, May 5, 2009 at 10:19 PM, Jack Kennedy <[email protected]> wrote:
>>
>>> In step 4 of Haskell in 5 Steps [
>>> http://haskell.org/haskellwiki/Haskell_in_5_steps],
>>> a parallel program is given. I changed it very slightly so it would run a
>>> long time (see below).
>>>
>>> It compiles and runs but my CPU meter is barely above 50%.  I have a dual
>>> core processor.
>>> What in the world would keep this program from completely saturating the
>>> CPU?
>>>
>>> import Control.Parallel
>>>
>>> main = a `par` b `pseq` print (a + b)
>>>     where
>>>         a = ack 4 10
>>>         b = ack 4 10
>>>
>>> fac 0 = 1
>>> fac n = n * fac (n-1)
>>>
>>> ack 0 n = n+1
>>> ack m 0 = ack (m-1) 1
>>> ack m n = ack (m-1) (ack m (n-1))
>>>
>>> fib 0 = 0
>>> fib 1 = 1
>>> fib n = fib (n-1) + fib (n-2)
>>>
>>>
>>> _______________________________________________
>>> 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/20090506/b0d546ff/attachment.htm

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

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


End of Beginners Digest, Vol 11, Issue 6
****************************************

Reply via email to