In imperative programming, we usually have code that looks the following way:
func addOneToSlice(xs []int) []int { rs := make([]int, len(xs)) for i, value := range xs { rs[i] = value 1 } return rs }
However, notice the following about the for loop:
Compare how the same task would be done in F#:
let rec addOneToList = function | [] -> [] | x :: xs -> x 1 :: addOneToList xs
Now consider the following:
Given these restrictions, adding 1 to any element y not at the head of the list would significantly alter the structure of our function.
Now compare how the computation progresses in both styles:
In functional style, marrying both scope with computational progress has the following consequences:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3