小编ram*_*ion的帖子

通用类型统一:多个参数(T,T)与多个参数列表(T)(T)?

我对curried和uncurried通用函数之间的类型检查的差异感到有点困惑:

scala> def x[T](a: T, b: T) = (a == b)
x: [T](a: T, b: T)Boolean
scala> def y[T](a: T)(b: T) = (a == b)
y: [T](a: T)(b: T)Boolean
Run Code Online (Sandbox Code Playgroud)

我的直觉是,这两个x(1, "one")y(1)("one")应该给类型的错误,但是我错了:

scala> x(1, "one")
res71: Boolean = false
scala> y(1)("one")
<console>:9: error: type mismatch;
 found   : java.lang.String("one")
 required: Int
              y(1)("one")
                   ^
Run Code Online (Sandbox Code Playgroud)

起初我以为有某种隐式演员正在进行,但事实似乎并非如此:

scala> x(1 :Int, "one" :String)
res73: Boolean = false
Run Code Online (Sandbox Code Playgroud)

发生什么了?我的直觉应该是什么?

generics scala

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

GHC:无法推断幻像类型参数

所以我试图使可变长度元组类型,基本上是一个漂亮的版本Either a (Either (a,b) (Either (a,b,c) ...))Either (Either (Either ... (x,y,z)) (y,z)) z.

