Re: The Haskell Road to Logic, Math and Programming中的一个问题不知怎么解决?

12 views
Skip to first unread message
Message has been deleted

lee

unread,
Jul 19, 2008, 2:17:08 AM7/19/08
to hask...@googlegroups.com

在 2008-7-17,下午10:30, Weizho...@gmail.com 写道:

>
> Exercise 1.14 A function for transforming strings into strings is of
> type String
> -> String. Write a function blowup that converts a string
> a1 a2 a3 · · ·
> to
> a1 a2 a2 a3 a3 a3 · · · .
> blowup "bang!" should yield "baannngggg!!!!!". (Hint: use ++ for
> string
> concatenation.)

我先抛块砖 :-)

ACA80110:tmp lee$ cat blowup.hs
module Blowup where

repeatn p = take n (repeat c)
where c = fst p
n = snd p

blowup ss = foldr (++) [] (map repeatn (zip ss [1..]))

ACA80110:tmp lee$ ghci blowup.hs
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
Loading package base ... linking ... done.
[1 of 1] Compiling Blowup ( blowup.hs, interpreted )
Ok, modules loaded: Blowup.
*Blowup> blowup "bang!"
"baannngggg!!!!!"
*Blowup>

lee

Reply all
Reply to author
Forward
0 new messages