我试图在我的Haskell代码中启动并运行一个简单的Json解析器,我遇到了Data.Aeson,它似乎是我问题的可行解决方案
{-#LANGUAGE OverloadedStrings #-}
import Data.Aeson
import Data.Text
import Control.Applicative
import Control.Monad
data Person =
Person { firstName :: Text
, lastName :: Text
, age :: Int
} deriving Show
instance FromJSON Person where
parseJSON (Object v) =
Person <$> v .: "f_name"
<*> v .: "l_name"
<*> v .: "age"
parseJSON _ = mzero
Run Code Online (Sandbox Code Playgroud)
在GHCi中运行以下内容会导致标题中出现令人讨厌的消息:
decode "{\"f_name\":\"Haskell\", \"l_name\":\"Curry\",\"age\":114}" :: Maybe Person
Run Code Online (Sandbox Code Playgroud)
那么,这里有没有人知道出了什么问题?我几乎完全按照编写的方式跟踪了示例代码,为什么它会失败呢?
我正在组织我的.emacs
文件以更好地跟踪我正在添加的所有内容.
这样做我遇到了标题中描述的错误,我不确定为什么
这是我的.emacs
文件:(评论的加载是我自己的参考)
;;;; Emacs config file
;; convenience function for loading multiple libs in a single call
(defun load-libs (&rest libs)
(dolist (lib libs)
(load-library lib)))
;; path to custom libraries as well as the libraries themselves
(add-to-list 'load-path "~/.emacs.d/lisp/")
(load-libs "convenience" "editor-behaviour")
;; Add support for the package manager
(require 'package)
;; Add various package archives
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/")
'("melpa" . "http://melpa.milkbox.net/packages/"))
;; Installs packages if they aren't already
(package-refresh-and-install ; from …
Run Code Online (Sandbox Code Playgroud) 有没有办法从Scala中的Anonymous类扩展另一个类?我的意思是
abstract class Salutation {
def saybye(): String = "Bye"
}
class anotherClass() {
def dummyFunction() = {
val hello = new {
def sayhello(): String = "hello"
} extends Salutation
val hi = hello.sayhello //hi value is "Hello"
val bye = hello.saybye //bye value is "bye"
}
}
Run Code Online (Sandbox Code Playgroud) 我有这两个测试功能:
int apply_a(int (*fun)(int, int), int m, int n) {
return (*fun)(m,n);
}
int apply_b(int (*fun)(int, int), int m, int n) {
return fun(m,n);
}
Run Code Online (Sandbox Code Playgroud)
它们似乎会返回不同的东西,为什么它们都会产生相同的结果呢?
int add(int a, int b) {return a + b;}
int res_a = apply_a(add, 2, 3); // returns 5
int res_b = apply_b(add, 2, 3); // returns 5
Run Code Online (Sandbox Code Playgroud)
我会假设其中一个会返回指针地址或指针本身; 而不是存储在指针上的值......
那为什么要这样做呢?
我正在编写一种扩展方法,通过删除大量的样板来简化哈希的创建,但问题是每当我单步执行代码时,无论是否调用,我都可以看到它总是选择,或者SHA256Managed
SHA256.Create()
SHA256Cng.Create()
SHA256Managed.Create()
SHA256CryptoServiceProvider.Create()
当我选择不同的哈希算法(如MD5)时,情况也是如此,但在MD5的情况下,无论我实际使用哪个类,它都会选择MD5CryptoServiceProvider
...
这是为什么?
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Utility.Methods
{
public enum HashType { MD5, SHA512, SHA256, SHA384, SHA1 }
public enum HashSubType {Normal, Cng, Managed, CryptoServiceProvider}
public static class TextHasher
{
public static string Hash(this string input, HashType hash, HashSubType subType = HashSubType.Normal)
{
Func<HashAlgorithm, string> hashFunction = alg => HashingHelper(input, alg);
switch (subType)
{
case HashSubType.Normal:
return hashFunction(NormalHashes(hash)); …
Run Code Online (Sandbox Code Playgroud) 我可以用字符串做这样的事情:
s match {
case "" => ...
case head +: tail => ...
}
Run Code Online (Sandbox Code Playgroud)
head
第一个字符在哪里,tail
是剩下的字符串?
在上面的代码中,类型head
是Any
,我希望它是String
或Char
.
几年前,在C#课程中,我学会了编写一个看起来或多或少像这样的二叉树:
data Tree a = Branch a (Tree a) (Tree a) | Leaf
Run Code Online (Sandbox Code Playgroud)
我看到了它的好处,它在分支上有它的值,这允许快速和容易地查找和插入值,因为它会在每个分支的根上遇到一个值,直到它击中叶子,没有价值.
然而,自从我开始学习Haskell之后; 我见过许多像这样定义的树的例子:
data Tree a = Branch (Tree a) (Tree a) | Leaf a
Run Code Online (Sandbox Code Playgroud)
这个定义让我很困惑.我看不到在没有分支的元素上使用数据的有用性,因为它最终会导致一个如下所示的树:
对我来说,这似乎是一个设计不佳的List的替代品.它也让我质疑它的查找时间,因为它无法评估哪个分支向下找到它正在寻找的值; 而是需要通过每个节点来找到它正在寻找的东西.
那么,任何人都可以解释为什么第二个版本(叶子上的值)在Haskell中比第一个版本更普遍吗?
为了测试我在Haskell的技能,我决定实现你在Land of Lisp/Realm of Racket中找到的第一款游戏.在"猜猜我的号码"的游戏.游戏依赖于可变状态来运行,因为它经常需要更新程序的上限和下限以回归用户正在考虑的值.
它有点像这样:
> (guess)
50
> (smaller)
25
> (bigger)
37
Run Code Online (Sandbox Code Playgroud)
现在,这种事情(据我所知)在Haskell中完全不可能,从REPL中调用一些修改全局可变状态的函数,然后立即打印结果,因为它违反了不变性原则.因此,所有交互必须存在于IO
和/或State
monad中.那就是我被困住的地方.
我似乎无法将我的想法包含在IO
monad和State
monad的组合中,所以我可以获得输入,打印结果和修改状态,所有这些都在同一个函数中.
这是我到目前为止所得到的:
type Bound = (Int, Int) -- left is the lower bound, right is the upper
initial :: Bound
initial = (1, 100)
guess :: Bound -> Int
guess (l, u) = (l + u) `div` 2
smaller :: State Bound ()
smaller = do
bd@(l, _) <- get …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数,它需要一个更大整数的个别数字来执行操作.
我尝试过以下方法:
// I can safely unwrap because I know the chars of the string are going to be valid
let digits = num.to_string().chars().map(|d| d.to_digit(10).unwrap());
Run Code Online (Sandbox Code Playgroud)
但借阅检查员表示字符串的寿命不够长.
以下工作:
let temp = num.to_string();
let digits = temp.chars().map(|d| d.to_digit(10).unwrap());
Run Code Online (Sandbox Code Playgroud)
但这看起来更加做作.
这样做有更好的,可能更自然的方式吗?
我正在尝试克隆盒装特征的向量.自然地简单地派生Clone
所有实现我的特征的结构是不够的,因为编译器在编译时不知道实现特征的所有结构都有Clone
.
好的,所以我接着尝试使用Clone
supertrait,但这只会导致标题中的错误.我对解决方案感到茫然.
这是最小工作实现(或不工作,因为我无法克隆)
#![allow(dead_code, unused_macros)]
use std::fmt::Debug;
trait MusicElement: Debug + Clone {
fn duration(&self) -> f32;
}
#[derive(Debug, Clone)]
struct Note<'a> {
name: &'a str,
duration: f32,
}
impl<'a> MusicElement for Note<'a> {
fn duration(&self) -> f32 {
self.duration
}
}
#[derive(Debug, Clone)]
struct Pause {
duration: f32,
}
impl MusicElement for Pause {
fn duration(&self) -> f32 {
self.duration
}
}
#[derive(Debug, Clone)]
struct Sequence {
elements: Vec<Box<MusicElement>>,
}
impl MusicElement for …
Run Code Online (Sandbox Code Playgroud)