Follow Slashdot stories on Twitter

 



Forgot your password?
typodupeerror

Comment Haskell and side effects (was Re:Concurrency?) (Score 1) 173

Haskell does have side effects, just like any other useful programming language. However, you can't put side effects just anywhere in a Haskell program; you have to explicitly specify which functions have side effects.

This is done using the type system so that the return values of all functions that can have side effects must be IO x (ie. IO<x> in C++/Java notation). This way, you (and the compiler) can be sure that any function that DOESN'T have the type IO x is 100% side effect free, allowing easy parallelization, many types of optimizations, etc. In other words, all side effects are put in a separate "side effect bin".

Another big thing to understand is that Haskell makes it impossible (well, not really, but strongly discouraged) to return a value from IO back to the pure part of the program. Any computation that may depend on the result of some side effect is considered to have side effects of its own; removing a value from the "side effect bin" is a side effect. Essentially, side effect code can call both side effect code and pure code, but pure code can only call other pure code.

Since every useful program has at least some side effects (reading input, returning output), every Haskell program has a main function which has the type IO () (ie. has side effects, doesn't return anything). The main function can then call the rest of the program, just like in other programming languages.

In order to keep this type of programming from being a total pain in the ass to program with, Haskell uses monads (which are an unrelated concept, they are used for lots of other things as well) to make it easy to compose smaller IO functions into larger, more higher-level IO functions.

Comment Re:Perhaps two of the weirdest: Ctrl-A and Ctrl-X (Score 1) 702

I find ctrl-a and ctrl-x to be very useful. In fact, I have a nifty script in my .vimrc that uses ctrl-a.

" this function repeats the given command for the given amount of times
function! Repeat(times, cmd)
let i = a:times
if i <= 0
let i = 1
endif
while i > 0
execute a:cmd
let i = i - 1
endwhile
endfunction

" define CTRL-Z to copy this line to the next line, but increment the number
" under the cursor for that line. sets the 'q' mark and overwrites the unnamed
" register with the line that has the number incremented.
nmap <C-Z> :<C-U>call Repeat(v:count, "normal mqyyp`qj<C-A>yy")<CR>

Very useful when you have a line of code with a number on it, and you need to replicate that many times and increment the number. Like with case statements:

case DEFINED_123_CONSTANT_0:

It accepts numeric arguments, so you can move your cursor past the '123' and then press 4ctrl-z and you get:

case DEFINED_123_CONSTANT_0:
case DEFINED_123_CONSTANT_1:
case DEFINED_123_CONSTANT_2:
case DEFINED_123_CONSTANT_3:
case DEFINED_123_CONSTANT_4:

I've found this useful very, very often.

Slashdot Top Deals

The trouble with computers is that they do what you tell them, not what you want. -- D. Cohen

Working...