小编lef*_*out的帖子

Haskell中的"多种类型"

我对函数式编程和Haskell都很陌生,所以我不确定我是否正确地提出了这个问题,或者它是否有意义,但我决定尝试,因为我没有找到任何有用的东西.我基本上试图实现一个可以返回a Int,a String或List 的函数.我知道我可以用它Either来返回两种类型中的一种,但我想返回三种或更多种中的一种.我尝试定义一个新类型,但我卡住了.

data Rets = Int | String | Bool

checkInt :: Rets -> Bool
check x = case x of
    Int x -> True
Run Code Online (Sandbox Code Playgroud)

checkInt应该返回,True如果给出Int,它只是为了测试,但无论如何我包括它.

我知道我的问题很乱,所以我会感谢任何解释.提前致谢!

haskell functional-programming

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

在 Haskell 中使用 Data.Sequence

我编写了一个程序,结果使用列表太慢了,所以我尝试切换到序列。但是,在查看文档后我似乎无法找出正确的语法。

\n\n

到目前为止,我正在尝试使用这个简单的代码来学习:

\n\n
import Control.Monad\nimport qualified Data.Sequence as S\n\nmain :: IO ()\nmain = do \n       let testSeq = S.empty\n       testSeq S.|> 5\n       testSeq S.|> 20\n       testSeq S.|> 3\n       let newSeq = S.update 2 3 testSeq\n       let x = lookup 2 testSeq\n       print x\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已经尝试了一段时间的语法但没有运气,但它仍然有很多错误:

\n\n
test.hs:9:8:\n    Couldn't match expected type \xe2\x80\x98IO a0\xe2\x80\x99\n                with actual type \xe2\x80\x98S.Seq Integer\xe2\x80\x99\n    In a stmt of a 'do' block: testSeq S.|> 5\n    In the expression:\n      do { let testSeq = S.empty;\n           testSeq S.|> …
Run Code Online (Sandbox Code Playgroud)

haskell

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

Haskell:绑定模式匹配的地方

目前我正在尝试通过在线教程学习Haskell来学习Haskell.在"函数中的语法"一章中,作者写道"你也可以使用绑定到模式匹配的地方!".之后有一部分代码示例,但我不知道模式匹配与新的where绑定一起使用的位置.因为代码块的第一部分被缩短了("我们可以将我们之前函数的where部分重写为"),你只能推断它,但我认为我选择了正确的部分.

功能:

bmiTell :: (RealFloat a) => a -> a -> String  
bmiTell weight height  
    | bmi <= skinny = "You're underweight, you emo, you!"  
    | bmi <= normal = "You're supposedly normal. Pffft, I bet you're ugly!"  
    | bmi <= fat    = "You're fat! Lose some weight, fatty!"  
    | otherwise     = "You're a whale, congratulations!"  
    where bmi = weight / height ^ 2  
          skinny = 18.5  
          normal = 25.0  
          fat = 30.0
Run Code Online (Sandbox Code Playgroud)

要替换的新where部分:

where bmi = weight …
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching pattern-guards

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

Haskell - 基本尾递归

我有一个具有参数的函数

whatIndex ::  (Eq a) => a -> [a] -> Integer
Run Code Online (Sandbox Code Playgroud)

我返回内部[a]的索引,从0开始,或者如果找不到则返回-1.这就是我写的

module WhatIndex where

whatIndex ::  (Eq a) => a -> [a] -> Integer

whatIndex p [] = -1

whatIndex p (a:as) 
    | p==a = index
    | otherwise = whatIndex p as
    where index = 1+whatIndex p as
Run Code Online (Sandbox Code Playgroud)

显然,我在这里没有正确增加索引.知道为什么这不起作用吗?另外,我无法更改参数.

========================

这是一些基本的输入/输出

whatIndex 3 [] = -1
whatIndex 2 [1,2,3,2,1]=1
whatIndex 1 [1,2,3,2,1]=0
whatIndex 'b' ['a' .. 'z']=1
Run Code Online (Sandbox Code Playgroud)

recursion haskell tail-recursion

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

在Haskell中调用比较运算符

我需要找出差异AB的差异是否最小:

