
Hello to everyone! Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice. Now my problem is connected with the "non-update" object feature. I can't write "variable" instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-( Let's assume that I've got to write the application that should works as follows: 1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout. How should I think "properly" in Haskell to get described action? cheers Waldemar

Hi Waldmar,
Now my problem is connected with the "non-update" object feature. I can't write "variable" instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-(
main = do username <- read_info_from_user do_whatever_is_needed print username Try not to think of this too hard. Once you start learning Haskell you'll suddenly realise a lot of the problems just don't happen if you think the Haskell way. Thanks Neil

To think "properly" in haskell when you need side effects you should look at
monads
and how they are used in I/O.
I'm no expert but most applications i've seen start like this
main = do x <-getArgs
mapM_ somefunction(x)
do is a construct that lets you have a bunch of IO statements in succession
which in turn
starts to look like imperative programming. So this is how you can access
the rest of the world
from Haskell.
(someone please correct me if i'm wrong)
-Dan
On 12/9/06, Waldemar Biernacki
Hello to everyone!
Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice.
Now my problem is connected with the "non-update" object feature. I can't write "variable" instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-(
Let's assume that I've got to write the application that should works as follows:
1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout.
How should I think "properly" in Haskell to get described action?
cheers Waldemar _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

wb:
Hello to everyone!
Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice.
Great! Welcome to Haskell.
Now my problem is connected with the "non-update" object feature. I can't write "variable" instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-(
It's not a problem :)
Let's assume that I've got to write the application that should works as follows:
1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout.
How should I think "properly" in Haskell to get described action?
Forget about updating values in place: its not necessary, and makes your programs more flexible, and simpler to understand. Here's a short program to read a user's name, and then apply various functions to that name, printing the result. import System.IO import Data.Char import System.Console.Readline main = do name <- do m <- readline "What is your name> " return (maybe "Nobody" id m) print (reverse name) print (length name) let n = map ord name print n print (sum n) print name We can run this: $ runhaskell A.hs What is your name> lambdaman "namadbmal" 9 [108,97,109,98,100,97,109,97,110] 925 "lambdaman" Just dive in (start on http://haskell.org). -- Don

Thank you all of you, your remarks and commants are very useful for me. I'm starting to studying Haskell! My first comparison to (my preferred) Perl is to show file on the screen: The Code in Haskell: ===================== import System main=do [ f ] <- getArgs s <- readFile f putStr s ===================== is more readable but a longer than that in Perl: ===================== open f, "<$ARGV[0]"; while ( <f> ) { print } ===================== Is it possible to make the Haskel code shorter? How to join the three line in one? Is it possible? What about the check if there exists referred file? In Perl it could look like: ===================== if ( open f, "<$ARGV[0]" ) { while ( <f> ) { print } } ===================== and is still shorter than that in Haskell. I have to admit that I think that as shorter source code as better. Therefore the question to those who know Perl: is the source code of a client-server application shorter in Haskel to compare to Perl one? How much shorter (more or less)? Is that source code really easier to debug (in OO Perl it is not really easy :-( cheers Waldemar

On Sun, Dec 10, 2006 at 10:46:37AM +0100, Waldemar Biernacki wrote:
Thank you all of you,
your remarks and commants are very useful for me. I'm starting to studying Haskell!
My first comparison to (my preferred) Perl is to show file on the screen:
The Code in Haskell:
===================== import System main=do [ f ] <- getArgs s <- readFile f putStr s =====================
is more readable but a longer than that in Perl:
===================== open f, "<$ARGV[0]"; while ( <f> ) { print } =====================
import System;main=getArgs>>=readFile.head>>=putStr * Do notation has been reduced to monadic combinators * Layout rule is not used

wb:
Thank you all of you,
your remarks and commants are very useful for me. I'm starting to studying Haskell!
My first comparison to (my preferred) Perl is to show file on the screen:
The Code in Haskell:
===================== import System main=do [ f ] <- getArgs s <- readFile f putStr s =====================
is more readable but a longer than that in Perl:
===================== open f, "<$ARGV[0]"; while ( <f> ) { print } =====================
Is it possible to make the Haskel code shorter?
import System main = getArgs >>= readFile . head >>= putStr If you prefer to read from standard input: main = interact id is enough. A variety of other unix tools are here: http://haskell.org/haskellwiki/Simple_unix_tools
How to join the three line in one? Is it possible? What about the check if there exists referred file? In Perl it could look like: ===================== if ( open f, "<$ARGV[0]" ) { while ( <f> ) { print } } ===================== and is still shorter than that in Haskell.
I have to admit that I think that as shorter source code as better. Therefore the question to those who know Perl: is the source code of a client-server application shorter in Haskel to compare to Perl one? How much shorter (more or less)? Is that source code really easier to debug (in OO Perl it is not really easy :-(
Don't worry, as your programs get larger, the code will tend to stay shorter than in other languages. Pattern matching, better abstractions, simpler data types all add up to less code. -- Don

I have to admit that I think that as shorter source code as better. Therefore the question to those who know Perl: is the source code of a client-server application shorter in Haskel to compare to Perl one? How much shorter (more or less)?
Counter question: can you make quicksort shorter than three lines qsort :: Ord a => [a] -> [a] qsort [] = [] qsort (x:xs) = [y | y <- xs, y <= x] ++ [x] ++ [y | y <- xs, x < y] ? Note that the first line even can be emitted because the compiler can deduce it automatically.
Is that source code really easier to debug (in OO Perl it is not really easy :-(
The point is not to debug your source code but get it correct in the first place. And that's what Haskell and its type system forces you to do. Regards, apflemus

Waldemar Biernacki wrote:
I have to admit that I think that as shorter source code as better. Therefore the question to those who know Perl: is the source code of a client-server application shorter in Haskel to compare to Perl one? How much shorter (more or less)?
My only hard data point here is this: I once converted (just for fun) a short (two screenful) Perl script to Haskell and found that both are about the same length (in LOC). (I find the Haskell version is more readable, but that may just be a personal preference; Perl's syntax is a bit too noisy for my taste.) Anyway, don't let yourself be misled by small trivial examples. For some idioms (e.g. line oriented I/O) Perl has special syntactic sugar that leads to very short code. This makes it quite useful for one-liners that you can hack directly into the command line. However, as programs get larger and more complex, other factors dominate the equation. I'd say the most important aspect is how easy it is to factor common code into functions/subroutines. I think it is very hard to beat Haskell here because of it's excellent support for higher order functions. Yes, you can do higher order functions in Perl (I often do it, my Perl programming is heavily influenced by Haskell ;) but without a type system it can become quite a challenge. Cheers Ben

Hello yourself!-)
Two days ago I have found Haskell in Internet. It sounds very nice. I have read some articles, few examples, ... yes it sounds nice.
you need to try it as well. haskell.org has information, tutorials, interactive implementations (and to answer the likely follow-on question: you will usually need an editor and a Haskell implementation side by side; edit your program in the editor, load the program into the implementation to play with), ..
Now my problem is connected with the "non-update" object feature. I can't write "variable" instead object because - from the meaning of the word ( variable ) - it has the possibility to CHANGE its value. Yes that my problem :-(
a common (mis-)interpretation of "variable". a variable is a representation of something that may not be constant, so far we are in agreement. but the updateable variables you are referring to are a special case of variables, those representing storage locations, the _contents_ of which may be changed. it may be useful to think of variables as named placeholders for things, that is, you either have the named placeholder, or you have the thing itself. the process of replacing those placeholders with the things they stand for is called variable substitution. update programming is all about communicating with some external store of boxes (or objects), and updateable variables stand for references to such boxes (or identities of objects). your program is a sequence of updates of the contents of those boxes - while the variables keep refering to the same locations/references/object identities! executing an update program modifies the external store by performing the prescribed updates, step by step. functional programming is all about replacing programs with programs, and variables are simply named placeholders in such programs. your program is a description of a problem, which may contain variables. evaluating a functional program modifies the program, replacing program parts by other program parts (and possibly variables by things), step by step. the aim of update programming is to transform a store whose contents describe a problem into a store whose contents describe a solution. the aim of functional programming is to transform a program which describes a problem into a program which describes a solution. non-declarative languages support very little functional programming (although that is changing;-), so programmers mostly focus on update programming. Haskell supports both functional programming and update programming, and programmers are encouraged to solve the major part of their problems through functional programming, resorting to update programming only where necessary. and no, I haven't forgotten your problem;-) if you want to reap the full benefits of learning functional programming, you need to adapt your way of thinking about programming to the additional flexibility that Haskell provides you with, and think of transforming programs into programs, rather than transforming stores into stores. variables now stand for parts of the programs that you can fill in, not for parts of the store that you can change. in update programming, you need box variables, because the program doesn't change, only the contents of the store change. in functional programming, the whole program is transformed during evaluation - most of the time, no need for an external store because everything is variable.
Let's assume that I've got to write the application that should works as follows:
1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout.
How should I think "properly" in Haskell to get described action?
Haskell implementations interleave the two phases of functional program evaluation and update program execution (the latter most commonly being input/output). in particular, the value of "Main.main" is expected to evaluate to an update program, the first step of which is then executed, feeding the result of that first step back into the functional evaluation phase, and so on and so on.. you describe an application which requires a dialogue between program and user, so you need for your program to produce such a dialogue as the value of main. in fact, dialogue descriptions can be created and composed just like other functional values: main = do name <- getName let output = various name putStrLn output getName = do putStr "your name, please? " getLine various name = "hello, "++name++" (that's "++show (length name)++" letters)" note how the value of main is a dialogue, composed of an input phase involving i/o, a computation phase involving no i/o, and an output phase involving i/o again. note also how "various" is the name of a placeholder variable standing for a specific function, and how "name" is the name of a placeholder variable standing for the user input. if you call "various" multiple times, or if you run "main" multiple times, the same name in the same dialogue and the same function will stand for different inputs, so the value of the variable named "name" will vary. but all of this talk only because you asked about how to think about actions and variables in Haskell - as others have pointed out, the alternative is to dive right in, and to wait for the implications to become clear later, after a (possibly extended) while of practice. hope this helps, claus

Hello Waldemar, Saturday, December 9, 2006, 7:57:52 PM, you wrote:
1. At the beginning the user should write her/his name (for instance as the application parameter). 2. Let's assume that the application has many various functions defined inside, and - after printing each of the outputs - the name has to be printout.
How should I think "properly" in Haskell to get described action?
1) variables exists, but supported via additional libs instead of being part of language definition 2) in many cases, you don't need vars because function parameters used instead. in particular, recursion used instead of cycles (code generator generates cycles, of course, in terms of raw cpu ocde). also, data input operations, technically speaking, receive "continuation procedure" which they calls with a data input as a parameter. for example, code: var <- getLine putStr var converted internally into the following: getLine (\var -> putStr var) you can find details in http://haskell.org/haskellwiki/IO_inside really, changing one's mind to see functional ways to do something instead of imperative ways, and realizing that full imperative power is still available in rare cases when you need it, is main problem when learning Haskell. i personally abandoned my first attempt to learn because i don't understood how arbitrary imperative action can be implemented in Haskell. but now i've written in Haskell I/O library, arrays&variables library, and data-compression utility :) so i suggest you to read as much Haskell code as possible to see how it can be used (in particular, look into sources of Base and MissingH libraries, and http://haskell.org/haskellwiki/Simple_unix_tools ), read briefly how I/O actions work in Haskell ( http://haskell.org/haskellwiki/IO_inside ), and may be look into sources of my utility ( http://www.haskell.org/bz/FreeArc-sources.tar.gz ) - it contains tons of Russian comments, if you know this language this will be much more interesting reading ps: if you are going from OOP world, look also at http://haskell.org/haskellwiki/OOP_vs_type_classes :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hello Bulat, Saturday, December 9, 2006, 9:39:35 PM, you wrote:
so i suggest you to read as much Haskell code as possible to see how it can
i forget to finish my thought :) ... and when you'll realize that Haskell has the same power for imperative programming as any other language, i suggest you to temporarily forget about this imperative side of Power and go to learn pure functional side. imperative Haskell programming should be considered as advanced material because it is much less required than in traditional languages, and works through the same pure functional mechanisms (recursion, continuations, monads) what you have to learn in order to get deep understanding how imperative Haskell programs work so, just remember that functional programming in Haskell is basic material and imperative programming is advanced material and learn in order :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (9)
-
apfelmus@quantentunnel.de
-
Benjamin Franksen
-
Bulat Ziganshin
-
Claus Reinke
-
Dan Mead
-
dons@cse.unsw.edu.au
-
Neil Mitchell
-
Stefan O'Rear
-
Waldemar Biernacki