Random programming things I'd want to remember

Wednesday, November 12, 2008

Project Euler in F#, Problem 6

Here's the code:

let square x = x * x
let list1 = [1 .. 10]

let rec sum list =
match list with
| h::tail -> (sum tail) + h
| []->0

let rec sumSquaredListElements list =
match list with
| h::tail -> (sumSquaredListElements tail) + h * h
| [] -> 0


printfn "%A" list1

let print x = printfn "%i" x

let x1 = square (sum list1)

let main () = print x1

main()

Tuesday, November 11, 2008

Learning a functional language

A challenge in learning a language is application of the newly-acquired knowledge. A wonderful web site, "Project Euler," presents a number of mathematical puzzles. I could learn F# by solving those problems. Here's the answer to problem number 1:

#light

open System

let by32 = List.filter(fun n -> n%3 = 0 || n%5 = 0) [1 .. 9]

let rec sum list =
match list with
| h::tail -> (sum tail) + h
| []->0

printfn "%A" by32

let print x = printfn "%i" x
let x1 = sum by32

let main () = print x1

main()

Console.ReadKey(true)