假设我定义了一个多参数类型类:
{-# LANGUAGE MultiParamTypeClasses, AllowAmbiguousTypes, FlexibleContexts, FlexibleInstances #-}
class Table a b c where
decrement :: a -> a
evalutate :: a -> b -> c
Run Code Online (Sandbox Code Playgroud)
然后decrement,为了简单起见,我定义了一个使用的函数:
d = decrement
Run Code Online (Sandbox Code Playgroud)
当我尝试将其加载到ghci(版本8.6.3)时:
• Could not deduce (Table a b0 c0)
arising from a use of ‘decrement’
from the context: Table a b c
bound by the type signature for:
d :: forall a b c. Table a b c => a -> a
at Thing.hs:13:1-28 …Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手,我正在尝试编写简单的函数让自己习惯于语法,我想编写自己的函数,将某个元素添加到特定索引的列表中.这是我在Atom(我的文本编辑器)中写的内容:
addElem :: a->[a]->Int->[a]
addElem elem list index
| index <= 0 = elem:list
| index < (length list) = a ++ (elem:b) where a = take index list; b = drop index list
| otherwise = list
Run Code Online (Sandbox Code Playgroud)
这个想法是,只要索引是一个Int并且elem与元素的类型相同,它就不会吓到list,但是当我尝试将它加载到ghci时,我得到"解析错误"|"".我是否需要约束参数的类型?我正在阅读Learn You A Haskell,但我还没有完全解释缩进的工作原理,所以我的错误也可能存在.
按照这个Elm教程,我假设了这个功能
update : Msg -> Model -> Model
Run Code Online (Sandbox Code Playgroud)
在教程中定义为
update msg model =
case msg of
Increment -> model + 1
Deccrement -> model - 1
Reset -> 0
Run Code Online (Sandbox Code Playgroud)
我以为我会以同样的方式定义它,但我更喜欢使用语法:
update Increment model = model + 1
update Decrement model = model - 1
update Reset model = 0
Run Code Online (Sandbox Code Playgroud)
但是这不能编译,Elm不支持这种语法或者我犯了错误吗?
假设我想实现Fermi函数(逻辑曲线的最简单的例子),这样如果它被传递,Float它返回一个Float,如果它被传递,Double它返回一个Double.这是我得到的:
e = 2.7182845904523536
fermiFunc :: (Floating a) => a -> a
fermiFunc x = let one = fromIntegral 1 in one/(one + e^(-x))
Run Code Online (Sandbox Code Playgroud)
问题是ghc说的e是Double.定义变量one也有点粗糙.我想到的另一个解决方案就是定义双精度函数:
e = 2.7182845904523536
fermiFuncDouble :: Double -> Double
fermiFuncDouble x = 1.0/(1.0 + e^(-x))
Run Code Online (Sandbox Code Playgroud)
然后使用Either:
fermiFunc :: (Floating a) => Either Float Double -> a
fermiFunc Right x = double2Float (fermiFuncDouble (float2Double x))
fermiFunc Left x = fermiFuncDouble x …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一些读取Lambda表达式并输出beta缩减版本的内容.Lambdas的输入方式如下:\ variable - > expression和applications将采用(表达式)(表达式)的形式.因此,如果在字符串的开头找到'\',它知道处理一个Lambda,如果找到'(',它知道处理一个应用程序.
我有一个Lambda表达式定义的类型:
data Expression = Variable String
| Lambda Expression Expression
| Apply Expression Expression
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试编写读取输入的函数
processInput :: String -> Expression
processInput ('\':input) = processLambda input
processInput ('(':input) = processApply input
processInput str = Variable str
Run Code Online (Sandbox Code Playgroud)
当我尝试加载此功能时,我得到了
lexical error in string/character literal at ':'
Run Code Online (Sandbox Code Playgroud)
所以我尝试使用警卫:
processInput input
| head input == '\' = processLambda (tail input)
| head input == '(' = processApply (tail input)
| otherwise = Variable input
Run Code Online (Sandbox Code Playgroud)
但得到了
lexical error in string/character literal …Run Code Online (Sandbox Code Playgroud) 最近我做了一个程序,我使用了以下形式的数据类型:
data MyType = Constructor1 | Constructor2 deriving Eq
Run Code Online (Sandbox Code Playgroud)
是的,这种类型实际上与Bool我刚才命名的不同,以使我的代码更具可读性.在程序的后期,我有表格的功能
myFunc input = if input == Constructor1 then --do something
else --do something else
Run Code Online (Sandbox Code Playgroud)
我认为这可能是一个坏主意的原因是,如果它被解释为它的方式,每次程序遇到这个分支时,它必须运行==它设置的函数MyType来获得Bool通过对于该if_then_else_功能,如果我刚刚使用Bool该==功能的必要性被消除,这将加快该过程.
我应该替换所有实例的MyType实例,Bool还是ghc以某种方式优化数据类型的使用?
看起来在几乎所有语言中都有这个函数,但是我在String或Core.String中找不到一个函数,而且这个站点上的所有相关问题似乎都要求一个从int到string的函数.
我是F#的新手,我正在编写一个程序,需要找到某个列表中给定长度的每个子列表.我不知道如何解决这个问题所以我读了这个问题并决定将答案移植到F#.这就是我所拥有的:
let rec getSubLists (len : int) (list : List<int>) : List<List<int>> =
let result = new List<List<int>>()
let current = new List<int>()
let rec findSubLists (len : int) (superSet : List<int>) (current : List<int>) (soln : List<List<int>>) (idx : int) : unit =
if current.Length = len then soln.Insert(len - 1, current)
elif idx = superSet.Length then
let x = superSet.[idx]
current.Insert(len, x)
findSubLists len superSet current soln (idx + 1)
current.RemoveAt(x)
findSubLists len superSet …Run Code Online (Sandbox Code Playgroud) 感谢这个问题的答案,我已经定义了这样一个类型:
data Chain = forall a. Integral a => Chain [[a]] [a] a a
Run Code Online (Sandbox Code Playgroud)
我需要为每个字段或参数编写一个getter函数,如果你愿意的话.这是我的第一次尝试:
getSimplices (Chain simplices _ _ _) = simplices
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译ghc时会出现以下错误:
Chain.hs:10:40: error:
• Couldn't match expected type ‘t’ with actual type ‘[[a]]’
because type variable ‘a’ would escape its scope
This (rigid, skolem) type variable is bound by
a pattern with constructor:
Chain :: forall a. Integral a => [[a]] -> [a] -> a -> a -> Chain,
in an equation for ‘getSimplices’
at …Run Code Online (Sandbox Code Playgroud) 我做了一些测试:
import Control.Parallel.Strategies
import Data.Vector as V
import Data.Maybe
parMapVec :: (a -> b) -> Vector a -> Vector b
parMapVec f v = runEval $ evalTraversable rpar $ V.map f v
range :: Integer -> Integer -> Vector Integer
range x y
| x == y = x `cons` empty
| x < y = x `cons` (range (x + 1) y)
| x > y = (range x (y + 1)) `snoc` y
fac :: Integer -> Integer …Run Code Online (Sandbox Code Playgroud) 每当我尝试使用 、 和其他几个软件包安装某些东西时stack,$ stack install hip它们$ stack install Gifcurry都会抛出相同的错误:
Downloaded lts-12.9 build plan.
AesonException "Error in $.packages.cassava.constraints.flags['bytestring--lt-0_10_4']: Invalid flag name: \"bytestring--lt-0_10_4\""
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试使用stack安装任何东西,所以我不知道该怎么做。在做这件事之前我确实跑步了$ stack upgrade。
堆栈版本是1.5.1 x86_64,它位于目录中usr/bin/stack。
为什么我会写这样的东西:
main :: IO ()
main = getLine >>= putStrLn
Run Code Online (Sandbox Code Playgroud)
从那以后getLine :: IO String,putStrLn :: String -> IO String似乎这个行动的整体类型应该是IO String.为什么编译而不是给出类型错误?我能想出的唯一原因是在编译时>> return ()添加到值的末尾main.那么这里发生了什么?