smallestDifference3 :: Int -> Int -> Int -> Int
smallestDifference a b c
    | differenceAB < differenceBC < differenceAC = differenceAB
    | otherwise = differenceAB
  where differenceAB 
         | a < b = -(a - b)
         | otherwise    = a - b
        differenceBC
         | b < c = -(b - c)
         | otherwise    = b - c
        differenceAC
         | a < c = -(a - c)
         | otherwise    = a - c
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

cannot mix `<' [infix 4] and `<' …
Run Code Online (Sandbox Code Playgroud)

haskell function call

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

如何在Haskell中将变量与数据类型进行比较?

data BTree a = Empty | Node a (BTree a) (BTree a) deriving Show

type Aluno = (Numero,Nome,Regime,Classificacao)
type Numero = Int
type Nome = String
data Regime = ORD | TE | MEL  deriving Show
data Classificacao = Aprov Int| Rep| Faltou deriving Show
type Turma = BTree Aluno 
Run Code Online (Sandbox Code Playgroud)

我有这个功能,计算有多少"Alunos"有Regime TE.

我的代码:

numeroT :: Eq  => Turma -> Int
numeroT Empty = 0
numeroT (Node (x,_,r,_) e d) = if (r==TE) then 1+((numeroT e)+(numeroT d))
                                      else (numeroT e)+(numeroT d) …
Run Code Online (Sandbox Code Playgroud)

haskell haskell-platform

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

将String - > [String] - > [String]类型的函数应用于两个[String]

我努力想出一个好头衔.

这是要点:

我有一个函数(remIgnored)打算从字符串列表中删除字符串.

module Main(main) where

import System.Environment
import Data.List
import Data.Char

getLines :: FilePath -> IO [String]
getLines path = do 
    ls <- readFile path
    return (lines ls)

getWords :: [String] -> [String]
getWords ws = words (unlines ws)

remIgnored :: String -> [String] -> [String]
remIgnored _ []                 = []
remIgnored x (y:ys) | x == y    = remIgnored x ys
                    | otherwise = y : remIgnored x ys

main :: IO ()
main = do …
Run Code Online (Sandbox Code Playgroud)

string haskell types function list

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

=,< - ,<=在haskell之间有什么区别?

我们平时看到的=,<-,<=在Haskell.这些有什么区别?

syntax haskell

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

如何在haskell中的函数参数中传递列表?

我知道这个问题以前已被问过很多次了,我已经仔细阅读了它们,但这并没有帮助我回答我的问题类型.我对Haskell很新,

让我们假设我们有以下内容:

filter p [] = []
filter p (h:l) = if (p h) then (h:(filter p l)) else (filter p l)
Run Code Online (Sandbox Code Playgroud)

我有两个问题

  1. 我怎么称呼过滤器?我所知道的是你传递的p是一个列表

  2. 老实说,我一般都不知道什么是多态类型,我无法弄清楚多态类型的过滤函数.

我没有事件了解函数过滤器在if语句中的作用.

如果你能帮我解决这两个问题,我将非常感激.

有很多资源可以解释多态性,但我不理解它们.

polymorphism haskell function list

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

将IO操作结果存储在LET子句中

你好有人可以解释一下let block如果你知道你将从那时起使用它(你需要像头一样的结果),你如何在haskell中存储一个动作的结果?

在我的情况下,我调用了两次listDirectory,我想在变量中保存第一个调用的值,initialList以便稍后将其长度与稍后的调用进行比较listDirectory.

如何listDirectory在我的变量中存储第一个调用initialList

module Main where 

import System.IO
import System.Directory
import DB(db)
import Company

main::IO()
main = do
    putStrLn "Insert folder for output:"
    folder<-getLine
    makeDir folder>>= \b -> 
        putStrLn (if b then "Created" else "Existed Already")

makeDir::String->IO  Bool
makeDir dirname=let root="D:\\"
                    enumerateDirs=listDirectory root
                    initialList=<<enumerateDirs in  //how can i store it here?

     if  dirname `elem` initialList then
        putStrLn "Directory found , folder count:"++length initialList
     else  
        createDirectory root++dirname>>
        length …
Run Code Online (Sandbox Code Playgroud)

haskell

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