亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3989 | 回復(fù): 5
打印 上一主題 下一主題

[haskell]問個求子串位置的程序 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-07-16 12:07 |只看該作者 |倒序瀏覽
本帖最后由 retuor 于 2010-07-16 12:40 編輯

做了個練習,大家給點意見。

Exercise 10. Write a function         f :: String -> String -> Maybe Int
that takes two strings. If the second string appears within the first, it
returns the index identifying where it starts. Indexes start from 0. For
example,
         f "abcde" "bc" ==> Just 1
         f "abcde" "fg" ==> Nothing

我的想法是,如果 s2 是 s1=(x:xs) 的前綴,則返回 0,否則返回 1+ s2 在 xs 中的位置,如是遞歸下去。由于題目要求返回 Maybe 類型,最好能有  (Just 1)+(Just 1)=Just 2 之類的運算,所以寫了個 Maybe Int 的 instance.

  1. instance (Num a)=> Num (Maybe a) where
  2.     (Just n) + (Just m) = Just (n+m)
  3.     _+_=Nothing                          

  4. f [] s2=Nothing
  5. f s1@(x:xs) s2 = if isPrefix s1 s2 then Just 0 else (Just 1)+ (f xs s2)
  6.                  where
  7.                    isPrefix _ [] = True
  8.                    isPrefix (x:xs) (y:ys) = if x==y then isPrefix xs ys else False
  9.                    isPrefix _ _ = False
復(fù)制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2010-07-17 19:44 |只看該作者
本帖最后由 SNYH 于 2010-07-17 20:16 編輯

  1. else (Just 1)+ (f xs s2)
  2. 可以改為
  3. else  fmap (+1) (f xs s2)
  4. 不需要instance
  5. Maybe 是一個典型的functor
復(fù)制代碼
而且你實現(xiàn)的這個Num 的instance 不完全其他的幾個方法沒有實現(xiàn)會出現(xiàn)警告的


另外 isPrefix 在Data.List 中有一個對應(yīng)的函數(shù)  Data.List.isPrefixOf

論壇徽章:
0
3 [報告]
發(fā)表于 2010-07-17 19:45 |只看該作者
再補充點
isPrefix s1 s2
一般如果是這種名字的函數(shù)寫成
  s1 `isPrefix` s2  可讀性是不是強點?

論壇徽章:
0
4 [報告]
發(fā)表于 2010-07-17 21:11 |只看該作者
fmap 好,改過來了。

  1. f [] s2=Nothing
  2. f s1@(x:xs) s2 = if isPrefix s1 s2 then Just 0 else fmap (+1) (f xs s2)
  3.                  where
  4.                    isPrefix _ [] = True
  5.                    isPrefix (x:xs) (y:ys) = if x==y then isPrefix xs ys else False
  6.                    isPrefix _ _ = False
復(fù)制代碼
可讀性那個也很有道理,不過暫時不改了。

有收獲,謝謝啊。

論壇徽章:
0
5 [報告]
發(fā)表于 2010-07-17 21:28 |只看該作者
[code]fmap (+1) (f xs s2) [\code]
換成
[code]f xs s2 >>= (\x -> return (x+1))[\code]
也是差不多


一個用的functor  一個用的是monad

還可以直接使用 pattern match
比如寫個
maybeAdd Nothing _ = Nothing
maybeAdd _ Nothing = Nothing
maybeAdd (Just x) (Just y) = Just (x+y)

helper function 相對來說也比 實現(xiàn) 一個  instance 來實現(xiàn)(+)要好點吧。。。。

論壇徽章:
0
6 [報告]
發(fā)表于 2010-07-17 21:48 |只看該作者
嘿嘿,再次學到,

  1. f [] s2=Nothing
  2. f s1@(x:xs) s2 = if isPrefix s1 s2 then Just 0 else (f xs s2)>>=return.(+1)
  3.                  where
  4.                    isPrefix _ [] = True
  5.                    isPrefix (x:xs) (y:ys) = if x==y then isPrefix xs ys else False
  6.                    isPrefix _ _ = False
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP