map with lists based on conditions

23 views
Skip to first unread message

Philip Sivyer

unread,
Oct 31, 2014, 11:03:55 AM10/31/14
to haskel...@googlegroups.com
Hi - newbie
Is it possible to use a condition on a list using map eg
 
map (*4) [1,2,3,4,5,4,3,2,1,20]
[4,8,12,16,20,16,12,8,4,80]
 
 
..... what I would like to achieve is that if the number in the list is <4 then map (*4) [1,2,3,4,5,4,3,2,1,20] else if  the number in the list is between 4 & 5 then map (/4) [1,2,3,4,5,4,3,2,1,20]
else map (+4) [1,2,3,4,5,4,3,2,1,20]
 
so, my result would be..
>[4,8,12,1,1.25 ,1,12,8,4,24]
 
or maybe map is not the answer?
 
Regards

Philip Sivyer

unread,
Nov 4, 2014, 7:31:49 AM11/4/14
to haskel...@googlegroups.com
Hi
I've answered my own question .... would appreciate comments if better way.
 
Regards
 
listManip n | n <4                   = n*4
                  | n >3 && n < 6    = n/4
                  | otherwise           = n+4
 
listManip1 = [1,2,3,4,5,4,3,2,1,20]

main = do 
print(map (listManip) listManip1)
Reply all
Reply to author
Forward
0 new messages