Implement recursive closures in Go

Introduction

Studying Go language version 2 means that I tried to study algorithms with Go.

Recursive closure

First of all, what is a closure, I will describe the sentence that I referred to below.

Go's anonymous function is a "closure". Closures are called "function closures" in Japanese, and are "confined (closures)" as a set of functions and "outside function" environments related to function processing.

So it represents an anonymous function. This time I started by using this to create a recursive function.

  1. I created a function [func fact (n int) int] that calculates the factorial below.

fact.go


package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var a int

	fmt.Fscan(r, &a)

	result := solveFact(a)
	fmt.Print(result)
}

func solveFact(x int) int {
	var fact func(n int) int
	 fact = func(n int) int{
		if n == 0{ return 1 }
		return n * fact(n - 1)
	}
	return fact(x)
}

Execution result

❯ go run fact.go
4
24
  1. I created a function [func fib (n int) int] that calculates the Fibonacci sequence.

fib.go


package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var a int

	fmt.Fscan(r, &a)

	result := solveFib(a)
	fmt.Print(result)
}

func solveFib(x int) int {
	var fib func(n int) int
	fib = func(n int) int{
		if n <= 1{return n}
		return fib(n -1) + fib(n - 2)
	}
	return fib(x)
}

Execution result

❯ go run fib.go
10
55

Summary

Fmt.Scan () / fmt.Printf () is used for input and output. However, I am afraid of the amount of input and output, so I use bufio and try to buffer it.

Recommended Posts

Implement recursive closures in Go
Implement and understand union-find trees in Go
Java programmer touched Go language (Implement Java inheritance in Go language)
Implement Enigma in python
Write Pulumi in Go
Implement recommendations in Python
Implement XENO in python
Implement sum in Python
Implement Traceroute in Python 3
Implement LSTM AutoEncoder in Keras
Implement follow functionality in Django
Implement timer function in pygame
Implement Style Transfer in Pytorch
Implement naive bayes in Python 3.3
Implement UnionFind (equivalent) in 10 lines
Implement ancient ciphers in python
Hello World in GO language
Implement Redis Mutex in Python
Implement extension field in Python
Implement fast RPC in Python
Implement method chain in Python
Implement Dijkstra's Algorithm in python
Implement Slack chatbot in Python
Implement Gaussian process in Pyro
Implement stacking learning in Python [Kaggle]
Implement Table Driven Test in Java
Implement R's power.prop.test function in python
Try implementing Yubaba in Go language
Use optinal type-like in Go language
Implement a date setter in Tkinter
Implement the Singleton pattern in Python
Post to slack in Go language
Quickly implement REST API in Python
Write tests in GO language + gin
Do something object-oriented in GO language