Aprender - F# - Learning a new language

06/01/2016 19:28

This is free and good way to study F#:

https://www.tryfsharp.org/Learn/getting-started#bindngs-values

 

 

// Sample 01 -- Calling function by function 
=========================================================
let chrisTest test = 
    test "Chris"
    
let isMe x = 
    if x = "Chris" then
        "it is Chris!"
    else 
        "it's someone else" 
        
chrisTest isMe   
printfn "%s" (isMe "Chris")          
 
// Sample 02 -- Calling function by function 
=========================================================
let twoTest test = 
    test 2
    
twoTest (fun x -> x > 0)
 
type MushroomColor =
| Brown
| Green
| Purple
| Yellow
 
type PowerUp =
| FireFlower
| Mushroom of MushroomColor
| Star of int
 
let myMushroomColors = [ Green; Green; Brown; Green; Purple; Yellow; Brown ]
 
let mushroomProcessor mushroomColors =
    mushroomColors 
    |> List.iter (fun mush -> 
        match mush with
        | Brown -> printfn "I'm a red"
        | Green | Purple | Yellow -> printfn "I'm a not red" )
        
mushroomProcessor myMushroomColors
 
let myMushroomColorsString = [ "Green"; "Green"; "Brown"; "Green"; "Purple"; "Yellow" ]
 
let mushroomProcessorS mushroomColorsString =
    mushroomColorsString 
    |> List.iter (fun mush -> 
        if mush = "Red" then
            printfn "I'm a red"
        else
            printfn "I'm a not red")
            
mushroomProcessorS myMushroomColorsString
 
 
// Sample 01 -- Everything in F# is "immutable" (read only)
=============================================================
let varX = "Test1"
 
printfn "Result: %s" (if varX = "Test1" then "Test2" else "")
 
//printfn "Result (using match): %s" (match varX with | "Test1" -> "Test2" | _ -> "")  
 
printfn "Result (using match): %s" (
    match varX with 
    | "Test1" -> "Test2" 
    | _ -> "")  
 
let z =
    if varX = "Test1" then 
        "Test2" 
    else 
        ""
 
//if varX = "Test1" then
//    "Test2"
//else
//    ""
//|> printfn "Result: %s"   
        
// Get out clause but don't use this! Not good practice
 
let mutable varX1 = "Test1"
 
let z1 =
    if varX = "Test1" then
        varX1 <- "Test2"
    else
        varX1 <- ""
    
printfn "Result: %s" varX1