在过去的几个月里,我一直在学习Haskell,我遇到了Monoids的例子让我感到困惑.
鉴于这些定义:
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
instance F.Foldable Tree where
foldMap f Empty = mempty
foldMap f (Node x l r) = F.foldMap f l `mappend`
f x `mappend`
F.foldMap f r
Run Code Online (Sandbox Code Playgroud)
而这棵树:
testTree = Node 5
(Node 3
(Node 1 Empty Empty)
(Node 6 Empty Empty)
)
(Node 9
(Node 8 Empty Empty)
(Node 10 Empty Empty)
)
Run Code Online (Sandbox Code Playgroud)
如果我跑:
ghci> F.foldl (+) 0 testTree
42
ghci> F.foldl …Run Code Online (Sandbox Code Playgroud) 我今年夏天和空闲时间一起练习Go图像包.
package main
import (
"os"
"image"
"image/png"
"image/color"
"log"
"fmt"
"reflect"
)
func main(){
file , err := os.OpenFile("C:/Sources/go3x3.png", os.O_RDWR, os.FileMode(0777))
if err != nil {
log.Fatal(err)
}
img , err := png.Decode(file)
if err != nil {
log.Fatal(err)
}
img.At(0,0).RGBA()
fmt.Println("type:", reflect.TypeOf(img))
m := image.NewRGBA(image.Rect(0, 0, 640, 480))
fmt.Println("type:", reflect.TypeOf(m))
m.Set(5, 5, color.RGBA{255, 0, 0, 255})
img.Set(0, 0, color.RGBA{136, 0, 21, 255})
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是当我用img.Set注释掉的时候运行它我得到了这个结果
type: *image.RGBA
type: *image.RGBA
Run Code Online (Sandbox Code Playgroud)
但当它没有注释时,我得到一个错误说
img.Set undefined (type image.Image has no field …Run Code Online (Sandbox Code Playgroud)