我试图从前到后获得状态.是否有方便的Haskell函数从列表中删除重复的元组?或者它可能有点复杂,比如遍历整个列表?
Before: the list of tuples, sorted by word, as in
[(2,"a"), (1,"a"), (1,"b"), (1,"b"), (1,"c"), (2,"dd")]
After: the list of sorted tuples with exact duplicates removed, as in
[(2,"a"), (1,"a"), (1,"b"), (1,"c"), (2,"dd")]
Run Code Online (Sandbox Code Playgroud) 所以我正在写一行来获取列表的倒数第二个元素.最初我的代码是
mySLast x = last.take ((length x) - 1) x
Run Code Online (Sandbox Code Playgroud)
哪个last功能一直有效.意识到我的take业务已经包含在Haskell中,init所以我改写为
mySLast = last.init
Run Code Online (Sandbox Code Playgroud)
这仍然不起作用.我觉得这是令人费解,因为init::[a]->[a]和last::[a]->a所以他们绝对应该在组合的同态Hask类别.
我试过问Haskell它认为类型是什么,它说
ghci> :t last.init
last.init :: [c] -> c
ghci> last.init [3,2,4,1]
<interactive>:45:6:
Couldn't match expected type ‘a -> [c]’
with actual type ‘[Integer]’
Relevant bindings include
it :: a -> c (bound at <interactive>:45:1)
Possible cause: ‘init’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘init [3, …Run Code Online (Sandbox Code Playgroud) 任何人都可以告诉我$以下Haskell线的功能.$$如果为最后一行,但功能$?
concat $ replicate 3 "12345"
Run Code Online (Sandbox Code Playgroud) 这是两段代码.
工作:
joins :: [String] -> String -> String
joins [] _ = ""
joins [x] _ = x
joins xs d = head xs ++ d ++ (joins (tail xs) d)
Run Code Online (Sandbox Code Playgroud)
不工作:
joins :: [String] -> String -> String
joins [] _ = ""
joins [x] _ = x
joins [x:xs] d = x ++ d ++ (joins xs d)
Run Code Online (Sandbox Code Playgroud)
后者的错误日志是:
test.hs:4:18:
Couldn't match expected type `[Char]' with actual type `Char'
In the first argument of `(++)', namely `x' …Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手.我从我的任务中得到了这个问题.它要我让这段代码工作:
area_of_triangle :: Float
-> Float
-> Float
-> Maybe Float
Run Code Online (Sandbox Code Playgroud)
我知道如何做到这一点Maybe; 就像是:
area_of_triangle :: Float -> Float -> Float -> Float
area_of_triangle a b c = sqrt(s*(s-a)*(s-b)*(s-c))
where
s = (a+b+c)/2
Run Code Online (Sandbox Code Playgroud)
我想要的是if area_of_triangle=0.0,return Nothing(因为这样的三角形不存在).但我不知道怎么写这个.
我试过谷歌搜索它,但我找不到一个简单的解释.谁能解释一下这个功能是如何工作的
import Data.Char
encode :: Int -> String -> String
encode offset msg = map (chr . (+ offset) . ord) msg
Run Code Online (Sandbox Code Playgroud)
我知道它正在映射(chr . (+ offset) . ord)函数msg,但内部发生了什么(chr . (+ offset) . ord).
所以,我有这个Haskell问题要解决:
定义一个mapIO函数,它接收一个函数f和一个输入和输出动作,a并产生一个输入和输出动作,当执行时,它执行给定的动作a并将应用程序返回f给返回a.
这是我的代码:
mapIO f a = do b <- a
return f(b);
Run Code Online (Sandbox Code Playgroud)
它编译但不起作用.当我尝试执行与以下执行示例相同的操作时,它不起作用.拜托,有人可以帮帮我吗?
Prelude Data.Char> mapIO even readLn
75
False
Run Code Online (Sandbox Code Playgroud) 我必须用该元素的出现次数替换列表中的所有元素,就像我有“泰勒斯威夫特”一样,结果将是 [1,1,1,1,1,1,1,1,1,1 ,1,1]。
我已经编写了计算出现次数的代码,我只知道如何用我已经尝试过的出现次数替换所有元素:
ocurr :: [Char] -> Char -> Int
ocurr xs x = length(filter (x==) xs)
frequencias :: [Char] -> [Char]
frequencias "" = []
frequencias xs = [ ocurr xs y| y <- xs]
Run Code Online (Sandbox Code Playgroud)
和
ocurr :: [Char] -> Char -> Int
ocurr xs x = length(filter (x==) xs)
frequencias :: [Char] -> [Char]
frequencias "" = []
frequencias xs = [x | y <- xs x = ocurr xs x]
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用……有人可以帮我吗?
我想打印出一个"Hello,World!" Haskell中的消息.
这是我正在做的事情:
在文本编辑器上,我写道:
main = putStrLn "Hello, World!"
Run Code Online (Sandbox Code Playgroud)
使用文件名helloworld.hs保存它
在命令提示符(使用Windows 10)中,我写了以下内容:
$ ghc --make helloworld
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误消息:
<interactive>:2:1:
parse error on input `$'
Perhaps you inteded to use TemplateHaskell
Run Code Online (Sandbox Code Playgroud)
我应该在尝试编译之前将helloworld文件导入ghci吗?我尝试过但仍然遇到了同样的错误.还尝试在编译行上包含文件扩展名,也没有运气.
###编辑### 非常感谢Carsten,Fraser,Sarah和Jakub Daniel.问题在于$符号.省略它解决了错误,现在我可以编译它.
如果有人遇到与我相同的问题,省略$将解决编译和运行程序的问题.
而不是
$ ghc --make filename
和
$ ./filename
尝试
ghc --make filename
Run Code Online (Sandbox Code Playgroud)
和
filename
Run Code Online (Sandbox Code Playgroud)
是的,在编译之后,您所要做的就是写下您要运行的文件的名称.
我希望通过滚动两个骰子生成一个点列表.如果两个骰子显示相同的值,则输出点将变为(i,i,i,i).
我的代码就像
[(i,j)|i<-[1..6],j<-[1..6], if i==j
then (i,i,i,i)
else (i,j)]
Run Code Online (Sandbox Code Playgroud)
当我把它输入GHCi时,它无法编译.
我期望的输出是
[(1,1,1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(2,1),(2,2,2,2),(2,3),(2,4),(2,5),(2,6),(3,1),(3,2),(3,3,3,3),(3,4),(3,5),(3,6),(4,1),(4,2),(4,3),(4,4,4,4),(4,5),(4,6),(5,1),(5,2),(5,3),(5,4),(5,5,5,5),(5,6),(6,1),(6,2),(6,3),(6,4),(6,5),(6,6,6,6)]
Run Code Online (Sandbox Code Playgroud) 我是函数式编程的新手,我正在尝试解决以下练习;
鉴于类型
type Cont r a = (a -> r) -> r
Run Code Online (Sandbox Code Playgroud)
实现以下高阶函数
mapReader :: (a -> b) -> (Cont r a) -> Cont r b
Run Code Online (Sandbox Code Playgroud)
第一步是简化类型,它给出:
mapReader :: (a -> b) -> ((a -> r) -> r) -> (b -> r) -> r
Run Code Online (Sandbox Code Playgroud)
接下来,定义需要在此函数中提供的参数.这些参数是我们得到的三个函数
mapReader :: (a -> b) -> ((a -> r) -> r) -> (b -> r) -> r
mapReader f g h = _1
Run Code Online (Sandbox Code Playgroud)
从这里,我们可以定义以下类型:
f :: a -> b
g :: (a -> r) -> …Run Code Online (Sandbox Code Playgroud) 我想从字符列表中创建一个字符串[Lettre]
例如,['1','2','3','W','5','5','W','3']应打印为(1、2、3,W,5 ,5,W,3)
现在,我用
data Lettre = Steen Char
makeString :: [Lettre] -> String
makeString [] = ""
makeString [Steen(x)] = x:[]
makeString (Steen(x):xs) = (x:", ") ++ makeString xs
Run Code Online (Sandbox Code Playgroud)
但我觉得这样可能会更容易。
是否有一个简单的函数,例如toString?我尝试使用一些我知道的函数,但是它似乎不适用于我的代码,因此我继续使用makeString。
非常感谢任何可以帮助我的人:)
当输入是数字时,我的搜索功能正常工作,但当输入不是时,我的搜索功能会崩溃.我可以添加什么来防止这种情况发生?
searchAge = do
putStrLn "\n Please type the age of the person you're looking for: \n"
age <- getLine
input <- readFile $ "databas.txt"
putStrLn "\n Here are all the people that matches your search: \n"
let people = parse input
output = map personToString (filter (\p -> personAge p == read age) people)
in putStrLn (unlines output)
putStrLn "Let's get back to the search menu again!"
searchDatabase
Run Code Online (Sandbox Code Playgroud)