小编che*_*ire的帖子

检查列表中的所有bolean元素在Haskell中是否相同

我想检查列表中的布尔元素是全是True,全是False还是True和False.这是我现在的代码:

data Answer3 = Just_True | Just_False | Both deriving (Eq, Ord, Show)
type A3 = Answer3


checkBoolean :: [Bool] -> A3
checkBoolean (x:x1:xs) = if (length (x:x1:xs) `mod` 2) == 0  
                            then if (x && x1) && checkBoolean'(x1:xs)== True
                                     then Just_True
                                     else if (x && x1) == True
                                             then Both
                                             else if checkBoolean'(xs)== True
                                                     then Both
                                                     else Just_False


                            else if x && checkBoolean'(x1:xs) == True
                                    then Just_True
                                    else if x == False
                                            then Just_False
                                            else Both 

checkBoolean':: [Bool] -> …
Run Code Online (Sandbox Code Playgroud)

haskell

9
推荐指数
3
解决办法
1121
查看次数

将函数作为参数传递并返回函数 - Haskell

我有一个函数图表f(n),它返回

5  if n = 0 
2  if n = 1 
-4 if n = 2 
1  if n = 3 
9  if n = 4 
8  if n = 5 
9  if n = 6 
0  otherwise 
Run Code Online (Sandbox Code Playgroud)

我想编写一个函数来表示一个带有一对List的图形:

type Nat0 = Int 
type Z = Int
type List = [Z] 
type Graph = [(Nat0,Z)] 

list_to_graph :: List -> Graph
list_to_graph x = list_to_graph' ([0..(length x)-1 ]) (x)

list_to_graph' :: [Int] -> List -> Graph
list_to_graph' (x:xs) (y:ys) …
Run Code Online (Sandbox Code Playgroud)

haskell higher-order-functions

7
推荐指数
1
解决办法
1167
查看次数

高阶函数在haskell中解谜函数

我试图重建一个金字塔的谜语:

插图1

金字塔的最后一层是从1到n的数字的排列,其中n是字段的数量.然后,不在最低层中的每个其他字段是该数字下对角线的数字的总和.

所以我想做一个函数,当给出左边的谜语时,返回右边的解决方案.我计划通过枚举这样的图层来做到这一点:

枚举金字塔

对于我制作自定义数据类型的图层:

data Layer = One | Two | Three | Four | Five | Six
deriving (Eq,Ord,Enum,Show)
Run Code Online (Sandbox Code Playgroud)

和其他类型:

type LayerDepth = Layer
type FieldNumber = Int
type FieldContent = Int
type FieldAssignment = (FieldNumber -> FieldContent)
type PyramidRiddle = FieldAssignment
type PyramidSolution = FieldAssignment
type PyramidSolutions = [PyramidSolution]
Run Code Online (Sandbox Code Playgroud)

和功能:

solveRiddle :: LayerDepth -> PyramidRiddle -> Pyramidsolutions
Run Code Online (Sandbox Code Playgroud)

对于图示的例子,我将创建类型的匿名函数(FieldNumber - > FieldContent):

fieldAssignment1 = \n -> if (n==6) || n==8) then 3 else 0
Run Code Online (Sandbox Code Playgroud)

此功能将标记第3和第8个字段,编号为3

然后打电话: solveRiddle Four fieldAssignment1 …

haskell higher-order-functions

4
推荐指数
1
解决办法
146
查看次数

Haskell映射函数,返回没有零的列表

这是我的代码:

x = map classify[1..20] 

classify :: Int -> Int 
classify n 
         | n `mod` 2 == 0 = n + 2
         | otherwise = 0
Run Code Online (Sandbox Code Playgroud)

调用x时,它返回一个列表

[0,4,0,6,0,8,0,10,0,12,0,14,0,16,0,18,0,20,0,22]
Run Code Online (Sandbox Code Playgroud)

如何指定我只想要不是0的数字列表:

[4,6,8,10,12,14,16,18,20,22]
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
211
查看次数

制作Ord类的newtype实例

由于Ord是Eq的一个子类,我发现很难理解如何使该类的newtype实例看起来像.

我设法做到了这一点:

    newtype NT1 = NT1 Integer

    instance Eq NT1 where 
        (NT1 x) == (NT1 y) = x == y 

    instance Ord NT1 where 
        (NT1 x) `compare` (NT1 y) = x `compare` y 
Run Code Online (Sandbox Code Playgroud)

如果我为考试有一个变量x = NT1 5和变量y = NT1 5并输入x == y它将返回True

我还设法做到了这一点:

instance Show NT1 where
        show (NT1 n) = show n
Run Code Online (Sandbox Code Playgroud)

whill显示x = NT1 55而不是NT1 5

在此之后,我应该能够做到这样的事情:

instance Ord NT1 where 
       (>)  (NT1 x)(NT1 y)  =  (NT1 x) …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
94
查看次数

从newtype转换为Int,从Int转换为newtype

如何将newtype转换为Int,反之亦然?

我试过了:

newtype NT1 = NT1 Integer

fromNT1toInt :: NT1 -> Int
fromNT1toInt x = let y = x :: Int
                 in y
Run Code Online (Sandbox Code Playgroud)

但我得到的不能匹配预期的类型错误

我尝试制作Enum类的NT1实例,但我不太了解toEnum是如何工作的

newtype NT1 = NT1 Integer

instance  Enum NT1 where
toEnum x = let x = x :: Integer
           in if x >= 0
                 then x :: NT1 
                 else x :: NT1
Run Code Online (Sandbox Code Playgroud)

当我调用Ennum 5 :: NT1这应该返回5 NT1但我得到StackOverflow错误.我哪里弄错了?

编辑:newtype名称

haskell

2
推荐指数
2
解决办法
250
查看次数

在路由中将对象作为参数传递返回 [Object object]

这是在我的 routing.module 中:

{path: 'hero' component: HeroComponent}
Run Code Online (Sandbox Code Playgroud)

这就是我通过路由传递对象的方式:

this.router.createUrlTree(['/hero', {my_object: this.Obj}]));
window.open(url, '_blank');
Run Code Online (Sandbox Code Playgroud)

在我的英雄组件中,我读取对象如下:

this.route.snapshot.paramMap.get('my_object');
Run Code Online (Sandbox Code Playgroud)

console.log(this.route.snapshot.paramMap.get('my_object');) 返回:

[Object object]
Run Code Online (Sandbox Code Playgroud)

并读取对象的属性,例如.id返回:

undefined
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用它进行迭代,*ngFor我会得到:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

typescript angular-routing angular angular-router

2
推荐指数
1
解决办法
3035
查看次数

理解递归函数列表中的列表 - haskell

我正在编写一个函数来按元组或三元组排序给定列表.它应该按照列表中两个元素之间的数字来排序.

例如,如果给定列表是[1,2,3,6,7]它应该返回,[[1,2,3], [6,7]] 因为在1,22,3之间以及在6,7之间存在零数

这是我的代码:

import Data.List 

check :: [Int] -> [[Int]]
check listCopy@(x:xs) = 
    let sorted = sort (listCopy) 
        in if (length sorted > 1) 
              then if ((sorted !! 1 ) - (sorted !! 0)) == 1 || ((sorted !! 1 ) - (sorted !! 0)) == 0 
                      then [[x]  ++ check(xs) !! 0] 
                      else [[x]] ++ check(xs)
              else [[x]]
check [] = [[]]
Run Code Online (Sandbox Code Playgroud)

if ((sorted !! 1 ) …

haskell

1
推荐指数
1
解决办法
166
查看次数

JPA 原生 sql 查询中的 like 子句

我有一个返回产品列表的查询:

@Query(value = "Select * from Product join Product_Pro pp on Product.ID = pp.PRODUCT_ID where A_ID= ?1 order by name",
        nativeQuery = true)
    List<Product> findAll(long id);
Run Code Online (Sandbox Code Playgroud)

我如何LIKE为此添加一个子句?

当我在数据库中执行此语句时,得到结果:

Select * from Product join Product_Pro pp on Product.ID = pp.PRODUCT_ID where A_ID= ?1 AND name LIKE '%someString%' AND content LIKE '%someString%' order by name
Run Code Online (Sandbox Code Playgroud)

但如何添加LIKE到我的 JPA 本机查询中?

我努力了:

@Query(value = "Select * from Product join Product_Pro pp on Product.ID = pp.PRODUCT_ID where A_ID= ?1 and name LIKE …
Run Code Online (Sandbox Code Playgroud)

sql jpa where-clause sql-like spring-boot

1
推荐指数
1
解决办法
6592
查看次数

如何调用rest api并且不等待任何响应

我有这个代码

private Boolean doSomething() {
    // SOME CODE
    this.sync();
    return true;
}

public void sync() {
    RestTemplate restTemplate = restTemplate();
    restTemplate.exchange(batchHost, HttpMethod.GET, header(), String.class);
}
Run Code Online (Sandbox Code Playgroud)

我不需要其余通话中的数据。这是他需要时间的批量执行服务。但对于我的doSomething功能来说,我不需要回复即可sync继续。

今天,当其余的调用不起作用、不可用时...我的函数 doSomething .... 什么都不做...

如何调用休息服务但继续没有错误doSomething()没有错误?

谢谢

我用的是java 11

java rest asynchronous spring-boot

1
推荐指数
1
解决办法
9344
查看次数

检查haskell中是否为元组或三元组

有没有办法检查有多少元素(,)?我知道我可以访问的元组的第一和第二个元素与fstsnd,但是,我想我能以某种方式和元素,然后将其与FST元组SND元组比较和检查是这样的:

tuple = (1,2)

sum tuple == fst tuple + snd tuple
Run Code Online (Sandbox Code Playgroud)

然后我为这个案子得到了真,并得到了假triple = (1,2,3).无论如何,我无法问fst (1,2,3)我也不能做sum tuple

有没有办法检查我是否有元组?

像这样的东西:

is_tuple :: (a,b) -> a
is_tuple (a,_) = a
Run Code Online (Sandbox Code Playgroud)

但是当我输入元组时得到True而当我给(1,2,3)或(1,2,3,4)等等时......作为输入.

即:

is_tuple :: Tuple -> Bool
is_tuple x = if x is Tuple 
                then True
                else False
Run Code Online (Sandbox Code Playgroud)

haskell

0
推荐指数
1
解决办法
265
查看次数