Images, snippets, snapshots, math

View Gabriele Lami's profile on LinkedIn

martedì 29 dicembre 2009

Verlet integration in Haskell

Verlet integration algorithm written in 3 minutes


-- Verlet integration

projOneTwo ( a, b, c , d ,e ) = ( a, b)

nextStepInt (_ ,_ , 0 ,_ ,_ ) = []
nextStepInt ( xt ,vt , n , acc , h ) =
let
atph = acc . ( xt + ) $ h
vtph = vt + atph * h * 0.5
xtph = xt + h * vt + 0.5 * acc xt * h^2
in
( xtph, vtph , n-1 , acc , h ) : nextStepInt ( xtph, vtph , n-1 , acc , h )
timeInt ( xt ,vt , n , acc , h ) = map ( projOneTwo ) $ nextStepInt ( xt ,vt , n , acc , h )


example ( spring ) :
timeInt (0 , 1 , 10000 , (\x -> 0.1 * ( - x ) ) , 0.2 )

giovedì 24 dicembre 2009

my delicious

now i'm in delicious: http://delicious.com/koteth
A lot of Haskell links.

martedì 8 dicembre 2009

Cairo-Chaos Haskell

logistic map in haskell
Haskell chaos and lib-cairo.

Is only a fixed-point iteration over this function:

lgs x r = r * x * exp(- x )

Dollar $ operator in Haskell

Do you know what is $ operator in Haskell?
$ means simply , 'apply the left function at the right value'.

f $ x := f x

it seems really trivial, isn't it ?
But, for example in this kind of situation, is really usefull:

zipWith ( $ ) ( cycle [ \x -> div (x + 1) 2 , \x -> div x 2 ] ) [1..]

here you have a infinite list of function:

a = cycle [ \x -> div (x + 1) 2 , \x -> div x 2 ]
( ie: [\x -> div (x + 1) 2 , \x -> div x 2 , \x -> div (x + 1) 2 , \x -> div x 2, ... ] )

and you want to apply every element of the list at the element
at the same index in the second list:

b = [1..]

zipWith, for every index i takes the element a(i) of the left list
and b(i) of the right list and execute what is requested inside the parentheses.
In this situation is specified $ so:

a(i) $ b(i ) := a(i) ( b(i) )

the result must be the following:

[ 1 ,1 , 2, 2 , 3 ,3 ... and so on.

Prime Numbers in haskell

well, do you want to know how to find 'prime numbers' in a quick and dirty
way using Haskell ?
try this!

import Data.List
nubBy ( \x y -> mod y x == 0 ) [2..]

Haskell is so easy and charming...

( ps: if you want to speed up a little bit:
nubBy ( \x y -> ( x*x-1 <= y ) && ( mod y x == 0 ) ) [2..]

)