Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: the role of assignments (Benjamin Edwards) 2. Re: the role of assignments (Daniel Fischer) 3. Enforcing Monad Laws (Jorden M) 4. Re: the role of assignments (Ertugrul Soeylemez) 5. multreplace (prad) 6. Re: multreplace (prad) 7. Re: multreplace (prad) ---------------------------------------------------------------------- Message: 1 Date: Fri, 2 Jul 2010 02:12:23 +0200 From: Benjamin Edwards <edwards.b...@gmail.com> Subject: Re: [Haskell-beginners] the role of assignments To: prad <p...@towardsfreedom.com> Cc: haskellbeginners <beginners@haskell.org> Message-ID: <aanlktik5jsx2rffahabjntfdhiaorc3tryj44u2i3...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" On 2 July 2010 01:59, prad <p...@towardsfreedom.com> wrote: > i'm trying to sort the assignment concept out in my mind. > as i understand it so far, > > <- is for IO > Not really :) <- is for working with monads. It is just syntactic sugar that makes using monads a bit more pleasant > so you can do something like > someIOVar <- readFile filename > this will be of type IO String which is different from String as in > Yes, because underneath what is really going on is the compiler is changing that to readFile fileName >>= \x -> ... let someStrVar = "this is a string" > > to work with an IO String you need to convert it into a String which > seems to automatically happen inside a do structure as in: > > main = do > tmplX <- readFile "zztmpl.htm" > navbx <- readFile "zznavb.htm" > let page = U.replace tmplX "NAVX" navbx > > are there some other ways to make IO String into String? > > Not that you should be using before leaning how monads work ;) But there is unsafePerformIO > also, it seems that assignment is different for the '=' in a program vs > ghci for functions: > > sum x y = x + y (program) > vs > let sum x y = x + y (ghci) > > in ghci you are *inside* the IO monad. Hence the need for let bindings > but not for strings and other things because you must always preface > assignment of values with 'let': > > let a = 4 > > i suppose the let is there for variable assignments because such things > necessitate a change of state and i've read that this is not desirable > in functional programming - you have to work a little harder to do > assignment than languages which just allow > a = 4 > b = 5 > c = 6 > etc > > in haskell, is it preferable to do something like this: > > var <- readFile fn > someFunction var > > or someFunction (readFile fn) > > actually this won't work, unless somefunction has type IO String -> IO a. You would need somefunction =<< readFile fn and that would have type IO a assuming somefunction returns something of type a > -- > In friendship, > prad > > ... with you on your journey > Towards Freedom > http://www.towardsfreedom.com (website) > Information, Inspiration, Imagination - truly a site for soaring I's > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > You definitely need to read up on monads and the do notation. All will become clearer :) Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20100701/9c5b092c/attachment-0001.html ------------------------------ Message: 2 Date: Fri, 2 Jul 2010 02:31:16 +0200 From: Daniel Fischer <daniel.is.fisc...@web.de> Subject: Re: [Haskell-beginners] the role of assignments To: beginners@haskell.org, prad <p...@towardsfreedom.com> Message-ID: <201007020231.16473.daniel.is.fisc...@web.de> Content-Type: text/plain; charset="utf-8" On Friday 02 July 2010 02:12:23, Benjamin Edwards wrote: > But there is unsafePerformIO Which is *really* unsafe and should only be used when one knows that here, in this place it is okay to use. There are places where unsafePerformIO is safe or needed (even if not entirely safe), but those are rare. ------------------------------ Message: 3 Date: Thu, 1 Jul 2010 21:19:30 -0400 From: Jorden M <jrm8...@gmail.com> Subject: [Haskell-beginners] Enforcing Monad Laws To: Biginners Haskell Mailinglist <beginners@haskell.org> Message-ID: <aanlktimwq8ihofnk6bdkwus6sxxgnx9yfqaf5dr1v...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Hello, C++ `Concepts', which almost made it into the C++0x standard, are roughly similar to Haskell type classes. The proposal for concepts in C++ had a feature called axioms, which allow the programmer to specify semantics on the functions the concept contains. This allows for enforcing things such as the Monad Laws, as well as letting the compiler make certain optimizations it may not have been able to make without axiomatic guarantees. Why does Haskell not have a similar functionality in its type classes? Was there not time, desire, etc.? Or are there technical limitations? Thanks, ~j ------------------------------ Message: 4 Date: Fri, 2 Jul 2010 03:27:00 +0200 From: Ertugrul Soeylemez <e...@ertes.de> Subject: [Haskell-beginners] Re: the role of assignments To: beginners@haskell.org Message-ID: <20100702032700.17a1a...@tritium.streitmacht.eu> Content-Type: text/plain; charset=US-ASCII prad <p...@towardsfreedom.com> wrote: > i'm trying to sort the assignment concept out in my mind. as i > understand it so far, > > <- is for IO > so you can do something like > someIOVar <- readFile filename > this will be of type IO String > > which is different from String as in > > let someStrVar = "this is a string" > > to work with an IO String you need to convert it into a String which > seems to automatically happen inside a do structure as in: > > main = do > tmplX <- readFile "zztmpl.htm" > navbx <- readFile "zznavb.htm" > let page = U.replace tmplX "NAVX" navbx > > are there some other ways to make IO String into String? I think, you're building up the wrong intuition here. Let me try to correct it. First of all that neat left-arrow belongs to the 'do' notation. On the right hand side of the arrow you place an IO computation. On the left hand side you put a name for its result. This is not an assignment. You just give the result of the computation a name. If the computation is of type 'IO a', then that result is of type 'a'. Example: do line <- getLine putStr "You entered: " putStrLn line getLine is a computation of type IO String, so 'line' is of type String. There is no conversion involved, because actually there is no "running" involved at all. You just describe what happens, /when/ the 'main' computation is run. As you can see from its type, it's also an IO computation: main :: IO () Note that others have already noted that the 'do' notation as well as the '<-' operator, which belongs to it, is more general than that. This is true, but you can safely ignore that for now, until you have understood the general concept. > also, it seems that assignment is different for the '=' in a program > vs ghci for functions: > > sum x y = x + y (program) > vs > let sum x y = x + y (ghci) > > but not for strings and other things because you must always preface > assignment of values with 'let': > > let a = 4 > > i suppose the let is there for variable assignments because such > things necessitate a change of state and i've read that this is not > desirable in functional programming - you have to work a little harder > to do assignment than languages which just allow > a = 4 > b = 5 > c = 6 > etc > > in haskell, is it preferable to do something like this: > > var <- readFile fn > someFunction var > > or someFunction (readFile fn) In its raw form there are no mutable variables in Haskell. Technically there are, but from a high level language perspective there is no mutation. Whenever an equals sign is involved, this is just name giving, not an 'assignment' in the usual sense. Example: sum x y = x + y This gives 'x + y' a name, namely 'sum x y'. This really is an equation, so whenever you write 'x + y', you can safely replace it by 'sum x y' and vice versa. The same holds for 'let' and 'where', but unlike a top level definition they are scoped. You can view GHCi's command line as one 'do' block, which gets executed as you type it. That's why you need 'let'. You can't write top level definitions in GHCi. And the difference between '<-' and '=' is that '<-' gives the result of an IO computation a name, so you can refer to it. '=' just gives a value a name, where a value can be a function, too. In a 'let' construct, both sides of the equation have the same type. In a '<-' construct the right side has type 'IO a' and the left side has type 'a'. Right: let a = 4 Right: x <- getLine print (read x + 1) Now a few wrong ones with explanations: Wrong: putStrLn getLine Reason: putStrLn has the type 'String -> IO ()', so it expects a String, but getLine is of type 'IO String', not 'String'. Wrong: let x = getLine This doesn't produce an error by itself, but remember that both sides of an equation must have the same type. So x is of type 'IO String' now. Wrong: y <- x^2 The right hand side of '<-' must be of type 'IO a', but 'x^2' is of type (say) 'Integer'. This doesn't fit. I hope, this helps. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/ ------------------------------ Message: 5 Date: Thu, 1 Jul 2010 21:57:20 -0700 From: prad <p...@towardsfreedom.com> Subject: [Haskell-beginners] multreplace To: haskellbeginners <beginners@haskell.org> Message-ID: <20100701215720.65114...@gom> Content-Type: text/plain; charset=US-ASCII so after being inspired by the continual excellent help here, i am trying my hand at writing a multiple replace using Useful.replace Useful.replace origStr pattStr replStr gives for Useful.replace "This original string" "orig" "very orig" "This very original string" what i want to do is something like multRepl origStr [patt1,patt2] [repl1,repl2] so we get from multRepl "This original string" ["orig","ing"] ["very orig","ucture"] "This very original structure" what i have come up with is: multRepl :: String -> [String] -> [String] -> [String] multRepl str ss rs = [U.replace str a b | (a,b) <- (zip ss rs)] this produces in 2 passes: ["This very original string","This very original structure"] to get the final result, i could just last multRepl but i would like to know if this is a good way to do things. i suppose going through the incremental steps is obligatory, but after printing out the result and seeing these steps, it feels like i'm producing throw away stuff - though perhaps that's just an illusion stemming from a similar function php5 has where you get the result without seeing anything behind the scenes. -- In friendship, prad ... with you on your journey Towards Freedom http://www.towardsfreedom.com (website) Information, Inspiration, Imagination - truly a site for soaring I's ------------------------------ Message: 6 Date: Thu, 1 Jul 2010 22:31:28 -0700 From: prad <p...@towardsfreedom.com> Subject: [Haskell-beginners] Re: multreplace To: beginners@haskell.org Message-ID: <20100701223128.74170...@gom> Content-Type: text/plain; charset=US-ASCII On Thu, 1 Jul 2010 21:57:20 -0700 prad <p...@towardsfreedom.com> wrote: > but i would like to know if this is a good way to do things. > ooops i just figured out that this doesn't work as stated. i was looking at replacement in a large file and got mixed up. the above code only replaces things one by one, so doing a last only produces the last change. what i need is something to merge the changes together ... so back to the drawing board! -- In friendship, prad ... with you on your journey Towards Freedom http://www.towardsfreedom.com (website) Information, Inspiration, Imagination - truly a site for soaring I's ------------------------------ Message: 7 Date: Thu, 1 Jul 2010 23:58:56 -0700 From: prad <p...@towardsfreedom.com> Subject: [Haskell-beginners] Re: multreplace To: beginners@haskell.org Message-ID: <20100701235856.6c992...@gom> Content-Type: text/plain; charset=US-ASCII On Thu, 1 Jul 2010 22:31:28 -0700 prad <p...@towardsfreedom.com> wrote: > so back to > the drawing board! > here's what emerged: ====== #!/usr/bin/env runghc module Main where import Useful as U main = do let str = "This is original string" let ss = ["orig","ing"] let rs = ["very orig","ucture"] putStrLn $ head (multRepl str ss rs) --multRepl :: String -> [String] -> [String] -> [String] multRepl [] _ _ = [] multRepl str (s:ss) (r:rs) = do let newStr = U.replace str s r if (length ss) == 0 then return newStr else multRepl newStr ss rs ======= this does produce the correct output: This is very original structure and here are my questions: 1. the type *Main Useful> :t multRepl multRepl :: (Eq t) => [t] -> [[t]] -> [[t]] -> [[t]] but i have it returning newStr which equals U.replace str s r and the type of U.replace is String as shown below *Main Useful> :t Useful.replace Useful.replace :: (Eq a) => [a] -> [a] -> [a] -> [a] so why is it returning [String] when newStr isn't a list of strings? 2. is the way i've done it proper haskellian? it took me quite some time to think this out trying to find my way through the fog of imperative programming. (my apologies for replying to my own posts - as well as my appreciation for your assistance) -- In friendship, prad ... with you on your journey Towards Freedom http://www.towardsfreedom.com (website) Information, Inspiration, Imagination - truly a site for soaring I's ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 25, Issue 4 ****************************************