Haskell:如何在do块中操作字符串类型 [英] Haskell: how to operate the string type in a do block

查看:126
本文介绍了Haskell:如何在do块中操作字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个函数,首先将列表l分为两个列表m和n。然后创建两个线程来找出两个列表中最长的回文。我的代码是:

  import Control.Concurrent(forkIO)
import System.Environment(getArgs)
import Data.List
import Data.Ord

main = do
l < - getArgs
forkIO $ putStrLn $ show $ longestPalindr $ mList l
forkIO $ putStrLn $ show $ longestPalindr $ nList l

longestPalindr x =
snd $ last $ sort $
map(\l - >(length l,l))$
map head $ group $ sort $
filter(\y - > y == reverse y)$
concatMap inits $ tails x

mList l = take(length l`div` 2)l

nList l = drop(length l`div` 2)l

现在我可以编译它,但结果是[]。当我运行 longestPalindr mList 时,我得到了正确的结果。我认为这里的逻辑是正确的。那么问题是什么?

解决方案

问题标题可能需要更改,因为这不再是输入错误。 / p>

程序的功能可以通过在列表的两半之间简单地映射 longestPalindr 来解决。在你的代码中,你可以找到 [[Char]] 中最长的回文,所以结果长度通常只有1个。



我给出了一个 par 和 pseq 的简单示例。这只是向编译器建议,独立评估左侧和右侧可能很明智。它并不保证并行评估,而是由编译器决定。



请参阅Wiki上的 Parallel Haskell 为了理解火花,使用 -threaded 标志进行编译,然后使用 + RTS -N2 运行它。添加 -stderr 进行性能分析,看看这里是否有任何好处。我希望在你开始喂它更长的列表之前,你会得到负面回报。



关于函数并行性的更多阅读,请看看 Control.Parallel.Strategies

  import Control.Parallel(par,pseq)
在Haskell中手动加入线程只是非确定性的场景。 import System.Environment(getArgs)
import Data.List
import Data.Ord
import Control.Function(on)

main = do
l< ; - getArgs
let left = map longestPalindr(mList l)
right = map longestPalindr(nList l)
left`par` right`pseq` print $ longest(left ++ right)

longestPalindr x =最长的朋友
其中pals = nub $ filter(\y-> y ==反向y)子串
子串= concatMap inits $尾数x

最长= maximumBy(比较长度)

mList l = take(length l`div` 2)l

nList l = drop(length l`div` 2)l


I want to make a function that firstly divides a list l to two list m and n. Then create two thread to find out the longest palindrome in the two list. My code is :

import Control.Concurrent (forkIO)
import System.Environment (getArgs)
import Data.List
import Data.Ord

main = do
    l <- getArgs
    forkIO $ putStrLn $ show $ longestPalindr $ mList l
    forkIO $ putStrLn $ show $ longestPalindr $ nList l

longestPalindr x =
    snd $ last $ sort $
        map (\l -> (length l, l)) $
            map head $ group $ sort $
                filter (\y -> y == reverse y) $
                    concatMap inits $ tails x

mList l = take (length l `div` 2) l

nList l = drop (length l `div` 2) l

Now I can compile it, but the result is a [ ]. When I just run the longestPalindr and mList , I get the right result. I thought the logic here is right. So what is the problem?

解决方案

The question title may need to be changed, as this is no longer about type errors.

The functionality of the program can be fixed by simply mapping longestPalindr across the two halves of the list. In your code, you are finding the longest palindrome across [[Char]], so the result length is usually just 1.

I've given a simple example of par and pseq. This just suggests to the compiler that it may be smart to evaluate left and right independently. It doesn't guarantee parallel evaluation, but rather leaves it up to the compiler to decide.

Consult Parallel Haskell on the wiki to understand sparks, compile with the -threaded flag, then run it with +RTS -N2. Add -stderr for profiling, and see if there is any benefit to sparking here. I would expect negative returns until you start to feed it longer lists.

For further reading on functional parallelism, take a look at Control.Parallel.Strategies. Manually wrangling threads in Haskell is only really needed in nondeterministic scenarios.

import Control.Parallel (par, pseq)
import System.Environment (getArgs)
import Data.List
import Data.Ord
import Control.Function (on)

main = do
    l <- getArgs
    let left = map longestPalindr (mList l)
        right = map longestPalindr (nList l)
    left `par` right `pseq` print $ longest (left ++ right)

longestPalindr x = longest pals
    where pals = nub $ filter (\y -> y == reverse y) substrings
          substrings = concatMap inits $ tails x

longest = maximumBy (compare `on` length)

mList l = take (length l `div` 2) l

nList l = drop (length l `div` 2) l

这篇关于Haskell:如何在do块中操作字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