I played around with the F # list
module File1
//Try using a list
//Declare a list
let strs= ["dogs";"cats";"bird"]
//Try declaring the format with printfn
printfn "%A" strs //["dogs"; "cats"; "bird"]Is displayed
printfn "%O" strs //[dogs; cats; bird]Is displayed
//Access the list using pattern matching
//Pattern match 1
let rec accessList strs =
match strs with
|x::xs->printfn "%O " x;printfn "%O " (accessList xs);x+" a"
|[]->printfn "The end";"b"
//Pattern match 2
let rec accessList2 strs =
match strs with
|x::xs->printf "%O " x; accessList2 xs
|[]->printfn "The end";
in
//There is a return value, so you need to bind the result with let
let a=accessList strs
//dogs
//cats
//bird
//The end
//b
//bird a
//cats a
//Is displayed
printfn "Display the result bound by let"
printfn "%s" a
//Display the result bound by let
//dogs a
//Is displayed
accessList2 strs
//dogs cats bird The end is displayed
It looks like the output result of the program is seen, and it looks like the figure below. If you start from the matching part with []->, it may be easier to grasp the execution image of recursive processing.
It's an image that has both Java List and Deque (Stack) properties. Lists in functional languages seemed to have smoother list operations than Java Lists, but if they also have the properties of Deque (Stack), that might be natural.
Recommended Posts