2

我正在学习haskell。我正在从文本文件中读取一个字符串,并且需要使这个字符串成为一个 char 列表。

输入文件是这样的:

Individuo A; TACGATCAAAGCT 
Individuo B; AATCGCAT 
Individuo C; TAAATCCGATCAAAGAGAGGACTTA 

我需要转换这个字符串

S1 = "AAACCGGTTAAACCCGGGG"  in  S1 = 
["A","A","A","C","C","G","G","T","T","A","A","A","C","C","C","G","G","G","G"] 
or S1 = 
['A','A','A','C','C','G','G','T','T','A','A','A','C','C','C','G','G','G','G'] 

但它们用“;”分隔

我应该怎么办?

我能做些什么?

获得两个列表后,我将它们发送到此代码:

lcsList :: Eq a => [a] -> [a] -> [a]
lcsList [] _ = []
lcsList _ [] = []
lcsList (x:xs) (y:ys) = if x == y
                          then x : lcsList xs ys
                          else
                            let lcs1 = lcsList (x:xs) ys
                                lcs2 = lcsList xs (y:ys)
                            in if (length lcs1) > (length lcs2)
                                  then lcs1
                                  else lcs2
4

2 回答 2

4

拆分每个字符串的粗略且现成的方法是使用类似这样的方法-您可以在 ghci 中尝试

let a = "Individuo A; TACGATCAAAGCT"
tail $ dropWhile (/= ' ') $ dropWhile (/= ';') a

这给了你:

"TACGATCAAAGCT"

由于 String 只是 Char 的列表,因此与以下内容相同:

['T', 'A', 'C', 'G', ...
于 2012-11-03T17:59:47.130 回答
3

If your file consists of several lines, it is quite simple: you just need to skip everything until you find “;”. If your file consists of just one line, you’ll have to look for sequences’ beginnings and endings separately (hint: sequence ends with space). Write a recursive function to do the task, and use functions takeWhile, dropWhile.

A String is already a list of Char (it is even defined like this: type String = [Char]), so you don’t have to do anything else. If you need a list of Strings, where every String consists of just one char, then use map to wrap every char (once again, every String is a list, so you are allowed to use map on these). To wrap a char, there are three alternatives:

  1. Use lambda function: map (\c -> [c]) s
  2. Use operator section: map (:[]) s
  3. Define a new function: wrap x = [x]

Good luck!

于 2012-11-03T17:48:14.100 回答