Friday 15 July 2011

haskell - How to stop recursing and produce the list in memory -



haskell - How to stop recursing and produce the list in memory -

(1.) function "samestring" returns boolean value whether 2 strings same regardless of capitalisation.

-- *main> samestring "hello" "hello" -- true -- *main> samestring "hello" "hi there" -- false samestring :: string -> string -> bool samestring str1 str2 | length str1 == length str2 = , [ == b | (a,b) <- zip (capitalise str1) (capitalise str2) ] | otherwise = false

(1) helper function "capitalise" capitalising.

capitalise :: string -> string capitalise str = [ toupper x | x <- str ]

(2) function "prefix" returns boolean value states whether first string prefix of second, regardless of capitalisation.

-- *main> prefix "bc" "abcde" -- false -- *main> prefix "bc" "bcde" -- true prefix :: string -> string -> bool prefix [] [] = true prefix substr str | samestring string' substr == true = true | otherwise = false chop_str :: string -> string -> string chop_str str substr = (take (length substr) str) string' = chop_str str substr

(3.) function "dropuntil" returns contents of sec string after first occurrence of first string. if sec string not contain first substring, should homecoming empty string.

*main> dropuntil "cd" "abcdef" "ef" dropuntil :: string -> string -> string dropuntil substr [] = "" dropuntil substr (s:tr) | prefix substr (s:tr) == false = drop 1 s : dropuntil substr tr | prefix substr (s:tr) == true =

so question. thinking of doing dropuntil recursion.

what think function above should is:

1) given string , substring (for substring not prefix of string) ...

it should drop head of string ...

and cons empty list ""

... recursive phone call on remaining tail , same substring.

the thought behind maintain dropping head of list until substring becomes prefix of list, function should produce remainder of string result.

however, have no thought how this. want make

| prefix substr (s:tr) == true = "leftover_string"

where "leftover_string" remains after recursive phone call drops elements until status met substring prefix of remainder.

is possible way started?

is want?

| prefix substr (s:tr) == true = drop (length substr) (s:tr)

some notes:

you have type error here:

| prefix substr (s:tr) == false = drop 1 s : dropuntil substr tr ^^^^^^^^

i think want:

| prefix substr (s:tr) == false = dropuntil substr tr

haskell recursion

No comments:

Post a Comment