我已经看到代码中使用了MaybeandEither函子(和 applicative),这是有道理的,但是我很难想出一个State函子和 applicative的例子。也许它们不是很有用,只是因为Statemonad 需要函子和应用程序而存在?有很多关于它们的实现的解释,但在代码中使用它们时没有任何示例,因此我正在寻找有关它们如何单独使用的说明。
我无法理解这个函数中发生了什么。我的理解是fmap f x返回一个接受最后一个参数的函数y。但是什么时候y“喂”到fmap f xcase 语句里面?
func :: (Num a, Num b) => (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
func f x y = case fmap f x of
Nothing -> Nothing
Just z -> fmap z y
Run Code Online (Sandbox Code Playgroud) 我无法使用 erlang 连接到 mysql/mariadb。
根据这个github-page的要求:https : //github.com/mysql-otp/mysql-otp
要求:
Erlang/OTP version R16B or later
MySQL database version 4.1 or later or MariaDB
GNU Make or Rebar or any other tool for building Erlang/OTP applications
Run Code Online (Sandbox Code Playgroud)
版本:
14> erlang:system_info(otp_release).
"22"
Run Code Online (Sandbox Code Playgroud)
我不确定是否不再需要此要求,但我添加了以下内容:
[mysqld] default_authentication_plugin=mysql_native_password
到我的 /etc/my.cnf。但这可能无关紧要,因为错误是一个未定义的函数。我可以编译代码,但我不能运行它。非常感谢任何帮助使其正常工作。
编码:
-module(mydatabase).
-compile(export_all).
connect_to_database() ->
Conn = mysql:start_link([{host, "localhost"}, {user, "user"},
{password, "password"}, {database, "MyDatabase"}]) ,
case Conn of
{ok, Pid} -> io:fwrite("~w~w~n", [ok,Pid]);
{error, ConnErr} -> io:fwrite("error : ~p ~n", [ConnErr])
end.
start() -> connect_to_database().
Run Code Online (Sandbox Code Playgroud)
mariadb …
haskell ×2
applicative ×1
erlang ×1
erlang-shell ×1
functor ×1
mariadb ×1
mysql ×1
state-monad ×1