{-# LANGUAGE TypeOperators, TypeFamilies, MultiParamTypeClasses, FlexibleInstances #-}
module Temp where

-- type level addition
data Unit
data Succ n

class Summable n m where
  type Sum n m :: *

instance Summable Unit m where
  type Sum Unit m = Succ m

instance Summable n m => Summable (Succ n) m where
  type Sum (Succ n) m = Succ (Sum n m)

-- …
Run Code Online (Sandbox Code Playgroud)

haskell tuples ghc phantom-types

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

初学者:我怎么说"任何超类通用A"

我正在使用Scala By示例开始时的QuickSort示例,并尝试将其调整为泛型类型A,而不仅仅是Ints.

到目前为止我的工作是

def sort[A <: Ordered[A]](xs: Array[A]) 
Run Code Online (Sandbox Code Playgroud)

它允许sort在所有反射性排序的类型上运行,例如RichBoolean.

但是我还想允许A它们扩展的类型,Ordered[B]其中B是A的超类(例如,任何扩展的东西Ordered[Any]).

我该怎么说呢?


我真正得到了工作,感谢agilesteel的回答:

case class X( i : Int ) extends Ordered[X] {
  def compare( x : X ) = x.i - i
}

class Y( i : Int, j : Int ) extends X(i)

case class Z( i : Int ) extends Ordered[Any] {
  def compare( a : Any ) : …
Run Code Online (Sandbox Code Playgroud)

generics scala

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

Scala:(Int,Int)=> Int不匹配(Int,Int)=> Int

我正在尝试使用y-combinator在scala中定义gcd:

object Main {
  def y[A,B]( f : (A => B) => A => B ) : A => B = f(y(f))
  def gcd = y[(Int,Int),Int]( (g) => (x,y) => if (x == 0) y else g(y % x, x) )
}
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

Main.scala:3: error: type mismatch;                                                  
 found   : (Int, Int) => Int                                                               
 required: (Int, Int) => Int                                                               
    def gcd = y[(Int,Int),Int]( (g) => (x :Int,y :Int) => if (x == 0) y else g(y % x, x) ) 
                                                       ^ …
Run Code Online (Sandbox Code Playgroud)

scala y-combinator greatest-common-divisor

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

找到最大集群的最小值?

将项目定义为:

  • 一个独特的身份
  • 一个值
  • 创造时间
  • 删除时间

我有两个输入流 - 一个在创建项目时通知我,一个在项目被删除时通知我.调用已创建但未销毁"生活"的项目.

我可以使用堆跟踪所有生活物品的最大值:

whenCreated(item):
  i = heap.size
  heap-up(item, heap.size)
  heap.size = heap.size + 1
  max-value = heap[0]

whenDeleted(item):
  ktem = heap[heap.size - 1]
  heap.size = heap.size - 1
  heap-up(ktem, index[item.id])
  heap-down(ktem, index[ktem.id])
  max-value = heap[0]

heap-up(item, i):
  while (i > 0):
    j = floor( (i-1) / 2 )
    jtem = heap[j]
    if (jtem.value > item.value):
      break while
    index[jtem.id] = i 
    heap[i] = heap[i]
    i = j
  index[item.id] = i
  heap[i] = item

heap-down(item, i): …
Run Code Online (Sandbox Code Playgroud)

language-agnostic algorithm cluster-analysis stream data-structures

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

Haskell:我应该编译正则表达式吗?

我的冲动是说是的,特别是如果我在多个代码位置使用相同的正则表达式,但本文表明该库将为我缓存编译(我甚至不确定它将如何做):

通常不需要编译正则表达式模式.模式将在第一次使用时进行编译,并且您的Haskell运行时应该为您记住编译的表示.

regex haskell

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

Scala:解析匹配令牌

我正在玩一个玩具HTML解析器,以帮助我熟悉Scala的解析组合库:

import scala.util.parsing.combinator._ 
sealed abstract class Node                                                                    
case class TextNode(val contents : String)  extends Node                                      
case class Element(                                                                           
  val tag : String,                                                                           
  val attributes : Map[String,Option[String]],                                                
  val children : Seq[Node]                                                                    
)  extends Node                                                                               

object HTML extends RegexParsers {                                                            
  val node: Parser[Node] = text | element                                                     
  val text: Parser[TextNode] = """[^<]+""".r ^^ TextNode                                      
  val label: Parser[String] = """(\w[:\w]*)""".r                                              
  val value : Parser[String] = """("[^"]*"|\w+)""".r                                   
  val attribute : Parser[(String,Option[String])] = label ~ (                                 
        "=" ~> value ^^ Some[String] | "" ^^ { …
Run Code Online (Sandbox Code Playgroud)

parsing scala

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

不能使用ghci推断的类型签名来返回Sing(d :: Symbol)的函数

我正在尝试恢复Symbol值的类型中使用的:

{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Temp where

import GHC.TypeLits

data Temp (d :: Symbol) (a :: *) where 
  T :: a -> Temp d a

{-
description :: SingI Symbol d => Temp d a -> Sing Symbol d
-}
description (_ :: Temp d a) = (sing :: Sing d)
Run Code Online (Sandbox Code Playgroud)

这在ghci(版本7.6.1)中加载正常:

% ghci
GHCi, version 7.6.1: http://www.haskell.org/ghc/  :? for help
Loading package …
Run Code Online (Sandbox Code Playgroud)

haskell

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

URI.parse的替代方法,允许主机名包含下划线

我使用DMOZ网址列表的主题,其中包含一些网址具有包含下划线的主机名.

例如:

608  <ExternalPage about="http://outer_heaven4.tripod.com/index2.htm">
609    <d:Title>The Outer Heaven</d:Title>
610    <d:Description>Information and image gallery of McFarlane's action figures for Trigun, Akira, Tenchi Muyo and other Japanese Sci-Fi animations.</d:Description>
611    <topic>Top/Arts/Animation/Anime/Collectibles/Models_and_Figures/Action_Figures</topic>
612  </ExternalPage>
Run Code Online (Sandbox Code Playgroud)

虽然这个网址可以在网络浏览器中使用(或者,至少在我的网站中也是如此:p),但根据标准,这是不合法的:

主机名可能不包含其他字符,例如下划线字符(_),

在尝试使用以下方法解析此类URL时会导致错误URI.parse:

[2] pry(main)> require 'uri'
=> true
[3] pry(main)> URI.parse "http://outer_heaven4.tripod.com/index2.htm"
URI::InvalidURIError: the scheme http does not accept registry part: outer_heaven4.tripod.com (or bad hostname?)
from ~/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/generic.rb:213:in `initialize'
Run Code Online (Sandbox Code Playgroud)

有没有替代URI.parse我可以使用具有较低严格性而不仅仅是自己滚动?

ruby uri

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

仅对直接模块进行 doctests

如果我有三个模块:

-- A.hs
module A where
-- $
-- >>> True
-- True

-- B.hs
module B where
import A
-- $
-- >>> True
-- True

-- C.hs
module C where
import B
-- $
-- >>> True
-- True
Run Code Online (Sandbox Code Playgroud)

运行doctest C.hs将运行所有三个文件中的所有 doctest。

$ doctest C.hs
Examples: 3  Tried: 3  Errors: 0  Failures: 0
Run Code Online (Sandbox Code Playgroud)

有没有办法doctest只在顶级模块上运行——也就是说,它不是递归搜索包含模块中的测试吗?

testing doctest haskell

5
推荐指数
0
解决办法
61
查看次数