Haskell List Comprehension - Ineffective Predicate -


i'm pretty brand new haskell (only written fizzbuzz program before current one) , trying write program takes unix wordlist ('/usr/share/dict/words') , prints out list of anagrams word, direct palindromes starred. have meat of summed 1 function:

findanagrams :: [string] -> [(string, [string])] findanagrams d =   [x | x <- map (\s -> (s, [if reverse s == t t ++ "*" else t | t <- d, s /= t && null (t \\ s)])) d, not (null (snd x))] 

however, when run program output:

abase: babes, bases abased: debase abasement: basements abasements: abatements abases: basses 

and on, isn't working properly. intention list comprehension read follows: t in d such t not equal s , there no difference between t , s other order, if t reverse of s include t*, otherwise include t. problem seems "no difference between t , s other order" part, i'm trying accomplish using "null (t \ s)". seems should work. testing in ghci gives:

prelude data.list> null ("abatements" \\ "abasements") false 

and yet passes predicate test. assumption i'm missing simple here, i've looked @ while , can't quite come it.

in addition, notes regarding best practice appreciated.

if break out multiple functions (remember, source code size not important), like:

import data.list  ispalindrome :: string -> bool ispalindrome s = s == reverse s  flagpalins :: [string] -> [string] flagpalins [] = [] flagpalins (x:xs)     | ispalindrome x = x ++ "*"     | otherwise      = x  isanagram :: string -> string -> bool isanagram s t = (ispalindrome s || s /= t) && ??? -- test anagram  findanagrams :: string -> [string] -> [string] findanagrams s ws = flagpalins $ filter (isanagram s) ws  findallanagrams :: [string] -> [(string, [string])] findallanagrams ws = filter (not . null . snd) ??? -- words paired anagrams 

i've intentionally left holes fill in, i'm not going give answers ;)

there 2 spots yourself. 1 in findallanagrams should pretty easy figure out, you're doing pretty similar map (\s -> ...) part. intentionally structured isanagram it'll return true if it's palindrome or if it's anagram, , need 1 more check determine if t anagram of s. @ comment made on question hint there. if stuck, comment , ask additional hint, i'll give name of function think should use solve problem.


if want make list comprehension, recommend solving way, converting comprehension. in general should write more verbose code, compress once understand fully.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -